or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-preset-jest

Babel preset for all Jest plugins that configures Jest-specific transformations automatically

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-preset-jest@30.0.x

To install, run

npx @tessl/cli install tessl/npm-babel-preset-jest@30.0.0

index.mddocs/

babel-preset-jest

babel-preset-jest is a Babel preset that provides all Jest-related transformations for test files. It automatically includes babel-plugin-jest-hoist for hoisting Jest mock calls and babel-preset-current-node-syntax for supporting current Node.js syntax features. This preset is automatically included when using babel-jest and simplifies the configuration process for Jest users.

Package Information

  • Package Name: babel-preset-jest
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install --save-dev babel-preset-jest

Core Imports

// Direct import of the preset factory function
const babelPresetJest = require('babel-preset-jest');

Note: This package exports CommonJS only. ES module imports are not supported.

Basic Usage

Via babel.config.js (Recommended)

module.exports = {
  presets: ['jest'],
};

Via CLI

babel script.js --presets jest

Via Node API

require('@babel/core').transform('code', {
  presets: ['jest'],
});

Automatic Usage with babel-jest

babel-preset-jest is automatically included when using babel-jest transformer:

// jest.config.js
module.exports = {
  transform: {
    '^.+\\.jsx?$': 'babel-jest',
  },
};

Architecture

babel-preset-jest is a simple Babel preset factory that combines two essential Jest-related transformations:

  • babel-plugin-jest-hoist: Moves Jest mock calls above import statements to ensure proper execution order
  • babel-preset-current-node-syntax: Enables current Node.js syntax features in test files

The preset exports a factory function required by Babel's plugin architecture, which returns a configuration object containing the resolved plugin and preset paths.

Capabilities

Preset Factory Function

The main export that creates and returns the Babel preset configuration for Jest transformations.

/**
 * Creates a Babel preset configuration for Jest transformations
 * @returns {BabelPresetConfig} Preset configuration object with resolved plugin and preset paths
 */
function babelPresetJest(): BabelPresetConfig;

interface BabelPresetConfig {
  /** Array of resolved plugin paths */
  plugins: string[];
  /** Array of resolved preset paths */
  presets: string[];
}

Usage Examples:

// Getting the preset configuration
const presetFactory = require('babel-preset-jest');
const presetConfig = presetFactory();

// presetConfig contains resolved paths:
// {
//   plugins: ['/path/to/node_modules/babel-plugin-jest-hoist/index.js'],
//   presets: ['/path/to/node_modules/babel-preset-current-node-syntax/index.js']
// }

Mock Call Hoisting

Automatically included via babel-plugin-jest-hoist. Hoists Jest mock calls above import statements.

Transforms these Jest calls:

  • jest.mock()
  • jest.unmock()
  • jest.disableAutomock()
  • jest.enableAutomock()

Example transformation:

// Before transformation
import { someFunction } from './module';
jest.mock('./anotherModule');

// After transformation
jest.mock('./anotherModule');
import { someFunction } from './module';

Node.js Syntax Support

Automatically included via babel-preset-current-node-syntax. Enables current Node.js syntax features in Jest tests.

Supported features:

  • Optional chaining (?.)
  • Nullish coalescing (??)
  • Dynamic imports
  • Other current Node.js syntax features

Configuration

Standard Babel Configuration

Use in any Babel configuration file or method:

// babel.config.js
module.exports = {
  presets: [
    'jest',
    // other presets...
  ],
};
// .babelrc.js
module.exports = {
  presets: ['jest'],
};

Integration with babel-jest

The preset is automatically applied when using babel-jest unless explicitly excluded:

// To exclude the preset (not recommended)
const babelJest = require('babel-jest').createTransformer({
  excludeJestPreset: true,
});

Environment Requirements

  • Node.js: ^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0
  • Peer Dependencies: @babel/core ^7.11.0

Types

/**
 * Babel preset configuration returned by the factory function
 */
interface BabelPresetConfig {
  /** Array of resolved Babel plugin paths */
  plugins: string[];
  /** Array of resolved Babel preset paths */
  presets: string[];
}

/**
 * The main preset factory function type
 */
type PresetFactory = () => BabelPresetConfig;

Error Handling

The preset factory function does not throw errors under normal circumstances. All plugin and preset resolutions are handled internally using require.resolve(). If dependencies are missing, Node.js will throw standard module resolution errors.

Integration Notes

  • Automatic Integration: When using babel-jest, this preset is automatically included
  • Manual Override: Can be explicitly configured in Babel configuration files
  • Plugin Resolution: Uses require.resolve() for reliable plugin/preset path resolution
  • Zero Configuration: Works out of the box with no additional setup required