{"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":"error","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/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-globals/src/index.ts#L77-L98","documentation":"`@jest/globals` is a thin re-export module whose source ends in a top-level `throw new Error(...)` (index.ts:95). The actual exports are injected at runtime by jest-runtime, which replaces this module's namespace. If the module is evaluated outside a Jest worker (e.g. imported by a build tool, a Node script, or a non-Jest test runner), the throw fires and import fails by design.","triggerScenarios":"`import {expect, jest} from '@jest/globals'` executed by anything other than jest-runtime: a Node REPL script, webpack/esbuild/rollup bundling the file, vitest/mocha loading the file, or a storybook preview that imports the test file.","commonSituations":"Sharing assertion helpers between Jest and a bundler; a storybook/addon that imports a file which transitively imports `@jest/globals`; running a migration spike with another runner; a config file accidentally imports a test file.","solutions":["Only import `@jest/globals` from files that Jest executes; for shared helpers use the globals `jest`/`expect` injected onto the runtime instead.","Conditionally import only when `process.env.JEST_WORKER_ID` is set.","Split shared logic into a framework-agnostic module with no `@jest/globals` import.","For bundlers, mark `@jest/globals` as external or exclude test files from the build."],"exampleFix":"// before\nimport {expect} from '@jest/globals'; // imported by a Storybook preview\nexport function assertShape(x) { expect(x).toMatchObject(...); }\n\n// after\nexport function assertShape(x) {\n  if (!process.env.JEST_WORKER_ID) return;\n  expect(x).toMatchObject(...); // uses the injected global\n}","handlingStrategy":"validation","validationCode":"if (!process.env.JEST_WORKER_ID) {\n  throw new Error('this module may only be imported inside a Jest worker');\n}","typeGuard":"const isJestContext = () => Boolean(process.env.JEST_WORKER_ID);","tryCatchPattern":null,"preventionTips":["Keep @jest/globals imports inside files only Jest runs.","Mark @jest/globals external in bundler configs.","Guard shared helpers with process.env.JEST_WORKER_ID."],"tags":["jest-globals","imports","runtime","bundling"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}