Skip to content

[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

@kowalski

Description

@kowalski

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions