vitest/prefer-importing-vitest-globals

Require describe, test, expect and vi to be imported from vitest

fixable

With globals: true in the Vitest config, describe, test, expect, vi and the hooks exist without an import. A file that relies on that fails with a ReferenceError as soon as the setting is off, and nothing in the file says where the names come from.

A file gets one report for all the names it uses.

--fix adds the import, or adds the names to the import from vitest that's already there.

Configure#

Off by default. To turn it on:

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

Examples#

example.ts1 problem
describe("cart", () => {
  test("starts empty", () => {
    expect(cart.items).toHaveLength(0);
  });
});
example.tsNo problems
import { describe, expect, test } from "vitest";

describe("cart", () => {
  test("starts empty", () => {
    expect(cart.items).toHaveLength(0);
  });
});

A call to one of these names is reported too if the name is imported from another package:

example.ts1 problem
import { test } from "node:test";

test("starts empty", () => {});

When not to use it#

Turn it off for files that use another test runner with the same names.