vitest/prefer-import-in-mock

Require import() instead of a string path in vi.mock() and vi.doMock()

fixable

TypeScript doesn't check a path that's written as a string. With vi.mock(import("./api")) it knows which module is mocked, so a wrong path is a type error and importOriginal() in the factory has the module's type.

--fix wraps the string in import().

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"vitest/prefer-import-in-mock" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"vitest/prefer-import-in-mock" = ["error", {
  fixable = true,
}]

Examples#

example.ts1 problem
import { vi } from "vitest";

vi.mock("./api", async importOriginal => {
  const api = await importOriginal();
  return { ...api, fetchUser: vi.fn() };
});
example.tsNo problems
import { vi } from "vitest";

vi.mock(import("./api"), async importOriginal => {
  const api = await importOriginal();
  return { ...api, fetchUser: vi.fn() };
});

Options#

OptionDefaultWhat it does
fixabletrueSet it to false to keep the report but leave the code alone with --fix.
example.tswith { "fixable": false }1 problem
import { vi } from "vitest";

vi.doMock("./api");