jest/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": ["jest"],
"rules": {
"jest/no-mocks-import": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["jest"],
rules: {
"jest/no-mocks-import": "error",
},
});bash
oxlint --deny jest/no-mocks-import --jest-plugin版本
此规则在 v0.0.13 中添加。
