jest/prefer-importing-jest-globals 样式
作用
更倾向于从 @jest/globals 导入 Jest 全局变量(describe、test、expect 等), 而不是依赖环境中的全局变量。
为什么这不好?
在没有显式导入的情况下使用全局 Jest 函数,会让依赖变得 隐式,并且在类型检查、编辑器工具支持,以及 在不同测试运行器之间迁移时,都可能引发问题。
示例
以下是此规则的错误代码示例:
javascript
describe("suite", () => {
test("foo");
expect(true).toBeDefined();
});以下是此规则的正确代码示例:
javascript
import { describe, expect, test } from "@jest/globals";
describe("suite", () => {
test("foo");
expect(true).toBeDefined();
});配置
此规则接受一个包含以下属性的配置对象:
types
type: array
default: ["hook", "describe", "test", "expect", "jest", "unknown"]
要强制导入的 Jest 函数类型。
types[n]
type: "hook" | "describe" | "test" | "expect" | "jest" | "unknown"
如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["jest"],
"rules": {
"jest/prefer-importing-jest-globals": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["jest"],
rules: {
"jest/prefer-importing-jest-globals": "error",
},
});bash
oxlint --deny jest/prefer-importing-jest-globals --jest-plugin版本
此规则已在 v1.60.0 中添加。
