Skip to content

js-cjs

js-cjs runs JavaScript tests as CommonJS in the node environment. jest-esm-transformer-2 transforms JavaScript, including ESM packages in node_modules.

jest.config.js
module.exports = {
preset: '@repobuddy/jest/presets/js-cjs',
}

None. A preset takes no options.

Printed from the built preset with Node.js 26, in a directory that has a src folder:

{
transform: {
'\\.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.jsCjs, configs.configSource(), and configs.node.

extract.extractPackages() on the preset returns:

['jest-environment-node', 'jest-esm-transformer-2']
  • transformIgnorePatterns: [] makes Jest transform files in node_modules. In the js-cjs fixture, require('chalk').default loads chalk 6, an ESM-only package.
  • The transform pattern matches .js, .jsx, .mjs, and .mjsx. .cjs files are not transformed.
  • Test file names: see test file names.

@repobuddy/jest/presets/js-cjs-watch is js-cjs plus the watch plugins of the watch preset.

The js-cjs fixture keeps its source in source/, which the preset detects:

jest.config.js
module.exports = {
preset: '@repobuddy/jest/presets/js-cjs-watch',
detectOpenHandles: true,
}