Skip to content

Customize a preset

A preset covers the common case. This guide changes one part of it and keeps the rest.

How Jest combines your config with a preset

Section titled “How Jest combines your config with a preset”

From jest-config 30:

Key Result
most keys yours replaces the preset’s
moduleNameMapper, transform merged: your patterns first, then the preset’s patterns you did not repeat
setupFiles, setupFilesAfterEnv concatenated: the preset’s entries, then yours

Jest uses the first transform pattern that matches a file.

Set the key in your config:

jest.config.mjs
export default {
preset: '@repobuddy/jest/presets/ts-esm',
testEnvironment: 'jsdom',
}

Your entries merge with the preset’s .js mapper:

jest.config.mjs
export default {
preset: '@repobuddy/jest/presets/ts-esm-watch',
moduleNameMapper: {
'^#utils$': '<rootDir>/src/utils.ts',
},
}
jest.config.mjs
export default {
preset: '@repobuddy/jest/presets/ts-esm',
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
}
jest.setup.ts
import { expect } from '@jest/globals'
import { toSatisfies } from '@repobuddy/jest/matchers'
expect.extend({ toSatisfies })

Detection picks the first of src, source, ts, js. To use another folder, or several, spread configs.configSource() with the names:

jest.config.mjs
import { configs } from '@repobuddy/jest'
export default {
preset: '@repobuddy/jest/presets/ts-esm',
...configs.configSource('lib', 'test'),
}

Use the same pattern as the preset so your entry replaces it. For ts-cjs, call knownTransforms.tsJestCjs() with your options:

jest.config.js
const { fields } = require('@repobuddy/jest')
module.exports = {
preset: '@repobuddy/jest/presets/ts-cjs',
transform: fields.knownTransforms.tsJestCjs({ tsconfig: 'tsconfig.test.json' }),
}

The dual-ts-jest fixture does the same with tsJestEsm() over jsdom-ts.

A different pattern does not remove the preset’s entry. With ts-esm plus knownTransforms.tsJestEsm(), jest --showConfig lists ts-jest for ^.+\.(ts|tsx|cts|mts)$ first and @swc/jest second. TypeScript files go to ts-jest, and JavaScript files still go to @swc/jest.

To drop the preset’s transformer completely, build the config from configs and fields without preset. This is ts-esm with ts-jest in place of SWC:

jest.config.mjs
import { configs, fields } from '@repobuddy/jest'
export default {
...configs.node,
...configs.configSource(),
extensionsToTreatAsEsm: fields.knownExtensionsToTreatAsEsm.ts,
moduleNameMapper: fields.knownModuleNameMappers.tsEsm,
...fields.defineTransform(fields.knownTransforms.tsJestEsm()),
}

ts-jest reads tsconfig.json, which must emit ESM (for example "module": "ESNext"). Run with NODE_OPTIONS=--experimental-vm-modules.

Print the merged config and check the keys you changed:

Terminal window
jest --showConfig