Skip to content
← Back to rules

vitest/prefer-import-in-mock Style

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

作用

此规则强制在 vi.mock()vi.doMock() 中使用动态 import(),这可以提升被模拟模块的类型信息和 IntelliSense。

为什么这不好?

缺少类型信息和 IntelliSense 会增加真实模块与其模拟版本不匹配的风险。

示例

以下是此规则的错误代码示例:

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

以下是此规则的正确代码示例:

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

配置

此规则接受一个包含以下属性的配置对象:

fixable

类型:boolean

默认值:true

表示该规则是否应生成修复。

如何使用

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

版本

此规则在 v1.49.0 中添加。

参考资料