Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

vitest/prefer-to-be

Require toBe() for primitive literals, and toBeNull(), toBeUndefined(), toBeNaN()

toEqual() and toStrictEqual() compare objects property by property. For a string, a number or a boolean they do the same check as toBe(), and toBe() tells the reader that one plain value is expected. null, undefined and NaN have matchers that name the value.

WrittenUse instead
toEqual(2), toStrictEqual("config")toBe(2), toBe("config")
toBe(null), toEqual(null)toBeNull()
toBe(undefined), not.toBeDefined()toBeUndefined()
not.toBe(undefined), not.toBeUndefined()toBeDefined()
toBe(NaN), toEqual(NaN)toBeNaN()

--fix rewrites the matcher.

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"vitest/prefer-to-be" = "error" # or "warn"

Examples

Reported:

import { expect, test } from "vitest";

test("parses the header", () => {
  expect(header.version).toEqual(2);
  expect(header.name).toStrictEqual("config");
  expect(header.parent).toBe(null);
  expect(header.comment).not.toBeDefined();
  expect(header.size).not.toBe(undefined);
  expect(header.date).not.toBeUndefined();
  expect(header.ratio).toEqual(NaN);
});

Not reported:

import { expect, test } from "vitest";

test("parses the header", () => {
  expect(header.version).toBe(2);
  expect(header.name).toBe("config");
  expect(header.parent).toBeNull();
  expect(header.comment).toBeUndefined();
  expect(header.size).toBeDefined();
  expect(header.date).toBeDefined();
  expect(header.ratio).toBeNaN();
});

Only literals count. The rule ignores objects, arrays and variables:

import { expect, test } from "vitest";

test("parses the body", () => {
  expect(body.tags).toEqual(["a", "b"]);
  expect(body.length).toEqual(expectedLength);
});