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.
npm install --save-dev babel-preset-jest// 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.
module.exports = {
presets: ['jest'],
};babel script.js --presets jestrequire('@babel/core').transform('code', {
presets: ['jest'],
});babel-preset-jest is automatically included when using babel-jest transformer:
// jest.config.js
module.exports = {
transform: {
'^.+\\.jsx?$': 'babel-jest',
},
};babel-preset-jest is a simple Babel preset factory that combines two essential Jest-related transformations:
The preset exports a factory function required by Babel's plugin architecture, which returns a configuration object containing the resolved plugin and preset paths.
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']
// }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';Automatically included via babel-preset-current-node-syntax. Enables current Node.js syntax features in Jest tests.
Supported features:
?.)??)Use in any Babel configuration file or method:
// babel.config.js
module.exports = {
presets: [
'jest',
// other presets...
],
};// .babelrc.js
module.exports = {
presets: ['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,
});/**
* 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;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.
require.resolve() for reliable plugin/preset path resolution