question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:49 (32 by maintainers)

github_iconTop GitHub Comments

6reactions
christophercurriecommented, Jun 20, 2018

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.

4reactions
screendrivercommented, Mar 8, 2018

Maybe we should re-open this issue? At the moment I have to enable allowSyntheticDefaultImports together with the new esModuleInterop and now my tests are failing again because of import() 🤔

What’s really strange: if I transpile my file directly with tsc I can see

case 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')];

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found