Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: ts-jest does not require ESM #4922

@codiophile

Description

@codiophile

Version

29.4.0

Steps to reproduce

Configure your CommonJS project to use the require ESM feature of TypeScript 5.8: #60761

Here's another example: #15331

Then run your tests.

Expected behavior

The tests run without problems.

Actual behavior

The test fails with:

SyntaxError: Cannot use import statement outside a module

Since TypeScript 5.8, it's not supposed to use an import statement, but rather use require, since this now works with ESM in Node 22. It seems like ts-jest is deviating from this behaviour, which means that I cannot test ES modules with jest in my CommonJS project.

Debug log

I'm afraid I am unable to share the logs, as my organisation does not allow me to. I could potentially set up a dummy project on a personal machine and add the logs later.

Additional context

I'm using NestJS, which is CommonJS and I was very happy to find out that it is now possible to seamlessly use ES modules in a CommonJS Typescript project, since version 5.8 of TypeScript. I tried it and it works fine, except, I am not able to import ESM from my tests. So far, I can get by, since I can simply mock the import, and it's not a dependency I need to write tests for, but it would be preferable if I could use ESM in tests as well.

Environment

System:
    OS: macOS 15.5
    CPU: (8) arm64 Apple M1
  Binaries:
    Node: 22.17.0 - ~/.nvm/versions/node/v22.17.0/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v22.17.0/bin/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions