Skip to content
← Back to rules

jest/no-restricted-jest-methods 风格

它的作用

限制使用特定的 jestvi 方法。

为什么这是不好的?

某些 Jest 或 Vitest 方法可能已被弃用、在特定上下文中不推荐使用,或与你的测试环境不兼容。限制这些方法有助于保持测试实践的一致性和可靠性。

默认情况下,此规则不会限制任何方法。 您必须配置该规则才能禁用任何内容。

示例

此规则的错误代码示例:

javascript
jest.useFakeTimers();
it("通过 advanceTimersByTime 在 1 秒后调用回调函数", () => {
  // ...

  jest.advanceTimersByTime(1000);

  // ...
});

test("播放视频", () => {
  const spy = jest.spyOn(video, "play");

  // ...
});

此规则与 eslint-plugin-vitest 兼容, 要使用它,请在您的 .oxlintrc.json 中添加以下配置:

json
{
  "rules": {
    "vitest/no-restricted-vi-methods": [
      "error",
      { "badFunction": "不要使用 `badFunction`,因为它不好。" }
    ]
  }
}

配置

此规则接受一个包含以下属性的配置对象:

restrictedJestMethods

type: Record<string, string>

default: {}

受限的 Jest 方法名称到自定义消息的映射 —— 或者为 null,表示使用通用消息。

如何使用

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

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

参考资料