Skip to content
← Back to rules

vitest/no-mocks-import Style

它的作用

此规则会报告从包含 __mocks__ 组件的路径进行的导入。

为什么这很糟糕?

手动从 __mocks__ 目录导入 mock 可能会导致意外行为,并破坏 Jest 的自动 mock 机制。Jest 的设计是:当调用 jest.mock() 时,会自动解析并使用来自 __mocks__ 目录的 mock。直接从这些目录导入会绕过 Jest 的模块解析系统,并可能导致测试环境与生产环境之间的不一致。

示例

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

ts
import thing from "./__mocks__/index";
require("./__mocks__/index");

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

ts
import thing from "thing";
require("thing");

如何使用

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

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

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

版本

此规则于 v0.0.13 中添加。

参考资料