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

How come "allowJs" is required in tsconfig.json when using jsWithTs? #1080

Closed
javoire opened this issue Apr 25, 2019 · 4 comments
Closed

How come "allowJs" is required in tsconfig.json when using jsWithTs? #1080

javoire opened this issue Apr 25, 2019 · 4 comments

Comments

@javoire
Copy link

javoire commented Apr 25, 2019

Issue :

The flag seems to be programmatically set when using ts-jest/presets/js-with-ts yet the config docs says it is required. As is it when I run my tests (see log below).

Expected behavior :

I would expect ts-jest to process *.js files when setting ts-jest/presets/js-with-ts without having allowJs in tsconfig.json.

Debug log:

Without allowJs in tsconfig.json

log file content
# content of ts-jest.log :
TS_JEST_LOG=stdout yarn test -i                                                                                   1 ↵  INSERT
yarn run v1.13.0
$ web-scripts test -i
ts-jest[jest-preset] (DEBUG) creating jest presets handling JavaScript files
 FAIL  src/math.test.js
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /Users/javoire/dev/test-module-serve/src/math.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { add } from '.';
                                                                                                    ^

    SyntaxError: Unexpected token {

      at ScriptTransformer._transformAndBuildScript (../node_modules/@jest/transform/build/ScriptTransformer.js:471:17)
      at ScriptTransformer.transform (../node_modules/@jest/transform/build/ScriptTransformer.js:513:25)

ts-jest[Importer] (DEBUG) creating Importer singleton
ts-jest[jest-preset] (DEBUG) creating jest presets not handling JavaScript files
ts-jest[versions] (DEBUG) checking version of jest: OK
ts-jest[jest-transformer] (DEBUG) created new transformer
ts-jest[jest-transformer] (DEBUG) computing cache key for /Users/javoire/dev/test-module-serve/src/math.test.js
ts-jest[jest-transformer] (INFO) no matching config-set found, creating a new one
ts-jest[backports] (DEBUG) backporting config
ts-jest[config] (DEBUG) normalized jest config
ts-jest[config] (DEBUG) normalized ts-jest config
ts-jest[config] (DEBUG) babel is disabled
ts-jest[Importer] (DEBUG) loaded module typescript
ts-jest[Importer] (DEBUG) patching typescript
ts-jest[versions] (DEBUG) checking version of typescript: OK
ts-jest[config] (DEBUG) readTsConfig(): reading /Users/javoire/dev/test-module-serve/tsconfig.json
ts-jest[config] (DEBUG) normalized typescript config
Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.039s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

With allowJs in tsconfig.json

log file content
TS_JEST_LOG=stdout yarn test -i                                                                                     ✔  INSERT
yarn run v1.13.0
$ web-scripts test -i
ts-jest[jest-preset] (DEBUG) creating jest presets handling JavaScript files
 PASS  src/math.test.js
  ✓ adds two numbers (3ms)
  ✓ adds three numbers

ts-jest[Importer] (DEBUG) creating Importer singleton
ts-jest[jest-preset] (DEBUG) creating jest presets not handling JavaScript files
ts-jest[versions] (DEBUG) checking version of jest: OK
ts-jest[jest-transformer] (DEBUG) created new transformer
ts-jest[jest-transformer] (DEBUG) computing cache key for /Users/javoire/dev/test-module-serve/src/math.test.js
ts-jest[jest-transformer] (INFO) no matching config-set found, creating a new one
ts-jest[backports] (DEBUG) backporting config
ts-jest[config] (DEBUG) normalized jest config
ts-jest[config] (DEBUG) normalized ts-jest config
ts-jest[config] (DEBUG) babel is disabled
ts-jest[Importer] (DEBUG) loaded module typescript
ts-jest[Importer] (DEBUG) patching typescript
ts-jest[versions] (DEBUG) checking version of typescript: OK
ts-jest[config] (DEBUG) readTsConfig(): reading /Users/javoire/dev/test-module-serve/tsconfig.json
ts-jest[config] (DEBUG) normalized typescript config
ts-jest[jest-transformer] (DEBUG) computing cache key for /Users/javoire/dev/test-module-serve/src/index.js
ts-jest[jest-transformer] (DEBUG) computing cache key for /Users/javoire/dev/web-module-skeleton/test-module-serve/src/math.js
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.924s
Ran all test suites.
✨  Done in 2.39s.

Minimal repo :

Cheers!

@ahnpnl
Copy link
Collaborator

ahnpnl commented Apr 27, 2019

from what I understand about allowJs flag is it allows tsc to compile js files. When you don't set this flag to true and use preset js-with-ts, tsc cannot compile your js files to pass them to jest, therefore it passes a non-compiled version to jest and jest throws error.

@javoire
Copy link
Author

javoire commented May 1, 2019

Hey @ahnpnl! Yes that's my understanding as well, but allowJs is already set here https://github.com/kulshekhar/ts-jest/blob/master/presets/index.js#L5. So what I don't understand is why it is also required to set in your project's tsconfig, according to https://kulshekhar.github.io/ts-jest/user/config/#the-3-presets :)

@ahnpnl
Copy link
Collaborator

ahnpnl commented May 1, 2019

Hmm you are right, have you tried to debug to check what is the generated config that ts-jest creates ? Maybe the documentation is outdated 😀

updated: the code set allowJs in ts-jest actually isn't related to final config. It is just for internal codes when creating the preset. Therefore the documentation is correct that you need to set allowJs in tsconfig.

@ahnpnl ahnpnl closed this as completed Jan 1, 2020
@LoganTann
Copy link
Contributor

Please be aware that setting this property might cause a performance issue : #4294

ts-jest suggests another workaround : changing the transform regex in your jest config to select only ts files, and not ts and js

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

3 participants