Skip to content
← Back to rules

jest/prefer-to-contain Style

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

它的作用

为了获得更好的失败提示,在断言一个包含对象的数组时,应使用 toContain()

为什么这很糟糕?

当使用 toBe()toEqual()toStrictEqual() 来断言对象包含在数组中时,此规则会触发警告

示例

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

javascript
expect(a.includes(b)).toBe(true);
expect(a.includes(b)).not.toBe(true);
expect(a.includes(b)).toBe(false);
expect(a.includes(b)).toEqual(true);
expect(a.includes(b)).toStrictEqual(true);

以下是此规则的正确代码示例:

javascript
expect(a).toContain(b);
expect(a).not.toContain(b);

如何使用

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

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/prefer-to-contain": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["jest"],
  rules: {
    "jest/prefer-to-contain": "error",
  },
});
bash
oxlint --deny jest/prefer-to-contain --jest-plugin

版本

此规则于 v0.2.14 中加入。

参考资料