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).toBeInstanceOfPrefer toBeObject() for object assertions(Object);
expectTypeOf(config instanceof Object).toBeTruthyPrefer toBeObject() for object assertions();
expectTypeOf(config.name instanceof Object).toBeFalsyPrefer toBeObject() for object assertions();
});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);
});