Skip to main content
Version: Next

ESM Support

To use ts-jest with ESM support:

ESM presets

There are also 3 presets to work with ESM.

caution

If you are using custom transform config, please remove preset from your Jest config to avoid issues that Jest doesn't transform files correctly.

Examples

Manual configuration

// jest.config.js
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
// [...]
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
// '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
},
],
},
}

Use ESM presets

important

Starting from v28.0.0, ts-jest will gradually switch to esbuild/swc to transform ts to js. To make the transition smoothly, we introduce legacy presets as a fallback when the new codes don't work yet.

// jest.config.js
const { createDefaultEsmPreset } = require('ts-jest')

const defaultEsmPreset = createDefaultEsmPreset()

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
// [...]
...defaultEsmPreset,
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
}

Support .mts extension

To work with .mts extension, besides the requirement to run Jest and ts-jest in ESM mode, there are a few extra requirements to be met:

// tsconfig.spec.json
{
"compilerOptions": {
"module": "ESNext", // or ES2015/ES2020/ES2022
"target": "ESNext",
"esModuleInterop": true
}
}