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.
Override a key
Section titled “Override a key”Set the key in your config:
export default { preset: '@repobuddy/jest/presets/ts-esm', testEnvironment: 'jsdom',}Add a module mapper
Section titled “Add a module mapper”Your entries merge with the preset’s .js mapper:
export default { preset: '@repobuddy/jest/presets/ts-esm-watch', moduleNameMapper: { '^#utils$': '<rootDir>/src/utils.ts', },}Register a matcher for every test
Section titled “Register a matcher for every test”export default { preset: '@repobuddy/jest/presets/ts-esm', setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],}import { expect } from '@jest/globals'import { toSatisfies } from '@repobuddy/jest/matchers'
expect.extend({ toSatisfies })Change the source folder
Section titled “Change the source folder”Detection picks the first of src, source, ts, js. To use another folder, or several, spread
configs.configSource() with the names:
import { configs } from '@repobuddy/jest'
export default { preset: '@repobuddy/jest/presets/ts-esm', ...configs.configSource('lib', 'test'),}Pass options to the transformer
Section titled “Pass options to the transformer”Use the same pattern as the preset so your entry replaces it. For ts-cjs, call
knownTransforms.tsJestCjs() with your options:
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.
Replace the transformer
Section titled “Replace the transformer”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:
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.
Verify
Section titled “Verify”Print the merged config and check the keys you changed:
jest --showConfigRelated
Section titled “Related”- Presets
presetsnamespace: read a preset’s values in code.