Skip to content

Convert unit tests to expect tests - #82

Merged
carlassmann merged 6 commits into
mainfrom
t3/explain-expect-tests
Nov 20, 2026
Merged

carlassmann merged 6 commits into
mainfrom
t3/explain-expect-tests

Conversation

@carlassmann

@carlassmann carlassmann commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Why

Hand-written assertions repeat the logic under test, and they only check what the author thought to check. Expect tests (vitest toMatchInlineSnapshot) record the real output. You review it once, and after that any change shows up as a diff. When a change is intended, vitest -u rewrites the snapshot.

What

  • Unit tests now check readable output:
    • pure functions: tables with one input -> output line per case
    • data operations: one short summary line per item, e.g. "pinned-1 · Ada · pinned"
  • Each AI tool test file has one snapshot of the full payload, showing what the model receives. IDs and timestamps are replaced via redactNoise in src/shared/tools/test-helpers.ts.
  • Rules that must always hold stay as named assertions: thrown errors, permission denials, data-loss guards.
  • extractSearchTerms was duplicated in text-highlight.tsx and markdown-with-highlight.tsx. Its test exercised a third copy pasted into the test file. It now lives in src/shared/lib/search-terms.ts, and the test covers the real function.
  • Same problem in use-sync-reminders-to-sw.test.ts, which tested a copy of extractRemindersForSync. The function is now exported from the hook module, unchanged, and tested directly.

Enforcement

Two ESLint rules in tools/eslint-plugin-architecture keep plain assertions from creeping back in. A few runtime checks back them up.

  • expect-tests-only runs on all of src. It doesn't need type information, so it's fast.
    • Allowed matchers: toMatchInlineSnapshot and toThrow*. The snapshot text must be a written string with no ${}.
    • Reported:
      • other matchers and .not
      • expect used other than by calling it directly: aliases, the test context's expect, .call and .bind
      • chai and assert in any form, including dynamic imports
      • snapshot property matchers
      • expect.extend and custom serializers outside vitest.setup.ts
      • expect.assertions(0)
      • throw directly in a test body
      • vitest, expect or the test globals in non-test files
  • expect-tests-no-booleans runs on test files and test helpers. It uses type information to report a value that is only true/false: directly, inside a template string, converted with String, Number, JSON.stringify or a ? : with literal branches, or as an array of booleans. Booleans as fields of an object are allowed.
  • Runtime:
    • Every test must make at least one assertion (requireAssertions).
    • Snapshotting a bare or boxed boolean fails.
    • In CI, an empty or outdated snapshot fails.
  • Opt-outs use // eslint-disable-next-line architecture/expect-tests-only -- invariant: <why>. A processor rejects opt-outs without invariant:, file-wide disables and eslint ... off comments, after ESLint applies disable comments. The codebase has 4 such opt-outs.
  • Type-only tests go in *.test-d.ts.
  • The convention is in docs/testing.md.

Stress test: Codex tried 105 ways to get a hand-written assertion past these checks. Every realistic bypass is now caught by lint or by the test run. Still open, and left to review:

  • obfuscation, such as computed keys or Reflect.get
  • a throw hidden in a helper function
  • snapshots of constants unrelated to the code under test
  • opt-out reasons that are empty in substance

Not converted

  • ESLint RuleTester tests: they are already tables of valid and invalid code.
  • Playwright e2e tests.

Weight

  • 40 files, +2068 / −2622 lines (includes bun.lock).
  • The first pass added ~1500 lines: every test had a full-payload snapshot plus the old assertions. Both were cut back.
  • Similar scenarios were merged, so the suite now has 225 tests. The biggest rewrite is delete-covalue.test.ts (352 → 114 lines).

Review

One review pass compared every rewritten file against main by mutating the source code. It found 4 checks the conversion had dropped:

  1. the people count
  2. the return value when marking a repeating reminder done
  3. the collaborator's note result
  4. the note text in later demo scenes

All 4 are restored (second commit). The count and repeating-reminder fixes were re-checked with the same mutations: the restored tests fail.

Second review (Claude + Codex)

Fixed, each proven by deliberately breaking the code under test or by running ESLint:

  • Lint gaps: .spec files, expect.soft, assert, toMatch, toSatisfy, custom matchers, comparisons hidden in snapshots, and opt-outs with any reason.
  • Dropped checks restored:
    • note search counts
    • note identity in cleanup: a copy instead of a move now fails
    • empty vs missing demo note text
  • redactNoise now walks the value. It replaces only whole-string timestamps and IDs, and gives the same ID the same <id:N> alias, so relationships stay visible.
  • Weak fixtures fixed: reminder-dates now sets different createdAt and updatedAt, and getPersonOwnerName uses a real profile name.

Risks

  • Approving snapshots without reading them. When running -u, read the diff.
  • markdown-with-highlight now trims the query inside extractSearchTerms. Its caller already trimmed, so behaviour is unchanged.
  • A new field in a tool's output fails one snapshot per tool file. That is intended.

Checks

  • bun check passes.
  • Vitest: 58 files, 225 tests pass.
  • ESLint plugin tests: 140 pass.

No UI changes, so no screenshots.

written with claude-opus-5-5 in T3 Code

Snapshot readable outputs (tables of input -> output, compact per-item
summaries) instead of hand-written assertions. Named assertions remain
for must-hold rules: thrown errors, permission denials, data-loss guards.

Also give extractSearchTerms one shared home; its test previously
covered a copy pasted into the test file.

written with claude-opus-5-5 in T3 Code
People count, repeating-reminder return value, collaborator note result,
and note text persisting into later demo scenes.

written with claude-opus-5-5 in T3 Code
@vercel

vercel Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tilly Ready Ready Preview Nov 20, 2026 1:21pm UTC

Ban hand-written matchers in src unit tests (vitest/no-restricted-matchers).
Must-hold rules opt out with a disable comment that states why; the
description is required. Convert the remaining assertions, and test the
real extractRemindersForSync instead of a copy in the test file.

written with claude-opus-5-5 in T3 Code
Replace the matcher denylist with an allowlist rule
(architecture/expect-tests-only) plus a processor that enforces
'-- invariant:' opt-outs, covering .spec files, expect.soft, assert,
custom matchers and comparisons hidden inside snapshots.

Restore dropped checks (search counts, note identity in cleanup,
empty vs missing demo notes) and make redactNoise alias ids
consistently so relationships stay visible.

written with claude-opus-5-5 in T3 Code
Report boolean-typed expect subjects (type-aware) instead of matching
comparison syntax, fail bare boolean snapshots at runtime, lint shared
test helpers, and ban throw directly in test bodies. expectTypeOf and
assertType stay allowed.

written with claude-opus-5-5 in T3 Code
Lint expect usage across all of src, reject expect aliases, chai and
assert in any form, boolean conversions, snapshot property matchers,
dynamic snapshot text and setup-only APIs outside vitest.setup.ts.
Require at least one assertion per test and reject boxed booleans at
runtime.

written with claude-opus-5-5 in T3 Code
@carlassmann
carlassmann merged commit 44b76ea into main Nov 20, 2026
4 checks passed

This branch was successfully deployed

1 active deployment
Preview — b171fd3c Deployed Nov 20, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant