{"id":19088,"library":"babel-plugin-jest-hoist","title":"babel-plugin-jest-hoist","description":"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.","status":"active","version":"30.3.0","language":"javascript","source_language":"en","source_url":"https://github.com/jestjs/jest","tags":["javascript","typescript"],"install":[{"cmd":"npm install babel-plugin-jest-hoist","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-jest-hoist","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-jest-hoist","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used to traverse the AST and hoist jest.mock calls.","package":"@babel/traverse","optional":false},{"reason":"Used to manipulate AST nodes when hoisting.","package":"@babel/types","optional":false}],"imports":[{"note":"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.","wrong":"import babelPluginJestHoist from 'babel-plugin-jest-hoist';","symbol":"default (plugin)","correct":"module.exports = require('babel-plugin-jest-hoist');"},{"note":"createVisitor is an internal export. Not typically imported by users.","wrong":"import { createVisitor } from 'babel-plugin-jest-hoist';","symbol":"createVisitor","correct":"const { createVisitor } = require('babel-plugin-jest-hoist');"}],"quickstart":{"code":"// This plugin is automatically used by babel-jest. Manual setup is rarely needed.\n// If you have a custom Babel config (.babelrc or babel.config.js), add:\nmodule.exports = {\n  plugins: ['babel-plugin-jest-hoist']\n};\n\n// Then in your test file, you can use:\nconst { exec } = require('child_process');\njest.mock('child_process'); // hoisted to top\n\ntest('mock works', () => {\n  exec.mockImplementation(() => {});\n  expect(exec).toHaveBeenCalled();\n});","lang":"javascript","description":"Shows how to configure the plugin manually in a custom Babel config and how jest.mock calls get hoisted."},"warnings":[{"fix":"Move all jest.mock() calls to module scope, not inside describe/it blocks.","message":"jest.mock calls must be at the top level of the test file; they cannot be inside functions or conditionals.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use jest.mock() instead of jest.doMock() if hoisting is required.","message":"The plugin only hoists jest.mock(), jest.unmock(), and jest.enableAutomock() calls. Other jest methods like jest.doMock() are not hoisted.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Ensure your project uses babel-jest or @jest/transform with the plugin.","message":"Jest 28 removed automatic hoisting for CJS modules; now hoisting only works with ESM or when using babel-jest.","severity":"breaking","affected_versions":">=28.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Hoist required variables or use `jest.mock('moduleName', () => ({}))` with inline modules.","cause":"The factory function passed to jest.mock() tries to use variables not in scope at the top of the file.","error":"babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables."},{"fix":"Install babel-jest: npm install --save-dev babel-jest @babel/core","cause":"The Babel plugin is used without having babel-jest installed or configured.","error":"Cannot find module 'babel-jest' from 'babel.config.js'"},{"fix":"Ensure the test file is run with Jest and not another test runner.","cause":"The test environment doesn't have jest defined, or jest.mock is not available in custom test frameworks.","error":"TypeError: jest.mock is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}