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

Dynamic imports don't work in nodejs #9580

Closed
aalexgabi opened this issue Feb 17, 2020 · 5 comments
Closed

Dynamic imports don't work in nodejs #9580

aalexgabi opened this issue Feb 17, 2020 · 5 comments

Comments

@aalexgabi
Copy link

🐛 Bug Report

Cannot dynamically import module in jest tests.

To Reproduce

Steps to reproduce the behavior:

test('dynami', async () => {
    const tenderElement = await import('mymodule');
});

Throws:

% npx jest test/dy.spec.js
(node:25196) ExperimentalWarning: The ESM module loader is experimental.
 FAIL  test/dy.spec.js
   dynami (1ms)

   dynami

    TypeError: A dynamic import callback was not specified.TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback wa
s not specified.

      1 | test('dynami', async () => {
    > 2 |     const tenderElement = await import('mymodule');
        |                           ^
      3 | });

      at Object.<anonymous> (test/dy.spec.js:2:27)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.313s, estimated 1s
Ran all test suites matching /test\/dy.spec.js/i.

Expected behavior

Expected the dynamic import to work.

envinfo

% npx envinfo --preset jest
npx: installed 1 in 0.696s

  System:
    OS: Linux 5.3 Manjaro Linux
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 13.7.0 - /usr/bin/node
    Yarn: 1.22.0 - /usr/bin/yarn
    npm: 6.13.6 - /usr/bin/npm
  npmPackages:
    jest: ^24.9.0 => 24.9.0 
@aalexgabi
Copy link
Author

It seems that this is a known issue: #9430

Essentially the same as above, but passed as importModuleDynamically when constructing the module. Will also support jest.mock, jest.resetModules etc more cleanly, so likely to be used quite a bit.

This can also be done for vm.Script via the same option.

@SimenB
Copy link
Member

SimenB commented Feb 17, 2020

Correct, this is blocked by #9430. For now you'll need to transpile it (using Babel, TypeScript, or something else like it)

@SimenB SimenB closed this as completed Feb 17, 2020
@d3x0r
Copy link

d3x0r commented Nov 6, 2020

It's not clear that there is a method that works... an alternative workaround is perhaps to use .mjs instead of .js.

@SimenB
Copy link
Member

SimenB commented Nov 6, 2020

dynamic import has been implemented in #10620, released in 26.5.3

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants