Tests that cannot fail: no assertion on some path, assertions that check nothing or run too late, focused and skipped tests #128

Merged
sionzee merged 116 commits from tests-that-cannot-fail into main 2026-11-22 17:32:40 +01:00
Owner

Based on flow-analyses (#127), and contains it and #126: the diff shown is this piece alone.

An agent aims at "the checker passes" and "the tests pass". SAFE-TS has closed many ways of getting the first without fixing the code; the second was wide open. A test that cannot fail passes, and counts as coverage. Each of these passed tsc, safe-ts check --deny-warnings, oxlint and the test runner:

test("no assertion", () => { sum(1, 2); });
test("cannot fail", () => { const r = sum(1, 2); expect(r).toBe(r); expect(true).toBe(true); });
test("never reached", () => { if (sum(1, 2) === 4) { expect(sum(2, 2)).toBe(4); } });
test("after the test passed", () => { setTimeout(() => { expect(sum(1, 2)).toBe(4); }, 0); });
test("swallowed", () => { try { expect(sum(1, 2)).toBe(4); } catch { /* ignored */ } });
test("passes by not throwing", () => { try { sum(1, 2); } catch (e) { expect(e).toBeInstanceOf(Error); } });
test.only("focused", ...);

Eight rules

Rule Level Reports
test-without-assertion error some path through the test reaches its end without an assertion: none at all, only inside an if, only in a catch, only in one switch case, after an early return
assertion-cannot-fail error a value compared with itself, constants only, expect(x) with no matcher, toBeDefined() on a value that is never undefined, an assertion on the test's own mock
deferred-assertion error an assertion in a timer, a listener or a .then nobody awaits or returns: it runs after the test has passed
swallowed-assertion error an assertion inside a try whose catch does not rethrow
focused-test error .only on a test or a suite, which silently skips the rest
skipped-test warning .skip, .todo, xit without a reason. Every skip, with a reason or not, is counted as an escape hatch in the summary line, --json and the baseline
duplicate-test-name warning the same name twice in one describe
empty-test-suite warning a describe with no test, or a file that imports a runner and defines none

"Some path" is asked on the control-flow layer, per test, and helpers are followed through the existing call resolution: by name, as an arrow, passed by name, across files. A helper counts when every path through it asserts.

What counts, and what was decided from evidence

  • An assertion is expect(...) with a matcher, assert*, t.assert*, expect.assertions / hasAssertions, t.plan, a subtest, a runtime skip, a throw written in the test, or a branch whose other side fails the test (if (isErr(r)) assert.fail(...)).
  • A loop counts as running. An adopter has 58 tests that assert only inside a loop; all were read, and every one walks a written table or a constant list. Counting a loop as possibly empty would report all 58.
  • A call the checker cannot resolve does not count, unless its name says it checks (assert*, expect*, check*, verify*, ensure*, validate*, should*, must*).
  • "Does not throw" must be said: assert.doesNotThrow(...), expect(() => f()).not.toThrow(), await expect(p).resolves.not.toThrow().
  • Only afterEach hooks count towards a test's assertions: a beforeEach runs before the test's code.
  • Narrowed after the repository's own tests: "the same expression on both sides" means names, not member reads or calls. assert.equal(Status.Archived, Status.Archived) is a deliberate test that a getter caches, and assert.equal(f(x), f(x)) tests that a function is pure.

Evidence

  • Held-out cases: helpers that assert, only log, or assert on one path; an early return before the assertion; a failing branch that fails the test; a .then returned to the runner against one that is not; a catch that rethrows against one that logs; the same name in two different suites; a focused suite; a skip with a reason (counted, not reported). All answered as expected.
  • An adopter's project: one new finding, a toBeDefined() on a value a helper had already checked: real and harmless. Nothing else changes. Besides the 58 loop tests, the six tests that pass only through helpers were read: all correct.
  • Soundness corpus: a new oracle, "the program's own test passes while the function it tests is wrong", and seven classes for it. All seven escaped at the starting commit and are rejected now. 2 of 69 classes escape.
  • This repository: its own test files needed no change. Seven fixture directories gained annotations for findings that are correct (tests that only log, or only call code), and two docs examples now assert instead of logging.

What stays unseen

  • A list that turns out empty at run time, since loops count as running.
  • A call the checker cannot resolve is judged by its name alone, both ways.
  • expect(spy).toHaveBeenCalled() after the test called the spy itself; vi.spyOn(...).mockReturnValue(...).
  • A whole test commented out; skips and focus set in a runner's config file; test.fails.
  • An assertion inside a helper that is called from a try, for swallowed-assertion.
  • Constant expressions that need folding (assert.equal(1 + 1, 2)).

Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests and the merge) in Claude Code.

