Skip to content

configs

configs holds partial Jest configs. Each preset spreads a few of them together. Spread them yourself to build a config no preset covers.

import { configs } from '@repobuddy/jest'
Member Kind Sets
configNode() function testEnvironment: 'node', testRegex, coveragePathIgnorePatterns
node config configNode()
nodeLoad config configNode(loadTestIdentifiers)
jsdom config testEnvironment: 'jsdom', testMatch
configSource() function roots, collectCoverageFrom
tsEsm config SWC transform and ESM settings for TypeScript
tsCjs config ts-jest and jest-esm-transformer-2 transforms
jsEsm config extensionsToTreatAsEsm for JavaScript
jsCjs config jest-esm-transformer-2 transform
defaultTestIdentifiers string[] identifiers in a normal run
loadTestIdentifiers string[] ['load']
knownTestIdentifiers string[] both lists

The values below were printed from the built package on Node.js 26.

Builds the Node.js environment and test file patterns for a set of identifiers.

function configNode(
identifiers?: string[],
minNodeVersion?: number,
): {
coveragePathIgnorePatterns: string[]
testEnvironment: string
testRegex: string[]
}
Parameter Type Default Effect
identifiers string[] defaultTestIdentifiers The <id> part of the test file names to run.
minNodeVersion number 14 The lowest major for <name>.<id>.node<major>.<ext> patterns.

Returns:

  • testEnvironment: 'node'.
  • testRegex: one pattern for <name>.<id>.<ext> and <name>.<id>.node.<ext>, then one pattern per major from minNodeVersion to the running Node.js major.
  • coveragePathIgnorePatterns: one pattern that matches every known identifier, whatever identifiers holds.

Behavior:

  • The version list uses process.version when configNode() runs.
  • minNodeVersion one above the running major gives no version patterns. A higher value throws RangeError: Invalid array length.
configs.configNode(['load'], 24)
// {
// coveragePathIgnorePatterns: [
// '\\.(spec|test|unit|accept|integrate|learning|system|perf|stress|load)(\\..*)?\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$',
// ],
// testEnvironment: 'node',
// testRegex: [
// '\\.(load)(\\.node)?\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$',
// '\\.(load)\\.node24\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$',
// '\\.(load)\\.node25\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$',
// '\\.(load)\\.node26\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$',
// ],
// }

Run normal tests and load tests together:

configs.configNode([...configs.defaultTestIdentifiers, ...configs.loadTestIdentifiers])

configNode() with its defaults. Every node preset spreads it.

const node: ReturnType<typeof configNode>
{
coveragePathIgnorePatterns: [
'\\.(spec|test|unit|accept|integrate|learning|system|perf|stress|load)(\\..*)?\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$',
],
testEnvironment: 'node',
testRegex: [
'\\.(spec|test|unit|accept|integrate|learning|system|perf|stress)(\\.node)?\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$',
'\\.(spec|test|unit|accept|integrate|learning|system|perf|stress)\\.node14\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$',
// one entry per Node.js major, from 15 up to the running major
],
}

configNode(loadTestIdentifiers). Runs *.load.* files only. A spec asserts that feature.load.ts matches and feature.spec.ts does not.

const nodeLoad: ReturnType<typeof configNode>
jest.load.config.mjs
import { configs } from '@repobuddy/jest'
export default {
preset: '@repobuddy/jest/presets/ts-esm',
...configs.nodeLoad,
}

See the load tests guide.

The jsdom environment and test file glob. Every jsdom preset spreads it.

const jsdom: { testEnvironment: string; testMatch: string[] }
{
testEnvironment: 'jsdom',
testMatch: [
'**/?*\\.(spec|test|unit|accept|integrate|learning|system|perf|stress)?(.jsdom).(js|jsx|cjs|mjs|ts|tsx|cts|mts)',
],
}

testEnvironment: 'jsdom' needs the jest-environment-jsdom package. The glob is fixed: it does not use defaultTestIdentifiers and has no load-test variant.

Points roots and collectCoverageFrom at one or more source folders.

function configSource(...dirs: string[]): {
collectCoverageFrom: string[]
roots: string[]
}
Parameter Type Default Effect
...dirs string[] detected Folders relative to <rootDir>. With none, the first of src, source, ts, js that exists is used, or src when none exists.

Behavior:

  • Detection checks the folders against the current working directory (path.resolve(dir)), not <rootDir>.
  • Coverage covers js, jsx, cjs, mjs, ts, tsx, cts, mts files and skips *.stories.* files.

From the spec:

configs.configSource('source')
// {
// collectCoverageFrom: [
// '<rootDir>/source/**/*.{js,jsx,cjs,mjs,ts,tsx,cts,mts}',
// '!<rootDir>/source/**/*.stories.*',
// ],
// roots: ['<rootDir>/source'],
// }

Several folders:

configs.configSource('lib', 'test')
// {
// collectCoverageFrom: [
// '<rootDir>/lib/**/*.{js,jsx,cjs,mjs,ts,tsx,cts,mts}',
// '!<rootDir>/lib/**/*.stories.*',
// '<rootDir>/test/**/*.{js,jsx,cjs,mjs,ts,tsx,cts,mts}',
// '!<rootDir>/test/**/*.stories.*',
// ],
// roots: ['<rootDir>/lib', '<rootDir>/test'],
// }

TypeScript as ESM, transformed by @swc/jest.

{
extensionsToTreatAsEsm: ['.ts', '.mts', '.tsx'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
'^.+\\.(js|jsx|cjs|mjs|ts|tsx|cts|mts)$': '@swc/jest',
},
}

Built from fields.knownExtensionsToTreatAsEsm.ts, fields.knownModuleNameMappers.tsEsm, and fields.knownTransforms.swc().

TypeScript as CommonJS, transformed by ts-jest, with ESM in node_modules transformed by jest-esm-transformer-2.

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

Built from fields.knownTransforms.tsJestCjs().

JavaScript as ESM. Treats .jsx as ESM and sets no transform.

{
extensionsToTreatAsEsm: ['.jsx'],
}

JavaScript as CommonJS, with ESM transformed by jest-esm-transformer-2.

{
transform: {
'\\.m?jsx?$': 'jest-esm-transformer-2',
},
transformIgnorePatterns: [],
}

The identifiers a normal run includes.

['spec', 'test', 'unit', 'accept', 'integrate', 'learning', 'system', 'perf', 'stress']

The identifiers for load tests. They are recognized but run only when you ask for them.

['load']

[...defaultTestIdentifiers, ...loadTestIdentifiers]. configNode() uses it for coveragePathIgnorePatterns.

['spec', 'test', 'unit', 'accept', 'integrate', 'learning', 'system', 'perf', 'stress', 'load']