Tests that cannot fail: no assertion on some path, assertions that check nothing or run too late, focused and skipped tests #128
Loading…
Reference in a new issue
No description provided.
Delete branch "tests-that-cannot-fail"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:Eight rules
test-without-assertionif, only in acatch, only in oneswitchcase, after an earlyreturnassertion-cannot-failexpect(x)with no matcher,toBeDefined()on a value that is neverundefined, an assertion on the test's own mockdeferred-assertion.thennobody awaits or returns: it runs after the test has passedswallowed-assertiontrywhosecatchdoes not rethrowfocused-test.onlyon a test or a suite, which silently skips the restskipped-test.skip,.todo,xitwithout a reason. Every skip, with a reason or not, is counted as an escape hatch in the summary line,--jsonand the baselineduplicate-test-namedescribeempty-test-suitedescribewith 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
expect(...)with a matcher,assert*,t.assert*,expect.assertions/hasAssertions,t.plan, a subtest, a runtime skip, athrowwritten in the test, or a branch whose other side fails the test (if (isErr(r)) assert.fail(...)).assert*,expect*,check*,verify*,ensure*,validate*,should*,must*).assert.doesNotThrow(...),expect(() => f()).not.toThrow(),await expect(p).resolves.not.toThrow().afterEachhooks count towards a test's assertions: abeforeEachruns before the test's code.assert.equal(Status.Archived, Status.Archived)is a deliberate test that a getter caches, andassert.equal(f(x), f(x))tests that a function is pure.Evidence
.thenreturned to the runner against one that is not; acatchthat 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.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.What stays unseen
expect(spy).toHaveBeenCalled()after the test called the spy itself;vi.spyOn(...).mockReturnValue(...).test.fails.try, forswallowed-assertion.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]>