Based on `flow-analyses` (#127), and contains it and #126: the diff shown is this piece alone. An agent aims at "the checker passes" and "the tests pass". SAFE-TS has closed many ways of getting the first without fixing the code; the second was wide open. A test that cannot fail passes, and counts as coverage. Each of these passed `tsc`, `safe-ts check --deny-warnings`, oxlint and the test runner: ```ts test("no assertion", () => { sum(1, 2); }); test("cannot fail", () => { const r = sum(1, 2); expect(r).toBe(r); expect(true).toBe(true); }); test("never reached", () => { if (sum(1, 2) === 4) { expect(sum(2, 2)).toBe(4); } }); test("after the test passed", () => { setTimeout(() => { expect(sum(1, 2)).toBe(4); }, 0); }); test("swallowed", () => { try { expect(sum(1, 2)).toBe(4); } catch { /* ignored */ } }); test("passes by not throwing", () => { try { sum(1, 2); } catch (e) { expect(e).toBeInstanceOf(Error); } }); test.only("focused", ...); ``` ## Eight rules | Rule | Level | Reports | |---|---|---| | `test-without-assertion` | error | some path through the test reaches its end without an assertion: none at all, only inside an `if`, only in a `catch`, only in one `switch` case, after an early `return` | | `assertion-cannot-fail` | error | a value compared with itself, constants only, `expect(x)` with no matcher, `toBeDefined()` on a value that is never `undefined`, an assertion on the test's own mock | | `deferred-assertion` | error | an assertion in a timer, a listener or a `.then` nobody awaits or returns: it runs after the test has passed | | `swallowed-assertion` | error | an assertion inside a `try` whose `catch` does not rethrow | | `focused-test` | error | `.only` on a test or a suite, which silently skips the rest | | `skipped-test` | warning | `.skip`, `.todo`, `xit` without a reason. Every skip, with a reason or not, is counted as an escape hatch in the summary line, `--json` and the baseline | | `duplicate-test-name` | warning | the same name twice in one `describe` | | `empty-test-suite` | warning | a `describe` with no test, or a file that imports a runner and defines none | "Some path" is asked on the control-flow layer, per test, and helpers are followed through the existing call resolution: by name, as an arrow, passed by name, across files. A helper counts when every path through it asserts. ## What counts, and what was decided from evidence - **An assertion** is `expect(...)` with a matcher, `assert*`, `t.assert*`, `expect.assertions` / `hasAssertions`, `t.plan`, a subtest, a runtime skip, a `throw` written in the test, or a branch whose other side fails the test (`if (isErr(r)) assert.fail(...)`). - **A loop counts as running.** An adopter has 58 tests that assert only inside a loop; all were read, and every one walks a written table or a constant list. Counting a loop as possibly empty would report all 58. - **A call the checker cannot resolve** does not count, unless its name says it checks (`assert*`, `expect*`, `check*`, `verify*`, `ensure*`, `validate*`, `should*`, `must*`). - **"Does not throw" must be said**: `assert.doesNotThrow(...)`, `expect(() => f()).not.toThrow()`, `await expect(p).resolves.not.toThrow()`. - **Only `afterEach` hooks count** towards a test's assertions: a `beforeEach` runs before the test's code. - **Narrowed after the repository's own tests:** "the same expression on both sides" means names, not member reads or calls. `assert.equal(Status.Archived, Status.Archived)` is a deliberate test that a getter caches, and `assert.equal(f(x), f(x))` tests that a function is pure. ## Evidence - **Held-out cases:** helpers that assert, only log, or assert on one path; an early return before the assertion; a failing branch that fails the test; a `.then` returned to the runner against one that is not; a `catch` that rethrows against one that logs; the same name in two different suites; a focused suite; a skip with a reason (counted, not reported). All answered as expected. - **An adopter's project:** one new finding, a `toBeDefined()` on a value a helper had already checked: real and harmless. Nothing else changes. Besides the 58 loop tests, the six tests that pass only through helpers were read: all correct. - **Soundness corpus:** a new oracle, "the program's own test passes while the function it tests is wrong", and seven classes for it. All seven escaped at the starting commit and are rejected now. 2 of 69 classes escape. - **This repository:** its own test files needed no change. Seven fixture directories gained annotations for findings that are correct (tests that only log, or only call code), and two docs examples now assert instead of logging. ## What stays unseen - A list that turns out empty at run time, since loops count as running. - A call the checker cannot resolve is judged by its name alone, both ways. - `expect(spy).toHaveBeenCalled()` after the test called the spy itself; `vi.spyOn(...).mockReturnValue(...)`. - A whole test commented out; skips and focus set in a runner's config file; `test.fails`. - An assertion inside a helper that is called from a `try`, for `swallowed-assertion`. - Constant expressions that need folding (`assert.equal(1 + 1, 2)`). Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests and the merge) in Claude Code.
What was open: SAFE-TS closed many ways to get "the checker passes" without fixing the code, but
"the tests pass" was wide open. A test that cannot fail passes and counts as coverage; each of
these passed tsc, `safe-ts check --idiomatic --deny-warnings`, oxlint and the runner:

    test("x", () => { sum(1, 2); });                         // no assertion
    expect(r).toBe(r); expect(true).toBe(true);              // can't fail
    if (sum(1, 2) === 4) { expect(...) }                     // never reached
    setTimeout(() => expect(...)); p.then((r) => expect(...)) // after the test passed
    try { expect(...) } catch {}                             // swallowed
    try { f() } catch (e) { expect(e)... }                   // passes by not throwing
    two it("adds"), test.only, test.skip without a reason

What closes it (cannot_fail.rs, on the shared control-flow layer):

- test-without-assertion (error): an every-path question on the test's flow graph, reduced to
  which blocks assert and which call helpers; the whole-program pass follows helpers by name,
  through arrows, passed by name and across files (resolve_callee). A branch whose other side
  fails the test checks something; loops and callbacks run where handed over count; an
  afterEach that asserts counts. Unknown calls fail closed except by name (assert*, check*, ...).
  The explicit "doesn't throw" forms are what to write instead.
- assertion-cannot-fail (error): same name on both sides, constants only, no matcher,
  toBeDefined() on a value whose written type has no undefined, the test's own mock.
- deferred-assertion (error), swallowed-assertion (error), focused-test (error),
  skipped-test (warn), duplicate-test-name (warn), empty-test-suite (warn).
- Skipped tests are escape hatches: summary line, --json, baseline (skippedTests).
- Soundness corpus: a test-passes oracle (the program's own node:test test passes while run()
  shows the function is wrong); seven classes added, all closed.

Shared code touched: shape.rs records the subject of toBeDefined()/toBeUndefined()/toBeNull()
(additive), project.rs adds a memo and one per-file check, the CLI one hatch count. flow.rs is
used as is.

Evidence: on an adopter (about 2,100 tests) one finding, a redundant toBeDefined() after a
helper made the value present; no other change by rule on the adopter or the examples. 58
adopter tests assert only inside loops over written tables: read one by one, all deliberate,
hence loops count as running. The repository's own tests needed no change once member reads
stopped counting as "the same expression" (a getter cache test compares one with itself).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Flow guide: the test analysis in the table of what runs on the layer
All checks were successful
Tests / test (pull_request) Successful in 8m9s
Benchmarks / bench (pull_request) Successful in 2m6s
a7e4f92581
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee changed target branch from flow-analyses to main 2026-11-22 01:53:17 +01:00
Merge main: the pass's speed work and the control-flow analyses have landed
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m25s
Tests / test (pull_request) Successful in 10m25s
e9787315d6
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch tests-that-cannot-fail 2026-11-22 17:32:40 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!128
No description provided.