Skip to content

js-esm

js-esm runs JavaScript tests as native ES modules in the node environment. It sets no transform.

jest.config.mjs
export default {
preset: '@repobuddy/jest/presets/js-esm',
}
Terminal window
NODE_OPTIONS=--experimental-vm-modules jest

None. A preset takes no options.

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

{
extensionsToTreatAsEsm: ['.jsx'],
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.jsEsm, configs.configSource(), and configs.node.

extract.extractPackages() on the preset returns:

['jest-environment-node']

That package is a dependency of Jest, so jest is the only install.

  • The preset sets no transform, so Jest’s default applies. With Jest 30, jest --showConfig in the js-esm fixture shows babel-jest for \.[jt]sx?$.
  • .js files load as ESM because package.json has "type": "module". extensionsToTreatAsEsm adds .jsx.
  • Without --experimental-vm-modules, ESM test files fail with SyntaxError: Cannot use import statement outside a module.
  • Test file names: see test file names.

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

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

jest.config.mjs
export default {
preset: '@repobuddy/jest/presets/js-esm',
detectOpenHandles: true,
}