Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError with newer JS features (nullish coalescing, optional chaining) by using TypeScript #3

Closed
gossi opened this issue Aug 1, 2020 · 7 comments

Comments

@gossi
Copy link

gossi commented Aug 1, 2020

Thanks for esbuild-jest,

unfortunately I'm not able to fully use it. I'm working on a CLI library running on node written in TS. I'm trying to switch from tsdx to esbuild. So far, I do have the build running, with this command (early/first build command):

esbuild --target=esnext --outfile=dist/theemo.esm.js --platform=node --bundle --format=esm src/index.ts

Now I wanna try to use jest with esbuild and TS getting this error:


    /Users/thomas/code/packages/theemo/test/reader.test.ts:134
                          x: effect.offset?.x ?? 0,
                                           ^

    SyntaxError: Unexpected token '.'

My feeling here is, that esbuild is either not picking up my tsconfig or something is not passed on properly from jest to esbuild to instruct it to do the ts transform.

// jest.config.ts
module.exports = {
  testEnvironment: 'node',
  transform: {
    "^.+\\.ts$": 'esbuild-jest'
  },
  testPathIgnorePatterns: ['/node_modules/', '/dist/', '/types/'],
}
// tsconfig.json
{
  "include": [
    "src",
    "types"
  ],
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "importHelpers": true,
    "declaration": true,
    "sourceMap": true,
    "rootDir": "./src",
    "alwaysStrict": true,
    "strict": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "resolveJsonModule": true,
    "moduleResolution": "node",
    "baseUrl": "./",
    "downlevelIteration": true,
    "paths": {
      "*": [
        "types/*",
        "node_modules/*"
      ]
    },
    "esModuleInterop": true
  }
}

Any ideas? How can I support this?

@aelbore
Copy link
Owner

aelbore commented Aug 3, 2020

@gossi will look into this. i get back you.

@aelbore
Copy link
Owner

aelbore commented Aug 3, 2020

@gossi what version of esbuild-jest you are using?
start esbuild-jest@0.2.x it uses the buildSync of esbuild i remove the dependencies to rollup and rollup-plugin-esbuild

just update to new version of esbuild-jest@0.2.x
then try this simple test (this works for me)

test('nullish coalescing, optional chaining', () => {

  const response = {
    data: [
      { age: 30, name: 'Jane' }
    ]
  }

  expect(response.data).toBeTruthy()
  // @ts-ignore
  expect(response?.status).toBeFalsy()
})

@gossi
Copy link
Author

gossi commented Aug 3, 2020

Two things to it:

  1. I'm occasionally try esbuild from time to time, yet not anywhere near perfect. For now I'm happy it compiles (not necessarily runs afterwards) - I'm all but an expert here.
  2. There is src/ and test/ in my project, the esbuild runs for src/ (which contains these features). Now I added tests with the help of esbuild-jest which consumes both src/ and test/ folder - but then the errors appear.

Following your example I made these scripts which compile my src for now:

const path = require('path');
const { buildSync } = require('esbuild');
const { builtinModules } = require('module');
const fs = require('fs');

const pkg = require(path.resolve('package.json'))
const external = [ 
  ...builtinModules, 
  ...Object.keys(pkg.dependencies || {})
]

const build = (outfile, options) => {
  buildSync({
    entryPoints: ['src/index.ts'],
    outfile,
    minify: false,
    bundle: true,
    write: true,
    target: 'es2015',
    sourcemap: true,
    platform: 'node',
    external,
    ...options
  });
}

build('dist/theemo.esm-build.js', {
  format: 'esm'
});

@aelbore
Copy link
Owner

aelbore commented Aug 4, 2020

@gossi im glad that it works, but have you updated the version of esbuild-jest?

@gossi
Copy link
Author

gossi commented Aug 4, 2020

I was seeing this on 0.2.0 - did an update just now to 0.2.1 but same errors.

I pushed the branch: theemo-tokens/theemo#3

Keep in mind, there is tsdx and esbuild running in parallel at the moment, I prefer to switch to esbuild (as ofc it is faster).

@aelbore
Copy link
Owner

aelbore commented Aug 4, 2020

@gossi yes theres some issue on esbuild-jest not sure why target = esnext is not working, i updated the target to "es2018"

also theres issue on esbuild with exporting default type i created bug to esbuild
evanw/esbuild#316

But i have workaround to make esbuild-jest work.

  1. update esbuild-jest to version 0.2.2 version
  2. remove the esbuild in devDependencies (esbuild-jest has already esbuild)
  3. change your ReaderConfig to name export like export { RenderConfig } esbuild didnt remove type in build output
  4. change npm script test to "test": "jest --clearCache && jest"

Let me know if you still encounter the issue

@gossi
Copy link
Author

gossi commented Aug 16, 2020

I just had the time to come back to this. Thanks to your findings and passing on an upstream bug and its fix, this is working brilliantly now. I'm using esbuild (v0.6.24) with esbuild-jest (v0.2.2) now.

I can happily close this now :)

Thank you so much for your support

@gossi gossi closed this as completed Aug 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants