On this page
Write the cheapest test that can fail on the mistake you are guarding against, at the layer where that mistake is visible.
Test kinds#
| Kind | What it proves | Where it lives | Run locally |
|---|---|---|---|
| Unit (vitest, pure) | A function, transform or component, with no database, Redis or network. A socket opened in a unit test fails the test (scripts/). |
*. and *. next to the code, in each workspace |
pnpm test (all workspaces plus script tests), or pnpm - while iterating |
| Integration | Router, RLS or queue behaviour against a real database. | Files that skip unless a database URL is set, such as apps/ |
Set the URL named in the file, then run vitest on that file. There is no separate root command. |
| End to end (Playwright) | An operator journey in a browser: sign-in, navigation, upload, export. Deterministic regression only, run serially. | apps/, config in apps/ |
pnpm test:e2e against a server that is already running at the exact NEXTAUTH_ origin (the config has no webServer) |
| Data reconciliation | A number on screen equals an independently derived number on real rows. | scripts/ |
pnpm run verify:data on a seeded database |
| Tenancy gate | RLS holds for the runtime database role. | scripts/ and related, listed in verify:rls |
pnpm run verify:rls on a migrated database |
| Static audit | A code pattern that caused a past bug. | scripts/ |
pnpm run audit:all (audit:all:parallel reports every failure) |
| Agent evaluation | AI assistant answers stay within a recorded baseline. | scripts/ |
pnpm eval:agent - (the dry run is what CI runs) |
pnpm test:e2e runs Playwright only; there is no vitest tier behind that name. CI runs one Playwright smoke (apps/web/e2e/appearance-portable.spec.ts) on a fresh database. It gates that journey only; the full operator suite is stateful and runs on an explicit request against a dedicated fixture, never on shared or production data.
The two database gates need a PostgreSQL instance. verify:data runs as the owner role; verify:rls runs as the application role, which is the identity it checks (§118). Run them separately. A tenancy gate seeds with one identity, proves with another and fails on an empty run (§119).
Choose the layer#
- Pure transform, formatter, schema, reducer: unit test.
- Router, RLS policy, queue processor: integration test, and
verify:rlsfor tenancy. - A journey that crosses pages: Playwright, only when the journey is operator-critical.
- A number shown to a user: a reconciliation assertion in
verify:datawhose expected value traces to an outside source such as a customer-visible total or a frozen oracle file, never a second derivation in code (§53, §55). - A bug class that recurs: a rule and the strongest guard at the owning layer, wired into
audit:all(seeAGENTS.md, "Class of bug").
Which layer a change needs#
| Change | Static audit | Unit | verify:data |
Browser or Playwright |
|---|---|---|---|---|
Pure math (packages/) |
runs | required, locked to a frozen oracle (§55) | runs if a surface shows the number | not needed |
| New metric surface, rollup or promoter | runs | if there is a pure helper | a new assertion is required (§53) | not needed |
| Date and timezone path | the date rules catch it (§34, §41) | not needed | runs | check a month edge |
Third-party market estimates (market_) |
runs | required for the pure core | excluded by design (data planes never mix, AGENTS.) |
required |
| UI, routing, journey | runs | not needed | not needed | required |
| Schema or RLS | policy inventory and scan | integration or upgrade test | runs, plus verify:rls |
cross-organization isolation |
| A bug class that came back | a new rule and audit | depends on the layer | if it is a number bug | if it is a behaviour bug |
Third-party market estimates have no outside ground truth, so a reconciliation on them would compare code with code; that is why they stay out of verify:data and are guarded by unit tests on the pure core and by browser checks. Where an estimate feature also reads first-party orders, that part is still reconciled.
Writing a test#
- State the wrong result the test catches. If you cannot, do not add the test.
- Run it against the old code, or with your fix reverted, and see it fail for that reason. Keep the failing output for the pull request. Rule: §229. Guard: review only.
scripts/audit/slop-audit-added.py(§225) catches the mechanical part:.only, fixed-delay waits, tautological asserts, assertions on log calls, commit or ticket narration. - Name the test after the behaviour (
rejects an expired invitation), not the function or the ticket. - Build fixtures from rows, not schema changes. Keep data synthetic; no customer data, names or credentials.
What not to test#
Call counts, log output, registry or list sizes, values the subject computed (asserting the output of the code against the same code), and near-twins of a case that already exists. These fail on a harmless refactor and pass on a real bug. Rule: §229. Guard: review only; slop-audit-added.py prints call-count and class-string pins as warnings.
Isolation and cache#
- A new
apps/webtest file runs isolated by default. It joins the shared-module-graph listapps/web/vitest.pure-files.jsononly when it uses no mocks, fake timers, environment or global writes, or DOM access (§184). Guard:scripts/audit/vitest-isolation-partition.pyandscripts/test/pure-isolation-guard.ts. - A package test that reads a file outside its package lists that file in the package's turbo
testinputs, or a stale cache hides the failure (§177). Guard:scripts/audit/turbo-test-inputs.py. - Run
pnpm testandpnpm run verifyat the repo root, not through--filter, which hides cross-package failures. Affected-test selection is feedback while you work; it is never the gate (§144).
Flaky tests#
No repository rule defines a flaky-test policy; the following is a proposal, not an enforced standard. A test that fails on a clean tree and passes on rerun is a defect, not noise. Do not add retries, .skip or a longer fixed sleep to turn it green. Find the shared state or timing it depends on and remove that. If a fix will take more than a day, delete the test and open an issue that names the behaviour it covered, so the gap is visible. Playwright is configured with one worker and no retries.
Run it#
| Goal | Command |
|---|---|
| Unit tests, whole repo | pnpm test |
| Types and lint | pnpm run typecheck, pnpm run lint |
| Static audits | pnpm run audit:all |
| Reconciliation | pnpm run verify:data |
| Tenancy | pnpm run verify:rls |
| Browser regression | pnpm test:e2e |
| Everything non-browser | pnpm run verify |
On the shared build host, heavy runs go through scripts/dev/mso-run.sh so they respect its memory and load limits; the commands inside are the same.