Skip to content

jest/prefer-to-contain Style

What it does

In order to have a better failure message, toContain() should be used upon asserting expectations on an array containing an object.

Why is this bad?

TThis rule triggers a warning if toBe(), toEqual() or toStrictEqual() is used to assert object inclusion in an array

Example

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

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

How to use

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

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

References

Released under the MIT License.