Registry /
testing / babel-plugin-jest-hoist
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
muslnode 18–226 runs
build_error
glibcnode 18–226 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();
});
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.
fixHoist 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.
fixInstall 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.
fixEnsure the test file is run with Jest and not another test runner.
Audit
Dependencies
@babel/traverserequiredUsed to traverse the AST and hoist jest.mock calls.
@babel/typesrequiredUsed to manipulate AST nodes when hoisting.