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 loads jest.config (not spec files) as commonjs when it should treat it as esm #15391

Open
its-dibo opened this issue Nov 28, 2024 · 0 comments

Comments

@its-dibo
Copy link

its-dibo commented Nov 28, 2024

Version

29.7.0

Steps to reproduce

minimal reproduction

1- install jest, ts-jest
2- create jest.config.ts that imports an esm pacjage like json-strip-comments

import strip from 'strip-json-comments'

export default function jestConfig(){
// call the esm function 
strip(...);

return {
  // normal jest configs here
}
}

3- add type: module in package.json
4- update tsconfig to use esm instead of commonjs
5- run jest using ts-node/esm loader and passing vm-modules

node  --loader ts-node/esm --experimental-specifier-resolution=node --experimental-import-meta-resolve --experimental-vm-modules  node_modules/jest/bin/jest.js

Expected behavior

load jest.config.ts as esm

Actual behavior

Error: Jest: Failed to parse the TypeScript config file jest.config.ts
  Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/.pnpm/[email protected]/node_modules/strip-json-comments/index.js from jest.config.ts not supported.
Instead change the require of index.js in jest.config.ts to a dynamic import() which is available in all CommonJS modules.

note that this behavior touches loading jest.config.ts as esm allowing importing other esm modules, not about running spec files as esm

also note that my project already configured and runs as esm, I don't use commojs at all

Additional context

No response

Environment

System:
    OS: Linux 5.15 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (8) x64 AMD Ryzen 7 PRO 3700U w/ Radeon Vega Mobile Gfx
  Binaries:
    Node: 22.11.0 - ~/.nvm/versions/node/v22.11.0/bin/node
    npm: 10.9.0 - ~/.nvm/versions/node/v22.11.0/bin/npm
    pnpm: 9.14.2 - ~/.nvm/versions/node/v22.11.0/bin/pnpm
  npmPackages:
    jest: 29.7.0 => 29.7.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant