vitest/require-mock-type-parameters Correctness
What it does
Enforces the use of type parameters on vi.fn(), and optionally on vi.importActual() and vi.importMock().
By default, only vi.fn() is checked. Set checkImportFunctions to true to also check vi.importActual() and vi.importMock().
Why is this bad?
Without explicit type parameters, vi.fn() creates a mock typed as (...args: any[]) => any. This disables type checking between the mock and the real implementation, which can lead to two problems:
- tests that fail due to incorrect mock usage when they should pass
- or worse, tests that pass while the mock silently diverges from the actual runtime behavior.
Examples
Examples of incorrect code for this rule configured as { "checkImportFunctions": false }:
import { vi } from "vitest";
test("foo", () => {
const myMockedFn = vi.fn();
});Examples of incorrect code for this rule configured as { "checkImportFunctions": true }:
import { vi } from "vitest";
vi.mock("./example.js", async () => {
const originalModule = await vi.importActual("./example.js");
return { ...originalModule };
});
const fs = await vi.importMock("fs");Examples of correct code for this rule configured as { "checkImportFunctions": false }:
import { vi } from "vitest";
test("foo", () => {
const myMockedFnOne = vi.fn<(arg1: string, arg2: boolean) => number>();
const myMockedFnTwo = vi.fn<() => void>();
const myMockedFnThree = vi.fn<any>();
});Examples of correct code for this rule configured as { "checkImportFunctions": true }:
import { vi } from "vitest";
vi.mock("./example.js", async () => {
const originalModule = await vi.importActual<any>("./example.js");
return { ...originalModule };
});
const fs = await vi.importMock<any>("fs");Configuration
This rule accepts a configuration object with the following properties:
checkImportFunctions
type: boolean
default: false
Also require type parameters for importActual and importMock.
How to use
To enable this rule using the config file or in the CLI, you can use:
{
"plugins": ["vitest"],
"rules": {
"vitest/require-mock-type-parameters": "error"
}
}import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vitest"],
rules: {
"vitest/require-mock-type-parameters": "error",
},
});oxlint --deny vitest/require-mock-type-parameters --vitest-pluginVersion
This rule was added in v1.58.0.
