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]: config:init should create ESM version of jest.config.js when type = module #4012

Open
pointlessrapunzel opened this issue Feb 1, 2023 · 3 comments · May be fixed by #4210
Open

[Bug]: config:init should create ESM version of jest.config.js when type = module #4012

pointlessrapunzel opened this issue Feb 1, 2023 · 3 comments · May be fixed by #4210
Labels
🚀 Feature Request new suggested feature

Comments

@pointlessrapunzel
Copy link

Version

29.0.5

Steps to reproduce

  1. Create an npm project that has "type": "module" in its package.json
  2. Install jest, typescript, and ts-jest
  3. Run config:init script to generate jest.config.js
  4. 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
@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 1, 2023

PR is welcome :)

@Pamplemousse
Copy link

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.

@pointlessrapunzel
Copy link
Author

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
}

@ahnpnl ahnpnl added 🚀 Feature Request new suggested feature and removed Bug Report Needs Triage labels Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 Feature Request new suggested feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants