Skip to content
← Back to rules

jest/prefer-to-contain 风格

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);

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

json
{
  "rules": {
    "vitest/prefer-to-contain": "error"
  }
}

如何使用

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

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/prefer-to-contain": "error"
  }
}
bash
oxlint --deny jest/prefer-to-contain --jest-plugin

参考资料