jestjs/jest · critical · Error

Do not import `@jest/globals` outside of the Jest test…

Error message

Do not import `@jest/globals` outside of the Jest test environment

What it means

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.

Solutions

  1. Ensure `@jest/globals` is only imported in files that run under Jest's test runner.
  2. If a shared utility is imported by both app and test code, split it or use conditional imports.
  3. Configure bundlers/build tools to externalize or ignore `@jest/globals` in non-test builds.
  4. Run the file with `jest` or `jest-environment-node`, not bare `node`.

Example fix

// before: src/utils.ts (imported by both app and tests)
import { jest } from '@jest/globals';
export function mockHelper() { jest.fn(); }

// after: move to test-only file
test/utils/mockHelper.ts
import { jest } from '@jest/globals';
export function mockHelper() { jest.fn(); }
Defensive patterns

Strategy: validation

Validate before calling

// Ensure @jest/globals is only resolved within Jest
function assertJestRuntime() {
  if (typeof (globalThis as any).jest === 'undefined' && !process.env.JEST_WORKER_ID) {
    throw new Error('This file must only be imported in the Jest test environment');
  }
}

Type guard

function isJestEnvironment(): boolean {
  return typeof process !== 'undefined' && !!process.env.JEST_WORKER_ID;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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`.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/79a76bcbae2951ee. Report an issue: GitHub.

Appendix: source

Thrown at packages/jest-globals/src/index.ts:95

   */
  export type SpiedClass<T extends ClassLike> = JestSpiedClass<T>;
  /**
   * Constructs the type of a spied function.
   */
  export type SpiedFunction<T extends FunctionLike> = JestSpiedFunction<T>;
  /**
   * Constructs the type of a spied getter.
   */
  export type SpiedGetter<T> = JestSpiedGetter<T>;
  /**
   * Constructs the type of a spied setter.
   */
  export type SpiedSetter<T> = JestSpiedSetter<T>;
}

export {jest};

throw new Error(
  'Do not import `@jest/globals` outside of the Jest test environment',
);

View on GitHub (pinned to 8e6d128e4a)