assertion-cannot-fail
An assertion whose outcome doesn't depend on the code: both sides the same expression (expect(r).toBe(r), assert.equal(r, r), assert.ok(a === a)), only constants (expect(true).toBe(true), expect([1, 2]).toEqual([1, 2]), a literal compared with a local const holding it), expect(x) with no matcher, toBeDefined() / not.toBeUndefined() on a value whose written type has no undefined, and a call of a mock the test made (const spy = vi.fn(() => 3); expect(spy()).toBe(3)). Reported in test files. On by default, as an error.
Why: such an assertion passes whatever the code does, so the test around it checks nothing while looking as if it did. It is also the shortest way to turn a failing test green without touching the bug. Guide: Testing.
❌ Rejected: the value is compared with itself
✕ Rejectedassertion-cannot-fail
ts
import assert from "node:assert/strict";
import { test } from "node:test";
function total(prices: readonly number[]): number {
return prices.reduce((sum, price) => sum + price, 0);
}
test("total adds the prices", () => {
const sum = total([3, 4]);
assert.equal(sum, sum);
});❌ Rejected: constants only
✕ Rejectedassertion-cannot-fail
ts
import assert from "node:assert/strict";
import { test } from "node:test";
function total(prices: readonly number[]): number {
return prices.reduce((sum, price) => sum + price, 0);
}
test("total adds the prices", () => {
const expected = 7;
total([3, 4]);
assert.equal(expected, 7);
});✅ Accepted: the code's result against what it must be
✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { test } from "node:test";
function total(prices: readonly number[]): number {
return prices.reduce((sum, price) => sum + price, 0);
}
test("total adds the prices", () => {
const expected = 7;
assert.equal(total([3, 4]), expected);
});❌ Rejected: the test checks what it told the mock to return
✕ Rejectedassertion-cannot-fail
ts
import assert from "node:assert/strict";
import { mock, test } from "node:test";
test("prices come from the catalogue", () => {
const lookup = mock.fn((sku: string) => sku.length);
assert.equal(lookup("tea"), 3);
});✅ Accepted: the mock is handed to the code, and the code's result is asserted
✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { mock, test } from "node:test";
function basket(skus: readonly string[], price: (sku: string) => number): number {
return skus.reduce((sum, sku) => sum + price(sku), 0);
}
test("a basket costs what its items cost", () => {
const lookup = mock.fn((sku: string) => sku.length);
assert.equal(basket(["tea", "cake"], lookup), 7);
assert.equal(lookup.mock.callCount(), 2);
});Presence checks
toBeDefined() and not.toBeUndefined() can only fail on a value that may be undefined. When the value's type as written has none (a function declared to return number, a value a check already made present), the matcher accepts everything the value can be. A value typed Option<T>, map.get(k), list[i] or find(...) may be missing, and checking it is fine. An adopter's project has one such assertion, on a value a helper had already checked: expect(tree).toBeDefined() after tree = some(...). not.toBeNull() on a value that is never null is no-null-check-for-undefined's.
What the rule can't see
- A member compared with itself (
expect(r.n).toBe(r.n)): a member may be a getter that makes a new value each time, and a test that its value is cached compares it with itself on purpose (the repository's own enum tests do,assert.equal(Status.Archived, Status.Archived)). Only a name, a literal or what operators make of them counts as the same on both sides; a call never does (expect(next()).toBe(next())). - A constant reached through a module-level binding (that is
pinned-constant-assert, which says what to test instead), an object or array held in aconst(its contents may change), and computed values. - A mock replaced on a real object (
vi.spyOn(obj, "m").mockReturnValue(3), thenexpect(obj.m()).toBe(3)), andexpect(spy).toHaveBeenCalled()after the test calledspyitself. - A negated assertion (
expect(a).not.toBe(a)always fails, which a run shows).