[Bug]: config:init should create ESM version of jest.config.js when type = module
pointlessrapunzel opened this issue · comments
Version
29.0.5
Steps to reproduce
- Create an npm project that has
"type": "module"
in itspackage.json
- Install
jest
,typescript
, andts-jest
- Run
config:init
script to generatejest.config.js
- Run
jest
yarn create vite --template vanilla-ts esm-config-ts-jest
cd esm-config-ts-jest
yarn add jest @types/jest ts-jest
yarn ts-jest config:init
yarn jest
Expected behavior
Jest reads the config file and runs normally if tests found.
Actual behavior
Jest can't read the config file because:
ReferenceError: module is not defined in ES module scope
Debug log
n/a
Additional context
It's not the biggest bug, and can easily be fixed manually, by changing the generated jest.config.js
to ESM. But it would be nice if it just worked out of the box.
It should be a simple fix in the cli/config/init.ts
script, I can make a PR if it's okay.
Environment
System:
OS: Linux 4.4 Ubuntu 20.04.5 LTS (Focal Fossa)
CPU: (12) x64 AMD Ryzen 5 5600H with Radeon Graphics
Binaries:
Node: 19.2.0 - ~/.nvm/versions/node/v19.2.0/bin/node
Yarn: 3.4.1 - ~/.nvm/versions/node/v19.2.0/bin/yarn
npm: 8.19.3 - ~/.nvm/versions/node/v19.2.0/bin/npm
npmPackages:
jest: ^29.4.1 => 29.4.1
PR is welcome :)
It's not the biggest bug, and can easily be fixed manually, by changing the generated jest.config.js to ESM.
It would have been nice to show how, for those of us who have this bug and are looking to fix it.
It's not the biggest bug, and can easily be fixed manually, by changing the generated jest.config.js to ESM.
It would have been nice to show how, for those of us who have this bug and are looking to fix it.
You can either change the config's extension to .cjs
,
or change
module.exports = {
// config
}
to ESM (any requires should also be changed to import
statements)
export default {
// same config
}