Skip to content
← Back to rules

vitest/prefer-import-in-mock Style

🛠️ An auto-fix is available for this rule.

What it does

This rule enforces using a dynamic import() in vi.mock() or vi.doMock(), which improves type information and IntelliSense for the mocked module.

Why is this bad?

A lack of type information and IntelliSense increases the risk of mismatches between the real module and its mock.

Examples

Examples of incorrect code for this rule:

js
vi.mock("./path/to/module");
vi.doMock("./path/to/module");

Examples of correct code for this rule:

js
vi.mock(import("./path/to/module"));
vi.doMock(import("./path/to/module"));

Configuration

This rule accepts a configuration object with the following properties:

fixable

type: boolean

default: true

Whether the rule should generate fixes or not.

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["vitest"],
  "rules": {
    "vitest/prefer-import-in-mock": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vitest"],
  rules: {
    "vitest/prefer-import-in-mock": "error",
  },
});
bash
oxlint --deny vitest/prefer-import-in-mock --vitest-plugin

Version

This rule was added in v1.49.0.

References