{"record":{"id":"79a76bcbae2951ee","repo":"jestjs/jest","slug":"do-not-import-jest-globals-outside-of-the-jest","errorCode":null,"errorMessage":"Do not import `@jest/globals` outside of the Jest test environment","messagePattern":"Do not import `@jest/globals` outside of the Jest test environment","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/jest-globals/src/index.ts","lineNumber":95,"sourceCode":"   */\n  export type SpiedClass<T extends ClassLike> = JestSpiedClass<T>;\n  /**\n   * Constructs the type of a spied function.\n   */\n  export type SpiedFunction<T extends FunctionLike> = JestSpiedFunction<T>;\n  /**\n   * Constructs the type of a spied getter.\n   */\n  export type SpiedGetter<T> = JestSpiedGetter<T>;\n  /**\n   * Constructs the type of a spied setter.\n   */\n  export type SpiedSetter<T> = JestSpiedSetter<T>;\n}\n\nexport {jest};\n\nthrow new Error(\n  'Do not import `@jest/globals` outside of the Jest test environment',\n);\n","sourceCodeStart":77,"sourceCodeEnd":98,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/jest-globals/src/index.ts#L77-L98","documentation":"This is a top-level `throw` statement at the bottom of the `@jest/globals` module. The module is designed to only be importable inside the Jest test runtime, where `jest-runtime` replaces this module with real implementations. If the module body executes verbatim (i.e. the file is loaded outside Jest's runtime), it throws immediately.","triggerScenarios":"Importing `@jest/globals` in a non-Jest context: a script run directly with Node, a file imported by a build tool (Webpack, Vite, esbuild), a Storybook config, or any non-test entry point that resolves the real `@jest/globals` package instead of the Jest-injected version.","commonSituations":"Accidentally importing from `@jest/globals` in a source (non-test) file that gets bundled. A shared utility file imported by both tests and application code. Storybook or other tooling resolving `@jest/globals` during its build. Running a test file with `node` directly instead of `jest`.","solutions":["Ensure `@jest/globals` is only imported in files that run under Jest's test runner.","If a shared utility is imported by both app and test code, split it or use conditional imports.","Configure bundlers/build tools to externalize or ignore `@jest/globals` in non-test builds.","Run the file with `jest` or `jest-environment-node`, not bare `node`."],"exampleFix":"// before: src/utils.ts (imported by both app and tests)\nimport { jest } from '@jest/globals';\nexport function mockHelper() { jest.fn(); }\n\n// after: move to test-only file\ntest/utils/mockHelper.ts\nimport { jest } from '@jest/globals';\nexport function mockHelper() { jest.fn(); }","handlingStrategy":"validation","validationCode":"// Ensure @jest/globals is only resolved within Jest\nfunction assertJestRuntime() {\n  if (typeof (globalThis as any).jest === 'undefined' && !process.env.JEST_WORKER_ID) {\n    throw new Error('This file must only be imported in the Jest test environment');\n  }\n}","typeGuard":"function isJestEnvironment(): boolean {\n  return typeof process !== 'undefined' && !!process.env.JEST_WORKER_ID;\n}","tryCatchPattern":null,"preventionTips":["Never import @jest/globals in application source files — only in test files.","Split shared utilities so test-only helpers are separate from app code.","Configure bundlers to externalize @jest/globals or mark it as dev-only.","Use import guards or conditional imports if a file must serve dual purposes."],"tags":["jest-globals","import-error","runtime","configuration"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}