vitest/prefer-to-contain
Require toContain() instead of comparing the result of includes() with a boolean
fixable
When expect(tags.includes("new")).toBe(true) fails, the message only says that false isn't true. expect(tags).toContain("new") prints the array and the item it looked for.
The rule reports expect(x.includes(y)) matched against true or false with toBe(), toEqual(), or toStrictEqual().
--fix rewrites the assertion, and adds or drops .not so it still tests the same thing.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"vitest/prefer-to-contain" = "error" # or "warn"Examples#
example.ts2 problems
import { expect, test } from "vitest";
test("a draft is tagged", () => {
const tags = getTags(draft);
expect(tags.includes("draft")).toBeSuggest using toContain().(true);
expect(tags.includes("published")).toEqualSuggest using toContain().(false);
});example.tsNo problems
import { expect, test } from "vitest";
test("a draft is tagged", () => {
const tags = getTags(draft);
expect(tags).toContain("draft");
expect(tags).not.toContain("published");
});A start index has no equivalent in toContain(), so this is allowed:
example.tsNo problems
import { expect, test } from "vitest";
test("the tag is not first", () => {
expect(getTags(draft).includes("draft", 1)).toBe(true);
});