Skip to content
← Back to rules

jest/no-jasmine-globals 风格

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

它做了什么

此规则报告任何使用 Jasmine 全局变量的情况,这些全局变量未被移植到 Jest,同时建议使用 Jest 自身 API 中的替代方案。

为什么这是个问题

在从 Jasmine 迁移到 Jest 时,依赖 Jasmine 特有的全局变量会导致兼容性问题,并阻止你利用 Jest 提供的改进测试功能和更优的错误报告。

示例

此规则的错误代码示例:

javascript
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
test("我的测试", () => {
  pending();
});
test("我的测试", () => {
  jasmine.createSpy();
});

此规则的正确代码示例:

javascript
jest.setTimeout(5000);
test("我的测试", () => {
  // 使用 test.skip() 替代 pending()
});
test.skip("我的测试", () => {
  // 跳过的测试
});
test("我的测试", () => {
  jest.fn(); // 使用 jest.fn() 替代 jasmine.createSpy()
});

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/no-jasmine-globals": "error"
  }
}
bash
oxlint --deny jest/no-jasmine-globals --jest-plugin

参考资料