vitest/no-importing-vitest-globals

Disallow importing test, expect, vi and the other Vitest globals

fixable

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:

bunfig.toml
[lint.rules]
"vitest/no-importing-vitest-globals" = "error" # or "warn"

Examples#

example.ts1 problem
import { describe, expect, it } from "vitest";

describe("slugify", () => {
  it("lowercases the title", () => {
    expect(slugify("Hello")).toBe("hello");
  });
});

require is reported too:

example.js1 problem
const { expect, test } = require("vitest");

test("adds", () => {
  expect(1 + 1).toBe(2);
});
example.tsNo problems
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:

example.tsNo problems
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.