jest/no-mocks-import 风格
它的作用
此规则报告从包含 mocks 组件路径的导入。
为什么这是个问题?
手动从 __mocks__ 目录导入模拟文件可能导致意外行为,并破坏 Jest 的自动模拟系统。Jest 的设计初衷是当调用 jest.mock() 时,能够自动解析并使用 __mocks__ 目录中的模拟文件。直接从这些目录导入会绕过 Jest 的模块解析机制,从而在测试环境和生产环境之间造成不一致。
示例
此规则的错误代码示例:
ts
import thing from "./__mocks__/index";
require("./__mocks__/index");此规则的正确代码示例:
ts
import thing from "thing";
require("thing");该规则与 eslint-plugin-vitest 兼容。要使用它,请将以下配置添加到您的 .oxlintrc.json 文件中:
json
{
"rules": {
"vitest/no-mocks-import": "error"
}
}如何使用
要通过配置文件或 CLI 启用此规则,可以使用:
json
{
"plugins": ["jest"],
"rules": {
"jest/no-mocks-import": "error"
}
}bash
oxlint --deny jest/no-mocks-import --jest-plugin