ts-cjs
ts-cjs runs TypeScript tests as CommonJS in the node environment. ts-jest transforms TypeScript, and
jest-esm-transformer-2 transforms JavaScript, including ESM packages in node_modules.
module.exports = { preset: '@repobuddy/jest/presets/ts-cjs',}Options
Section titled “Options”None. A preset takes no options.
Effective config
Section titled “Effective config”Printed from the built preset with Node.js 26, in a directory that has a src folder:
{ transform: { '^.+\\.(ts|tsx|cts|mts)$': ['ts-jest', { isolatedModules: true }], '\\.m?jsx?$': 'jest-esm-transformer-2', }, transformIgnorePatterns: [], collectCoverageFrom: [ '<rootDir>/src/**/*.{js,jsx,cjs,mjs,ts,tsx,cts,mts}', '!<rootDir>/src/**/*.stories.*', ], roots: ['<rootDir>/src'], 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 ],}It is built from configs.tsCjs,
configs.configSource(), and
configs.node.
Packages it needs
Section titled “Packages it needs”extract.extractPackages() on the preset returns:
['jest-environment-node', 'jest-esm-transformer-2', 'ts-jest']ts-jest also needs typescript.
Behavior
Section titled “Behavior”transformIgnorePatterns: []makes Jest transform files innode_modulestoo.jest-esm-transformer-2turns ESM-only packages into CommonJS, soimport chalk from 'chalk'works with chalk 6 in thets-cjsfixture.- The
.js,.jsx,.mjsfiles of the project also go throughjest-esm-transformer-2. ts-jestreads the project’stsconfig.json. The preset passes onlyisolatedModules: true, which skips type checking.- With
ts-jest29.4.14, the run prints a warning that theisolatedModulesoption ofts-jestis deprecated in favor ofisolatedModules: trueintsconfig.json. The tests still run. - Test file names: see test file names.
ts-cjs-watch variant
Section titled “ts-cjs-watch variant”@repobuddy/jest/presets/ts-cjs-watch is ts-cjs plus the watch plugins of the watch
preset.
Examples
Section titled “Examples”module.exports = { preset: '@repobuddy/jest/presets/ts-cjs-watch',}Pass your own ts-jest options. Your transform pattern is the same as the preset’s, so it replaces that entry and
keeps jest-esm-transformer-2:
const { fields } = require('@repobuddy/jest')
module.exports = { preset: '@repobuddy/jest/presets/ts-cjs', transform: fields.knownTransforms.tsJestCjs({ tsconfig: 'tsconfig.test.json' }),}Related
Section titled “Related”ts: picksts-esmorts-cjsfrompackage.json.jsdom-ts-cjs: the same transforms in jsdom.- TypeScript with CommonJS guide