- A minimal example
jest.config.ts - Tested works with edge ESM-only packages. Here is
node-fetchas example.
Adapt below files to your project folder
jest.config.tsconfig/tsconfig*json
If you accept --experimental-vm-modules flag, you can get full ESM jest, or I say hardcore ESM.
- external ESM-only packages -> jest
vmmodules
Pro: No hardcode.
Con: Both Node.js and jest official document still state that as experimental.
$ export NODE_OPTIONS="$NODE_OPTIONS --experimental-vm-modules"
$ jest$ env NODE_OPTIONS="$NODE_OPTIONS --experimental-vm-modules" jestIf you don't accept --experimental-vm-modules flag, you get ESM->CJS, or I say faking ESM. Hardcore ESM feature within test/config/ESMOnlyHard.spec.ts not work.
- external ESM-only packages -> transformer(ts-jest) -> CJS -> jest
vm
Pro: Stable
Con:
- You need to know all ESM-only package name under recursive dependency tree.
- Hard core ESM feature will fatal, or silent fail
# modify `HARDCODE_ESMONLY` pattern jest.config.ts
$ jest
[x] pnpm
[x] npm
[x] yarn
[x] yarn@berry
$ git clone <repo> .
...
$ bash ./test.sh
...
ALL PASS! 🎉 Alarm, although I don't think there is real world use case, exclude I'm testing config.
Lazy to extract and reproduce, brief:
If you have
- setted
--experimental-vm-modules - jest config
.projectslength >= 2 - jest config
.projectsincludejsdom - using
import('node-fetch')instead ofimport nf from 'node-fetch'
Good luck, bro!