Skip to content

fields

fields holds values for one Jest config field each. The known* members are ready-made values. The define* functions wrap a value in its field name.

import { fields } from '@repobuddy/jest'
Member Field Kind
knownTestEnvironments testEnvironment object
knownExtensionsToTreatAsEsm extensionsToTreatAsEsm object
knownModuleNameMappers moduleNameMapper object
defineModuleNameMappers() moduleNameMapper function
knownTransforms.swc() transform function
knownTransforms.tsJest() transform function
knownTransforms.tsJestCjs() transform function
knownTransforms.tsJestEsm() transform function
knownTransforms.esmPackages() transform function
defineTransform() transform function
knownWatchPlugins watchPlugins object of functions
watchPlugins watchPlugins array
defineWatchPlugins() watchPlugins function

The package also exports the types Transform, Transform.TsJestOptions, Transform.TransformerConfig, WatchPlugins, WatchPlugins.BaseOptions, WatchPlugins.SuspendOptions, and WatchPlugins.ToggleConfig from this namespace.

{ jsdom: 'jsdom', node: 'node' }
{
js: ['.jsx'],
ts: ['.ts', '.mts', '.tsx'],
}

configs.tsEsm uses ts. configs.jsEsm uses js.

Key Value Use
tsEsm { '^(\\.{1,2}/.*)\\.js$': '$1' } Map a relative ./x.js import to ./x, so TypeScript ESM source resolves.
cssAll { '\\.module\\.css$': 'identity-obj-proxy', '.+\\.(css|styl|less|sass|scss)$': 'identity-obj-proxy' } Map style imports to identity-obj-proxy. Needs the identity-obj-proxy package.

No preset uses cssAll. Add it yourself (see DOM tests with jsdom).

Merges mapper objects into one.

function defineModuleNameMappers(...entries: Array<Record<string, string>>): Record<string, string>
Parameter Type Default Effect
...entries Record<string, string>[] none Mapper objects. A later entry wins on the same key.

Returns a new object. It does not wrap the result in { moduleNameMapper }.

From the spec:

fields.defineModuleNameMappers(fields.knownModuleNameMappers.tsEsm, fields.knownModuleNameMappers.cssAll)
// {
// '^(\\.{1,2}/.*)\\.js$': '$1',
// '\\.module\\.css$': 'identity-obj-proxy',
// '.+\\.(css|styl|less|sass|scss)$': 'identity-obj-proxy',
// }

@swc/jest for every JavaScript and TypeScript extension.

swc(options?: Record<string, unknown>): Record<string, string | [string, Record<string, unknown>]>
Parameter Type Default Effect
options Record<string, unknown> none Passed to @swc/jest. Without it, the entry is the bare name.
fields.knownTransforms.swc()
// { '^.+\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$': '@swc/jest' }
fields.knownTransforms.swc({ jsc: { target: 'es2022' } })
// { '^.+\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$': ['@swc/jest', { jsc: { target: 'es2022' } }] }

ts-jest for .ts, .tsx, .cts, .mts, with the options you pass and no defaults.

tsJest(options: Transform.TsJestOptions): Record<string, string | [string, Record<string, unknown>]>
Parameter Type Default Effect
options Transform.TsJestOptions required Passed to ts-jest.

Transform.TsJestOptions types these ts-jest options: compiler, tsconfig, isolatedModules, astTransformers, diagnostics (boolean or { ignoreCodes }), babelConfig, stringifyContentPathRegex, useESM.

fields.knownTransforms.tsJest({ tsconfig: 't.json' })
// { '^.+\\.(ts|tsx|cts|mts)$': ['ts-jest', { tsconfig: 't.json' }] }

ts-jest with isolatedModules: true, plus esmPackages(). The ts-cjs presets use it.

tsJestCjs(options?: Transform.TsJestOptions): Record<string, string | [string, Record<string, unknown>]>
Parameter Type Default Effect
options Transform.TsJestOptions none Merged over { isolatedModules: true }.

