Registry / testing / babel-plugin-jest-hoist

babel-plugin-jest-hoist

JSON →
library30.3.0jsnpmunverified

A Babel plugin that hoists Jest mock and unmock calls to the top of the test module scope, ensuring they execute before any imports. Version 30.3.0 is the latest stable release, part of Jest v30 series. It works alongside babel-jest and jest-runtime to allow proper mocking with ES6 modules. Key differentiator: it is specifically designed for Jest's transformation pipeline and is automatically installed as a dependency of babel-jest in Jest projects. Does not require separate configuration unless using a custom Babel setup.

npm install babel-plugin-jest-hoist
INSTALL
IMPORT
SIG · BABEL-PLUGIN-JEST-
B
babel-plugin-jest-hoist
testingjavascriptv30.3.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (plugin)
module.exports = require('babel-plugin-jest-hoist');
import babelPluginJestHoist from 'babel-plugin-jest-hoist';
This plugin is a CommonJS module. In modern Jest with babel-jest, you never directly import it; it's used internally. Only use require() if configuring custom Babel.
createVisitor
const { createVisitor } = require('babel-plugin-jest-hoist');
import { createVisitor } from 'babel-plugin-jest-hoist';
createVisitor is an internal export. Not typically imported by users.

Shows how to configure the plugin manually in a custom Babel config and how jest.mock calls get hoisted.

// This plugin is automatically used by babel-jest. Manual setup is rarely needed. // If you have a custom Babel config (.babelrc or babel.config.js), add: module.exports = { plugins: ['babel-plugin-jest-hoist'] }; // Then in your test file, you can use: const { exec } = require('child_process'); jest.mock('child_process'); // hoisted to top test('mock works', () => { exec.mockImplementation(() => {}); expect(exec).toHaveBeenCalled(); });
Debug
Known issues
gotchajest.mock calls must be at the top level of the test file; they cannot be inside functions or conditionals.
fix
Move all jest.mock() calls to module scope, not inside describe/it blocks.
affects: >=0.0.0
gotchaThe plugin only hoists jest.mock(), jest.unmock(), and jest.enableAutomock() calls. Other jest methods like jest.doMock() are not hoisted.
fix
Use jest.mock() instead of jest.doMock() if hoisting is required.
affects: >=0.0.0
breakingJest 28 removed automatic hoisting for CJS modules; now hoisting only works with ESM or when using babel-jest.
fix
Ensure your project uses babel-jest or @jest/transform with the plugin.
affects: >=28.0.0
Errors
Common errors & fixes
babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
The factory function passed to jest.mock() tries to use variables not in scope at the top of the file.
fix
Hoist required variables or use `jest.mock('moduleName', () => ({}))` with inline modules.
Cannot find module 'babel-jest' from 'babel.config.js'
The Babel plugin is used without having babel-jest installed or configured.
fix
Install babel-jest: npm install --save-dev babel-jest @babel/core
TypeError: jest.mock is not a function
The test environment doesn't have jest defined, or jest.mock is not available in custom test frameworks.
fix
Ensure the test file is run with Jest and not another test runner.
Upgrade
Version history
30.3.0latest on npm
Audit
Dependencies
@babel/traverserequiredUsed to traverse the AST and hoist jest.mock calls.
@babel/typesrequiredUsed to manipulate AST nodes when hoisting.
Agent activity
25 hits · last 30 days
node
22
Amazon
1
OpenAI (training)
1
Resources
babel-plugin-jest-hoist — npm install babel-plugin-jest-hoist · libregistry