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

[Bug]: Jest with ESM enable fails with ENOENT: no such file or directory, open '<<NAME>>' when trying to import a CJS module that re-export a core node module #13854

Closed
kowalski opened this issue Feb 3, 2023 · 4 comments · Fixed by #13856

Comments

@kowalski
Copy link
Contributor

kowalski commented Feb 3, 2023

Version

29.4.1

Steps to reproduce

  1. Clone my repo https://github.com/rumblefishdev/jest-reproduce-import-bug
  2. npm i
  3. npm run test

Expected behavior

You should see

(node:376068) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
 PASS  src/index.test.ts
  describe
    ✓ run main (12 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.653 s

Actual behavior


> jest-reproduce-import-bug@1.0.0 test
> NODE_OPTIONS=--experimental-vm-modules node_modules/.bin/jest src/index.test.ts

 FAIL  src/index.test.ts
  ● Test suite failed to run

    ENOENT: no such file or directory, open 'constants'

      at Runtime.readFileBuffer (node_modules/jest-runtime/build/index.js:2316:21)
          at Array.forEach (<anonymous>)
          at async Promise.all (index 0)
          at async Promise.all (index 1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.643 s
Ran all test suites matching /src\/index.test.ts/i.
(node:375769) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

Additional context

If you edit node_modules/iso-constants/index.js file like:

'use strict';

-module.exports = require('constants');
+const c = require('constants');
+module.exports = c;

and re-run the tests, stuff works fine and you the expected output.

I believe that the problem origins in this line: https://github.com/facebook/jest/blob/main/packages/jest-runtime/src/index.ts#L954
In here it should be caling this._resolver.isCoreModule(modulePath) and acting accordingly.

This is why patching index.js to replace re-export with normal export fixes the problem.

Environment

System:
    OS: Linux 5.15 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (12) x64 Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
  Binaries:
    Node: 16.18.0 - ~/.nvm/versions/node/v16.18.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.18.0/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v16.18.0/bin/npm
  npmPackages:
    jest: ^29.4.1 => 29.4.1
@SimenB
Copy link
Member

SimenB commented Feb 3, 2023

I believe that the problem origins in this line: main/packages/jest-runtime/src/index.ts#L954
In here it should be caling this._resolver.isCoreModule(modulePath) and acting accordingly.

Agreed! Wanna send a PR? 🙂

@kowalski
Copy link
Contributor Author

kowalski commented Feb 3, 2023

@SimenB I've attached a pull request. I tested this using the example repository I've attached and it fixes the problem with a particular package that was giving me a headache.
I wish I knew how to add a testcase for such scenario.

@SimenB
Copy link
Member

SimenB commented Feb 7, 2023

@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 Mar 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants