noTestGlobals (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/noTestGlobals - This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Inspired from
jest/prefer-importing-jest-globals - Inspired from
vitest/prefer-importing-vitest-globals
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noTestGlobals": "error" } } }}Description
Section titled “Description”Disallow using test APIs as global variables.
Test runners such as Jest and Vitest can provide describe, it, expect, and their other test APIs as global variables.
Whether these global variables exist depends on the configuration of the test runner, and nothing in a test file shows where they come from.
Importing the test APIs works with any configuration.
The rule reports a test API that a file uses without importing or declaring it.
It ignores a test API that is only used as a type, or only as the operand of typeof, as in typeof jest === "undefined".
Biome reads the nearest package.json file to choose the test runner:
- If
vitestis listed as a dependency, the rule checks the global variables of Vitest, which are imported fromvitest. - Otherwise, the rule checks the global variables of Jest, which are imported from
@jest/globals.
The fix adds an import declaration.
It is unsafe because Biome can’t know whether the test runner loads the file as an ES module, which is the only kind of file that allows import declarations.
Biome doesn’t offer the fix in files that it treats as CommonJS, such as .cjs files.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”describe("sum", () => {});code-block.js:1:1 lint/nursery/noTestGlobals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The test API describe is used as a global variable.
> 1 │ describe("sum", () => {});
│ ^^^^^^^^
2 │
ℹ Whether this global variable exists depends on the configuration of the test runner. An import shows where the test API comes from and works with any configuration.
ℹ Import describe from @jest/globals instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Import describe from @jest/globals.
1 │ + import·{·describe·}·from·"@jest/globals";
1 2 │ describe("sum", () => {});
2 3 │
jest.useFakeTimers();code-block.js:1:1 lint/nursery/noTestGlobals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The test API jest is used as a global variable.
> 1 │ jest.useFakeTimers();
│ ^^^^
2 │
ℹ Whether this global variable exists depends on the configuration of the test runner. An import shows where the test API comes from and works with any configuration.
ℹ Import jest from @jest/globals instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Import jest from @jest/globals.
1 │ + import·{·jest·}·from·"@jest/globals";
1 2 │ jest.useFakeTimers();
2 3 │
import { describe, expect, it } from "@jest/globals";
describe("sum", () => { it("adds two numbers", () => { expect(1 + 2).toBe(3); });});
if (typeof jest !== "undefined") { console.log("Running inside Jest.");}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.