Linter · Rules
vitest/prefer-import-in-mock
Require import() instead of a string path in vi.mock() and vi.doMock()
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
Reported:
import { vi } from "vitest";
vi.mock("./api", async importOriginal => {
const api = await importOriginal();
return { ...api, fetchUser: vi.fn() };
});
Not reported:
import { vi } from "vitest";
vi.mock(import("./api"), async importOriginal => {
const api = await importOriginal();
return { ...api, fetchUser: vi.fn() };
});
Options
| Option | Default | What it does |
|---|---|---|
fixable | true | Set it to false to keep the report but leave the code alone with --fix. |
import { vi } from "vitest";
vi.doMock("./api");