ECMAScript Modules
With the warnings out of the way, this is how you activate ESM support in your tests.
Execute
node
with--experimental-vm-modules
, e.g.node --experimental-vm-modules node_modules/jest/bin/jest.js
or etc..On Windows, you can use cross-env to be able to set environment variables.
If you use Yarn, you can use
yarn node --experimental-vm-modules $(yarn bin jest)
. This command will also work if you use .If you want to treat other file extensions (such as
.jsx
or.ts
) as ESM, please use the extensionsToTreatAsEsm option.
Most of the differences are explained in Node’s documentation, but in addition to the things mentioned there, Jest injects a special variable into all executed files - the . To access this object in ESM, you need to import it from the @jest/globals
module. To access this object in ESM, you need to import it from the @jest/globals
module or use .
Please note that we currently don’t support jest.mock
in a clean way in ESM, but that is something we intend to add proper support for in the future. Follow this issue for updates. Follow for updates.