vitest/prefer-to-be-object

Require toBeObject() when expectTypeOf() checks for an object

fixable

expectTypeOf() has its own matcher for object types, toBeObject(). toBeInstanceOf(Object) and an instanceof Object check are longer ways to ask the same question.

The rule only checks expectTypeOf(). expect(value).toBeInstanceOf(Object) isn't reported.

--fix rewrites the assertion to toBeObject(), or to .not.toBeObject() if the original tested for the opposite.

Configure#

Off by default. To turn it on:

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

Examples#

example.ts3 problems
import { expectTypeOf, test } from "vitest";

test("the config is an object", () => {
  const config = loadConfig();
  expectTypeOf(config).toBeInstanceOf(Object);
  expectTypeOf(config instanceof Object).toBeTruthy();
  expectTypeOf(config.name instanceof Object).toBeFalsy();
});
example.tsNo problems
import { expect, expectTypeOf, test } from "vitest";

test("the config is an object", () => {
  const config = loadConfig();
  expectTypeOf(config).toBeObject();
  expectTypeOf(config.name).not.toBeObject();
  expectTypeOf(config.createdAt).toBeInstanceOf(Date);
  expect(config).toBeInstanceOf(Object);
});