jest icon indicating copy to clipboard operation
jest copied to clipboard

[Feature]: Support loading jest.config.ts as ESM

Open luxaritas opened this issue 2 years ago • 4 comments

🚀 Feature Proposal

  • If "type": "module" is set in the package.json, jest.config.ts should be treated as ESM, not CJS
  • jest.config.mts and jest.config.cts should be looked for and loaded as ESM and CJS, respectively

Motivation

  • This is currently the behavior with jest.config.js, and so ESM vs CJS handling differs between JS and TS configs
  • I would like to be able to import ESM libraries in my TS config

Example

No response

Pitch

  • Config loading is handled by core
  • Module behavior between JS and TS should match

luxaritas avatar Aug 11 '22 20:08 luxaritas

Related: #11453

luxaritas avatar Aug 11 '22 20:08 luxaritas

PR welcome! Logic for config file resolution and reading is in https://github.com/facebook/jest/blob/1919ef13df544a968bd2d32e5d7b8a2a1c4b35d1/packages/jest-config/src/readConfigFileAndSetRootDir.ts

SimenB avatar Aug 12 '22 07:08 SimenB

That said, I don't think anything has changed since https://github.com/facebook/jest/issues/11453#issuecomment-1040424443 (i.e. you need a loader). But we should at least find cts as that is supposed to be converted to cjs

SimenB avatar Aug 12 '22 07:08 SimenB

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Sep 11 '22 08:09 github-actions[bot]

@SimenB as a first step, maybe let's get support for jest.config.cts, to avoid the extra ts-node.moduleTypes config for ts-node

Took a quick shot at a PR - does it need anything else? (other than fixing the tests)

  • https://github.com/facebook/jest/pull/14064

Edit: @DerTimonius opened a new PR based on mine, which also fixes the tests:

  • https://github.com/facebook/jest/pull/14070

karlhorky avatar Apr 12 '23 09:04 karlhorky

https://github.com/jestjs/jest/releases/tag/v30.0.0-alpha.3 for .cts support

SimenB avatar Feb 20 '24 11:02 SimenB