ESM Support
To use ts-jest
with ESM support:
- Check ESM Jest documentation.
- Enable useESM
true
forts-jest
config. - Include
.ts
in extensionsToTreatAsEsm Jest config option. - Ensure that
tsconfig
hasmodule
with value for ESM, e.g.ES2015
orES2020
etc...
ESM presets
There are also 3 presets to work with ESM.
Examples
Manual configuration
// jest.config.js
module.exports = {
// [...]
extensionsToTreatAsEsm: ['.ts'],
globals: {
'ts-jest': {
useESM: true,
},
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
}
// package.json
{
// [...]
"jest": {
"extensionsToTreatAsEsm": [".ts"],
"globals": {
"ts-jest": {
"useESM": true
}
},
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
}
}
}
Use ESM presets
// jest.config.js
module.exports = {
// [...]
preset: 'ts-jest/presets/default-esm', // or other ESM presets
globals: {
'ts-jest': {
useESM: true,
},
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
}
// package.json
{
// [...]
"jest": {
"preset": "ts-jest/presets/default-esm", // or other ESM presets,
"globals": {
"ts-jest": {
"useESM": true
}
},
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
}
}
}