FAIL src/lib/localizations/LocalizationManager.spec.tsx
● Test suite failed to run
/Users/Gabe/Sites/roastlogger-web/src/lib/docs/index.tsx: Unexpected token (6:18)
4 | const Loader = props => React.createElement('div', props);
5 | const Docs = loadableVisiblity({
> 6 | loader: () => import('./container'),
| ^
7 | loading: Loader
8 | });
9 | export { actions, actionTypes, KEY, reducer };
I think I ran into the same problem as you guys and I made a minimal repro repo here so that the maintainers can figure out what's wrong. Steps to reproduce:
Clone and run yarn
.
git checkout ea5ea730
.
Run npm test
. Tests fail because of the dynamic import in src/addAndMultiply.ts
.
git checkout df062207
.
Run npm test
. Tests pass because of this change.
Hi @kulshekhar,
unfortunately not because @huy-nguyen just disable allowSyntheticDefaultImports
for it's tests and that's it. That means that your code with third party dependencies (for example import React from 'react'
) will not compile anymore when you run tsc -p <your-directory-with-the-test-tsconfig>
.
I already have two tsconfig.json
files:
one for my production application code
one for my tests (where I set "module": "commonjs"
)
In my CI build I run tsc
and after that tsc -p tests
. When I enable allowSyntheticDefaultImports
in the first tsconfig.json
and disable that in in the test specific second tsconfig.json
the build will fail. If I would disable the second build step tsc -p tests
and just rely on jest and only would run the jest tests without compilation I get a lot of failing tests because for example
Warning: React.createElement: type is invalid -- expected a string (for built-in
components) or a class/function (for composite components) but got:
undefined. You likely forgot to export your component from the file it's
defined in, or you might have mixed up default and named imports.
because TypeScript emits wrong / no files for third party dependencies like import glamorous from 'glamorous';
Finally I could create a repository that reproduces this issue 🎉
If you run yarn tsc
everything works.
If you run yarn jest
you can see the new error coming up when I set allowSyntheticDefaultImports
to false
in my test tsconfig.json
If you run yarn tsc -p test-setup
you can see that the workaround above is not a real solution (additionally to the failing tests)
I enabled that now (I oversight that flag...) but the tests are now really slow (4 tests are running in 30 seconds 😳) and I'm getting different errors now:
TypeError: Cannot read property 'text' of undefined
at transpileViaLanguageServer (node_modules/ts-jest/dist/transpiler.js:61:21)
at Object.transpileTypescript (node_modules/ts-jest/dist/transpiler.js:10:16)
at process (node_modules/ts-jest/dist/preprocessor.js:27:41)
at Object.process (node_modules/ts-jest/index.js:8:51)
@screendriver I did, but the error persisted.
But then I removed the enableTsDiagnostics
flag, and it works, for some reason?
Also leaving the enableTsDiagnostics
flag, and setting the allowSyntheticDefaultImports
to false
also makes the tests work, so I'm not sure what's going on.
I did a pull request and it works now in your repository (it's a different issue as I said 😉)
noblica/ts-jest-dynamic-imports#1
Hi i've tried out the above solution with a separate jest tsconfig however i'm still getting the same error.
The only difference to add is that I'm in a mixed TS project (js enabled). Could this be the cause?
In case this helps anyone in the future, I got this working by configuring .babelrc
to add dynamic import support as indicated in the Jest docs:
"presets": [["env", {"modules": false}]],
"plugins": ["syntax-dynamic-import"],
"env": {
"test": {
"plugins": ["dynamic-import-node"]
Note you may need to run jest --clearCache
to clear your Jest cache to get this to work.
If you don't use babel for your project normally, you will still need to add a .babelrc
file and devDependencies on these two plugins just for this use case.
Use 100%cpu! work with plotly-extension and 'create a new view of output' of a chart that output by plotly.
jupyterlab/jupyterlab#5500
In case someone still struggles with this problem, what helped for me was changing Jest preset from
ts-jest/presets/js-with-babel
to ts-jest/presets/js-with-ts
. I guess it had something to do with the fact that in our codebase we use mix of js and ts files and our tests are written in TS while the tested file with dynamic import was still JS.