Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

vitest/prefer-expect-type-of

Require toBeTypeOf instead of expect(typeof value).toBe(...)

expect(typeof value).toBe("string") only gives expect the name of the type. When the test fails, the output compares "number" with "string" and doesn't show the value. expect(value).toBeTypeOf("string") shows it.

The rule reports typeof inside expect with toBe or toEqual, with or without .not. Despite its name, it asks for the toBeTypeOf matcher, not for expectTypeOf().

--fix rewrites the assertion.

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"vitest/prefer-expect-type-of" = "error" # or "warn"

Examples

Reported:

import { expect, test } from "vitest";

test("the user has a name", () => {
  expect(typeof user.name).toBe("string");
  expect(typeof user.save).not.toEqual("undefined");
});

Not reported:

import { expect, test } from "vitest";

test("the user has a name", () => {
  expect(user.name).toBeTypeOf("string");
  expect(user.save).not.toBeTypeOf("undefined");
});