Skip to content

jest/prefer-to-be Style

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

What it does

When asserting against primitive literals such as numbers and strings, the equality matchers all operate the same, but read slightly differently in code.

This rule recommends using the toBe matcher in these situations, as it forms the most grammatically natural sentence. For null, undefined, and NaN this rule recommends using their specific toBe matchers, as they give better error messages as well.

Example

javascript
// valid
expect(value).not.toBe(5);
expect(getMessage()).toBe("hello world");
expect(loadMessage()).resolves.toBe("hello world");
expect(didError).not.toBe(true);
expect(catchError()).toStrictEqual({ message: "oh noes!" });

// invalid
expect(value).not.toEqual(5);
expect(getMessage()).toStrictEqual("hello world");
expect(loadMessage()).resolves.toEqual("hello world");

How to use

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

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

References

Released under the MIT License.