kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.

Home Page:https://kulshekhar.github.io/ts-jest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[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

  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
commented

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
}