Skip to content
← Back to rules

jest/no-standalone-expect Correctness

作用

禁止在 testit 块之外使用 expect 语句。位于辅助函数中的 expect(但在 testit 块之外)不会触发此规则。

expect.hasAssertions() 这样的语句不会触发此规则,因为这些调用即使不在测试块中也会执行。

为什么这不好?

test 块之外的 expect 语句不会被 Jest 测试运行器执行,这意味着它们实际上没有测试任何内容。这会导致对测试覆盖率产生错误的信心,并可能掩盖本来可以通过正确测试发现的 bug。

示例

以下是此规则的错误代码示例:

javascript
describe("a test", () => {
  expect(1).toBe(1);
});

配置

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

additionalTestBlockFunctions

type: string[]

default: []

一个函数名数组,也应被视为测试块。

使用方法

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/no-standalone-expect": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["jest"],
  rules: {
    "jest/no-standalone-expect": "error",
  },
});
bash
oxlint --deny jest/no-standalone-expect --jest-plugin

版本

此规则在 v0.0.13 中新增。

参考资料