vitest/no-importing-vitest-globals
Disallow importing test, expect, vi and the other Vitest globals
With globals: true in the Vitest config, describe, test, expect, vi and the hooks are available in every test file. Importing them again adds a line to each file that does nothing, and the files end up written in two styles.
The names are suite, test, it, describe, expect, expectTypeOf, assert, assertType, chai, vi, vitest, beforeAll, afterAll, beforeEach, afterEach, onTestFailed and onTestFinished.
--fix removes these names from the import, and removes the whole statement if nothing else is left.
Configure#
Off by default. To turn it on:
[lint.rules]
"vitest/no-importing-vitest-globals" = "error" # or "warn"Examples#
import { describeDo not import/require global functions from vitest., expect, it } from "vitest";
describe("slugify", () => {
it("lowercases the title", () => {
expect(slugify("Hello")).toBe("hello");
});
});require is reported too:
const { expectDo not import/require global functions from vitest., test } = require("vitest");
test("adds", () => {
expect(1 + 1).toBe(2);
});describe("slugify", () => {
it("lowercases the title", () => {
expect(slugify("Hello")).toBe("hello");
});
});An import of types, or of names that aren't globals, isn't reported:
import { type Mock, inject } from "vitest";
const save: Mock = vi.fn();
const port = inject("port");When not to use it#
Only use this rule with globals: true. Without it the names don't exist until you import them. vitest/prefer-importing-vitest-globals enforces the opposite style.