oxc-project/oxc · warning · OxcDiagnostic

Import the following Jest functions from `@jest/globals`: {g

Error message

Import the following Jest functions from `@jest/globals`: {globals}

What it means

This is oxlint's 'jest/prefer-importing-jest-globals' diagnostic. It collects all Jest globals used in a file (describe, it, expect, jest, beforeEach, ...) that are not already imported and reports them in one message listing the missing names, with a fixer that adds the import from @jest/globals. Importing explicitly makes the dependency on Jest visible and supports environments where globals are disabled (injectGlobals: false).

Source

Thrown at crates/oxc_linter/src/rules/jest/prefer_importing_jest_globals.rs:25

use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use rustc_hash::FxHashSet;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    context::LintContext,
    fixer::{RuleFix, RuleFixer},
    module_record::ImportImportName,
    rule::{DefaultRuleConfig, Rule},
    utils::{
        JestFnKind, JestGeneralFnKind, ParsedJestFnCallNew, collect_possible_jest_call_node,
        parse_jest_fn_call,
    },
};

fn prefer_importing_jest_globals_diagnostic(span: Span, globals: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(
        "Import the following Jest functions from `@jest/globals`: {globals}"
    ))
    .with_label(span)
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct PreferImportingJestGlobals(Box<PreferImportingJestGlobalsConfig>);

impl std::ops::Deref for PreferImportingJestGlobals {
    type Target = PreferImportingJestGlobalsConfig;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct PreferImportingJestGlobalsConfig {

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Apply the autofix (or manually add): import { describe, it, expect } from '@jest/globals'; at the top of the file.
  2. Keep the import list in sync when adding new globals (jest, beforeEach, beforeAll, afterEach, afterAll).
  3. If your project deliberately relies on injected globals, disable the rule in .oxlintrc.json.
  4. Combine with jest.config.js injectGlobals settings so runtime and lint expectations match.

Example fix

// before
describe('math', () => {
  it('adds', () => {
    expect(1 + 1).toBe(2);
  });
});

// after
import { describe, it, expect } from '@jest/globals';

describe('math', () => {
  it('adds', () => {
    expect(1 + 1).toBe(2);
  });
});
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Enable the rule and lint a file that calls describe/it/expect (recognized via parse_jest_fn_call) without a matching named import from @jest/globals for every used global. The diagnostic message lists the specific globals, e.g. "Import the following Jest functions from `@jest/globals`: describe, it, expect".

Common situations: Projects migrating to explicit imports (or enabling injectGlobals: false in jest.config.js) hit this across every existing spec file. Also common when moving from Create React App defaults, where globals are injected, to ESM-style setups where globals resolve ambiguously or collide with user-defined names.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/fdf1e745eb99fc89. Report an issue: GitHub.