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
- Apply the autofix (or manually add): import { describe, it, expect } from '@jest/globals'; at the top of the file.
- Keep the import list in sync when adding new globals (jest, beforeEach, beforeAll, afterEach, afterAll).
- If your project deliberately relies on injected globals, disable the rule in .oxlintrc.json.
- 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
- Write new spec files with the @jest/globals import from the start.
- Run oxlint --fix once when adopting the rule to add imports across the suite.
- If you disable injected globals in jest.config.js, keep this lint rule on so missing imports surface at lint time, not runtime.
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
- {deprecated:?} has been deprecated in favor of {new:?}
- Every test should have either `{prefix}.assertions(<number o
- Could not find the reuseWorker option in ${path}
- Require a message for {matcher_name:?}.
- Modules should not be imported multiple times in the same fi
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/fdf1e745eb99fc89.
Report an issue: GitHub.