test-without-assertion
A test with a path from its start to its end on which no assertion runs: it has none at all, its assertions sit in a branch the test may not take, it returns before them, or they are all in a catch. Reported in test files (named *.test.ts / *.spec.ts, or importing a test runner: node:test, Vitest, Jest's @jest/globals, bun:test, @playwright/test, a package re-exporting one, one listed in "testRunners"). On by default, as an error.
Why: a test that checks nothing passes whatever the code does, and still counts as a test. An agent asked to make a failing test pass can do it by taking the assertion out, or moving it where it doesn't run, without touching the bug. tsc, the linters and the runner all pass it. Guide: Testing.
❌ Rejected: it passes whatever total returns
✕ Rejectedtest-without-assertion
ts
import { test } from "node:test";
function total(prices: readonly number[]): number {
return prices.reduce((sum, price) => sum + price, 0);
}
test("total adds the prices", () => {
total([3, 4]);
});✅ Accepted: the result is asserted
✓ 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", () => {
assert.equal(total([3, 4]), 7);
});❌ Rejected: when the result is wrong, the assertion is skipped and the test passes
✕ Rejectedtest-without-assertion
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]);
if (sum > 5) {
assert.equal(sum, 7);
}
});❌ Rejected: the assertions are all in the catch, so it passes when nothing throws
✕ Rejectedtest-without-assertion
ts
import assert from "node:assert/strict";
import { test } from "node:test";
test("broken settings are refused", () => {
try {
JSON.parse("{ port: 80");
} catch (error) {
assert.ok(error instanceof SyntaxError);
}
});✅ Accepted: the throw itself is what is checked
✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { test } from "node:test";
test("broken settings are refused", () => {
assert.throws(() => JSON.parse("{ port: 80"), SyntaxError);
});A test whose point is that the code doesn't throw
Say so: the explicit form is an assertion, and fails with a message that names the throw.
✓ Accepted0 diagnostics
ts
import assert from "node:assert/strict";
import { test } from "node:test";
function warmUp(sizes: readonly number[]): readonly number[] {
return sizes.map((size) => size * 2);
}
async function connect(host: string): Promise<string> {
await Promise.resolve();
return host;
}
test("warming up the caches doesn't throw", () => {
assert.doesNotThrow(() => warmUp([1, 2, 3]));
});
test("connecting doesn't reject", async () => {
await assert.doesNotReject(connect("localhost"));
});ts
import assert from "node:assert/strict";
import { test } from "node:test";
function warmUp(sizes: readonly number[]): readonly number[] {
{ const __la1 = sizes; const __lr1: any[] = new Array(__la1.length); for (let __li1 = 0, __ln1 = __la1.length; __li1 < __ln1; __li1++) { if (!(__li1 in __la1)) continue; const __le1 = __la1[__li1]!; { let size = __le1; __lr1[__li1] = (size * 2); }} return __lr1; }
}
async function connect(host: string): Promise<string> {
await Promise.resolve();
return host;
}
test("warming up the caches doesn't throw", () => {
assert.doesNotThrow(() => warmUp([1, 2, 3]));
});
test("connecting doesn't reject", async () => {
await assert.doesNotReject(connect("localhost"));
});With Vitest or Jest: expect(() => warmUp(sizes)).not.toThrow() and await expect(connect(host)).resolves.not.toThrow().
What counts as an assertion
expect(...)with a matcher (.toBe,.toThrow,.resolves.toEqual, chai's.to.be.true, ...),expect.assertions(n),expect.hasAssertions(),expectTypeOf(...);assert(...),assert.*(...)(node:assert, chai, and the functions imported from them by name),t.assert.*(...)andt.plan(n)on the test's context; std'sexpectPresent(value).- A subtest (
t.test(...)), a skip at run time (t.skip("why"), Playwright'stest.skip()), and athrowwritten in test code (test files and atestdirectory's helpers). - A branch whose other side fails the test: after
if (isErr(r)) assert.fail(r.error.message), the path that goes on has checkedr. - A helper whose every path asserts, followed through calls by name, through an arrow (
const check = (n) => expect(n)...), passed by name (list.forEach(check)), and across files (expectSumimported from./checks.ts). A helper that asserts only on some paths doesn't count. - A loop whose body asserts: the table it walks is taken not to be empty. A callback that runs where it is handed over (
list.forEach((x) => ...), a helper's parameter,new Promise(...)) counts where it is handed over; one that runs later doesn't (deferred-assertion). - An
afterEachthat asserts on every path checks every test around it (afterEach(() => expect(errors).toEqual([]))). AbeforeEachdoesn't: it runs before the test's code.
A call the checker can't follow (a package's function, a method on a value of unknown type) counts only when its name says it checks: assert…, expect…, check…, verify…, ensure…, validate…, should…, must… followed by a capital letter or nothing. Other unknown calls fail closed: they don't count. On an adopter's project (about 2,100 tests) no test depends on this either way: every test there asserts directly or through a helper the checker follows.
A test that applies only sometimes is skipped explicitly (test.skipIf(condition), t.skip("why")), not ended early with a return.
What the rule can't see
- An empty table: a loop or
forEachover a list that turns out empty runs no assertion, and the test passes. An adopter has 58 tests whose assertions are all inside a loop, every one over a written table or a constant list; counting the loop as running is what keeps them silent. - A helper in a file without assertions that only calls another helper (a wrapper of a wrapper in source code) is taken not to assert.
- What is asserted: whether the assertion is about the code under test is out of scope; one that can't fail is
assertion-cannot-fail's.