Skip to content
← Back to rules

vitest/prefer-to-be Style

💡 A suggestion is available for this rule.

What it does ​

Recommends using toBe matcher for primitive literals and specific matchers for null, undefined, and NaN.

Why is this bad? ​

This rule recommends using the toBe matcher for primitive literals, 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.

Examples ​

Examples of incorrect code for this rule:

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

Examples of correct code for this rule:

javascript
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!" });

How to use ​

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

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

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

Version ​

This rule was added in v0.2.14.

References ​