Skip to content

[Bug]: testing code with dynamic imports and module nodenext #5218

@Hilzu

Description

@Hilzu

Version

29.4.6

Steps to reproduce

  1. Clone repo at https://github.com/Hilzu/ts-jest-dynamic-import
  2. Install dependencies: npm install
  3. Run tests: npm test

Expected behavior

Tests pass.

Actual behavior

Tests fail.

 FAIL  src/importer.test.ts
  ● tests

    Cannot find module './message.js' from 'src/importer.ts'

Debug log

ts-jest.log

Additional context

I'm trying to test code that uses dynamic imports. In tsconfig.json I have "modules": "nodenext" enabled but I still have "type": "commonjs" in package.json. This combination means that modules are transformed to CommonJS by TypeScript but dynamic imports are kept as is.

ts-jest doesn't seem to support this scenario. The local module that is dynamically imported is not found when running the tests.

If the code is first compiled with npm run build it works on runtime:

$ node .
world

Running the compiled test also works:

$ NODE_OPTIONS=--experimental-vm-modules npx jest dist/importer.test.js

 PASS  dist/importer.test.js
  ✓ tests (3 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.055 s, estimated 1 s
Ran all test suites matching dist/importer.test.js.

Not sure if this is a configuration issue on my side but I have not been able to find a combination of settings that works.

Environment

System:
    OS: macOS 26.3.1
    CPU: (16) arm64 Apple M4 Max
  Binaries:
    Node: 24.14.0 - /Users/hilzu/.asdf/installs/nodejs/24.14.0/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 11.9.0 - /Users/hilzu/.asdf/plugins/nodejs/shims/npm
    pnpm: 10.15.1 - /Users/hilzu/.asdf/shims/pnpm
  npmPackages:
    jest: ^30.3.0 => 30.3.0

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions