Dynamic import support?
See original GitHub issue- Issue
Because ts-jest
overrides module
to commonjs
in thetsconfig.json
no tests can be written with the new dynamic import('./myModule')
syntax. You get either 'import' and 'export' may only appear at the top level
or a Syntax error
.
- Expected behavior
Dynamic imports should work.
- Link to a minimal repo that reproduces this issue
Just type import('./myModule')
in your test or implementation. That’s enough.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:49 (32 by maintainers)
Top Results From Across the Web
JavaScript modules: dynamic import() | Can I use... Support ...
"Can I use" provides up-to-date browser support tables for support of front-end web ... Loading JavaScript modules dynamically using the import() syntax.
Read more >import - JavaScript - MDN Web Docs - Mozilla
The static import declaration is used to import read-only live bindings ... To load modules in non-module contexts, use the dynamic import ......
Read more >Advanced Features: Dynamic Import
Dynamically import JavaScript modules and React Components and split your code ... Next.js supports lazy loading external libraries with import() and React ...
Read more >Dynamic import()
Static import (recap) #. Chrome 61 shipped with support for the ES2015 import statement within modules. Consider the following module, located ...
Read more >JavaScript modules: dynamic import()
Browser Support for JavaScript modules: dynamic import() · Chrome · Safari · Firefox · IE Internet Explorer · Opera · Edge ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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: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.Maybe we should re-open this issue? At the moment I have to enable
allowSyntheticDefaultImports
together with the newesModuleInterop
and now my tests are failing again because ofimport()
🤔What’s really strange: if I transpile my file directly with
tsc
I can seecase 0: return [4 /*yield*/, Promise.resolve().then(function () { return require('../foo.json'); }
but when I run the test for that Jest complains with
case 0: return [4 /*yield*/, import('../foo.json')];