From the spec:

fields.knownTransforms.tsJestCjs()
// {
// '^.+\\.(ts|tsx|cts|mts)$': ['ts-jest', { isolatedModules: true }],
// '\\.m?jsx?$': 'jest-esm-transformer-2',
// }

Your options keep the default unless you set it:

fields.knownTransforms.tsJestCjs({ isolatedModules: false })
// {
// '^.+\\.(ts|tsx|cts|mts)$': ['ts-jest', { isolatedModules: false }],
// '\\.m?jsx?$': 'jest-esm-transformer-2',
// }

ts-jest with isolatedModules: true and useESM: true. No preset uses it. It is the way to run TypeScript ESM through ts-jest instead of SWC.

tsJestEsm(options?: Transform.TsJestOptions): Record<string, string | [string, Record<string, unknown>]>
Parameter Type Default Effect
options Transform.TsJestOptions none Merged over { isolatedModules: true, useESM: true }.

From the spec:

fields.knownTransforms.tsJestEsm()
// { '^.+\\.(ts|tsx|cts|mts)$': ['ts-jest', { isolatedModules: true, useESM: true }] }

jest-esm-transformer-2 for .js, .jsx, .mjs, .mjsx. Pair it with transformIgnorePatterns: [] so it reaches node_modules.

esmPackages(): Record<string, string>

Parameters: none.

fields.knownTransforms.esmPackages()
// { '\\.m?jsx?$': 'jest-esm-transformer-2' }

tsJestCjs() and tsJestEsm() call this.tsJest(). Call them on the object. A destructured call fails:

const { tsJestCjs } = fields.knownTransforms
tsJestCjs()
// TypeError: Cannot read properties of undefined (reading 'tsJest')

Wraps a transform map in { transform }, typed as Jest’s transform field.

function defineTransform(transform: Transform): { transform: Transform }
Parameter Type Default Effect
transform Transform (Jest’s Config['transform']) required The transform map.
fields.defineTransform(fields.knownTransforms.swc())
// { transform: { '^.+\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$': '@swc/jest' } }

Functions that return one watchPlugins entry. Each returns the bare name without options, or [name, options] with options.

Function Package entry Options type
suspend(options?) jest-watch-suspend WatchPlugins.SuspendOptions: key, prompt, 'suspend-on-start'
toggleConfig(options?) jest-watch-toggle-config WatchPlugins.ToggleConfig: key, prompt, setting (required in the type)
typeaheadFilename(options?) jest-watch-typeahead/filename WatchPlugins.BaseOptions: key, prompt
typeaheadTestname(options?) jest-watch-typeahead/testname WatchPlugins.BaseOptions: key, prompt

From the spec:

fields.knownWatchPlugins.suspend({ 'suspend-on-start': true })
// ['jest-watch-suspend', { 'suspend-on-start': true }]
fields.knownWatchPlugins.toggleConfig({ setting: 'bail' })
// ['jest-watch-toggle-config', { setting: 'bail' }]

The default plugin list. The watch preset and every -watch preset use it.

[
'jest-watch-suspend',
['jest-watch-toggle-config', { setting: 'collectCoverage' }],
['jest-watch-toggle-config', { setting: 'verbose' }],
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
]

Wraps a plugin list in { watchPlugins }.

function defineWatchPlugins(config?: WatchPlugins): { watchPlugins: WatchPlugins }
Parameter Type Default Effect
config WatchPlugins (Jest’s Config['watchPlugins']) watchPlugins The plugin list.
fields.defineWatchPlugins([
fields.knownWatchPlugins.suspend({ 'suspend-on-start': true }),
fields.knownWatchPlugins.typeaheadFilename({ key: 'f' }),
])
// {
// watchPlugins: [
// ['jest-watch-suspend', { 'suspend-on-start': true }],
// ['jest-watch-typeahead/filename', { key: 'f' }],
// ],
// }