Skip to content

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

@pointlessrapunzel

Description

@pointlessrapunzel

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions