📚 Focus cross-module testing guidance

Keep testing discipline and verification requirements while leaving
module-specific commands and fixtures to their own memories.

AI-assisted-by: gpt-6.1-terra
This commit is contained in:
Andrey Antukh 2026-11-23 22:39:28 +00:00
parent e202ed1878
commit 842c017c10

View File

@ -1,152 +1,18 @@
# Testing
## Overview
Every behavior change needs a test. Read the affected modules' testing guidance for commands, fixtures and runner registration: `mem:backend/testing`, `mem:common/testing`, `mem:frontend/testing` and `mem:exporter/testing`. Documentation and configuration changes with no behavioral impact do not need behavior tests.
Tests are proof that code works. Every behavior change needs a test.
## Workflow
Testing in this monorepo varies by module. Each module has its own test commands, helpers, runner registration requirements, and conventions. This memory covers cross-cutting testing principles. For module-specific commands and helpers, consult:
- Prefer RED → GREEN → REFACTOR: write a failing test, implement the smallest change that passes, then refactor and rerun tests. For a bug, first demonstrate the failure and confirm it fails before fixing it. If TDD is impractical during exploration, still add tests before completing the work.
- Assert observable outcomes rather than implementation details. Keep each test readable, isolated and focused on one behavior; code-facing test text must be in English.
- Prefer real implementations, then fakes, stubs and mocks. Mock at external boundaries when needed; call counts or order assertions should have a behavioral reason. Use per-test setup and teardown; suite-wide fixtures are for expensive immutable setup.
- Use table-driven tests when each case has the same short body. Cases with complex setup, asynchronous work or shared mutable state are clearer as separate tests.
- Choose the appropriate level: unit tests for pure logic, integration tests for RPC or database boundaries, and end-to-end tests for full flows. Do not add frontend Playwright E2E tests unless the user explicitly requests them.
- Avoid timing-dependent or order-dependent tests, broad snapshots, and tests of framework behavior. Never skip or disable a test merely to make checks pass.
- `mem:common/testing` — CLJC unit tests (JVM + JS), test helpers, fixture builders, production-path change helpers
- `mem:frontend/testing` — CLJS unit tests, Playwright E2E integration tests, live browser verification via nREPL
- `mem:backend/testing` — JVM `clojure.test` under `backend/test/`
- `mem:exporter/testing` — exporter unit tests
## Execution and verification
## When to Use
- Implementing new logic or behavior
- Fixing any bug (reproduction test required)
- Modifying existing functionality
- Adding edge case handling
**When NOT to use:** Pure configuration changes, documentation updates, or static content changes with no behavioral impact.
## TDD: Recommended Workflow
Write a failing test before writing the code that makes it pass. For bug fixes, reproduce the bug with a test before attempting a fix.
When TDD isn't practical (exploratory work, tight coupling to unknown APIs), still write tests before considering the work complete.
```
RED GREEN REFACTOR
Write a test Write minimal code Clean up the
that fails ──→ to make it pass ──→ implementation ──→ (repeat)
│ │ │
▼ ▼ ▼
Test FAILS Test PASSES Tests still PASS
```
- **RED** — Write the test first. It must fail. A test that passes immediately proves nothing.
- **GREEN** — Write the minimum code to make the test pass. Don't over-engineer.
- **REFACTOR** — With tests green, improve the code without changing behavior: extract shared logic, improve naming, remove duplication. Run tests after every step.
## The Prove-It Pattern (Bug Fixes)
When a bug is reported, **do not start by trying to fix it.** Start by writing a test that reproduces it:
1. Write a test that demonstrates the bug
2. Confirm the test FAILS (proving the bug exists)
3. Implement the fix
4. Confirm the test PASSES (proving the fix works)
5. Run the full test suite for the module (no regressions)
## Core Principles
- **Test State, Not Interactions** — assert on outcomes, not method calls; survives refactoring
- **DAMP over DRY** — tests are specifications; duplication is OK if each test is self-contained and readable. A test should tell a complete story without requiring the reader to trace through shared helpers.
- **Prefer Real Implementations** — hierarchy: Real > Fake > Stub > Mock; mock only at boundaries (network, RPC, filesystem, email)
- **Arrange-Act-Assert** — every test: setup / action / verify
- **One Assertion Per Concept** — each test verifies one behavior; split compound assertions
- **Descriptive Test Names** — names read like specifications
- **English Only** — test names, `testing` strings, comments, and every code-facing text MUST be in English; see the hard rule in `AGENTS.md`.
## Prefer Real Implementations Over Mocks
Work down this list:
1. **Real implementation** — Test the actual code with real collaborators. Highest confidence.
2. **Fake** — A simplified but functional in-memory implementation (e.g. atom/dict-backed store instead of a real database).
3. **Stub** — Returns canned data. Use when the collaborator's logic is irrelevant.
4. **Mock** — Last resort, only at boundaries. Use only when verifying interaction with an external system that cannot be faked.
**Rule of thumb:** If you can write a fake or use the real implementation, do that. If you find yourself asserting on call counts or invocation order, ask whether a fake would be clearer.
## Fixtures over Manual Setup
Use fixture/`beforeEach` mechanisms for shared setup and teardown. Each test should own its state so tests don't interfere with each other. Shorter-scope fixtures (`:each` / per-test) are preferred; longer-scope fixtures (`:once` / suite-level) are only for expensive, immutable shared setup.
## Parametrized Tests
Use your test framework's parametrize/table-driven mechanism to test multiple scenarios with a single test body. Keeps tests concise and surfaces all cases at a glance.
Only when the body is short and identical for every row. A loop that builds its own world per row (fresh store, fixtures, several awaits) or that nests two dimensions hides which case failed and shares state across cases: write one test per case instead, with a shared fixture for the arrangement.
## Test Pyramid
```
╱╲
╱ ╲ E2E (few)
╱ ╲ Full flows, real browser/server
╱──────╲
╱ ╲ Integration (some)
╱ ╲ Cross-module, test DB
╱────────────╲
╱ ╲ Unit (most)
╱ ╲ Pure logic, fast
╱──────────────────╲
```
Prefer unit tests for pure logic. Reach for integration/E2E tests when covering RPC handlers, database queries, or full user flows. In the frontend, Playwright E2E tests should not be added unless explicitly requested.
## Anti-Patterns
| Anti-Pattern | Problem | Fix |
|---|---|---|
| Testing implementation details | Breaks on refactor | Test inputs/outputs |
| Flaky tests (timing, order-dependent) | Erodes trust | Deterministic assertions, isolate state |
| Mocking everything | Tests pass, production breaks | Prefer real implementations or fakes |
| No test isolation | Pass individually, fail together | Per-test state fixtures |
| Testing framework/platform code | Wastes time | Only test YOUR code |
| Snapshot abuse | Nobody reviews, break on any change | Focused assertions |
| Skipping tests to make suite pass | Hides real failures | Fix the test or fix the code |
## Execution discipline
**CRITICAL: Test output handling rules**
When running ANY test command (CLJS/JS or JVM):
1. **NEVER pipe test output directly to `| head`, `| tail`, `| grep`, or similar filters** — this can hide failures and cause you to miss critical errors.
2. **ALWAYS redirect to a file first, then read the file:**
```bash
# CORRECT:
pnpm run test > /tmp/test-output.txt 2>&1
grep -A 5 "failures" /tmp/test-output.txt
# WRONG:
pnpm run test 2>&1 | tail -20
pnpm run test 2>&1 | grep "failures"
```
3. **Use `--focus` to narrow test scope** instead of filtering output.
4. **Read the full output file** to understand test results completely.
When running CLJS/JS tests (frontend, common):
- **Always use `pnpm run test:quiet`** — it silently builds the test bundle then runs the test runner, giving you clean test output.
- Use `pnpm run test` when you want to see build output alongside test results (always builds, then runs).
- After `build:test` has been run once, you can invoke the runner directly: `node target/tests/test.js [--focus ...] [--log-level ...]`.
When running JVM tests (backend, common):
- For backend, prefer `./scripts/ci --test backend` from the repo root. It adds `enable-backend-asserts`, without which Clojure asserts compile out. When invoking backend tests directly, including with `--focus`, add the flag to any existing flags: `PENPOT_FLAGS="${PENPOT_FLAGS:+$PENPOT_FLAGS }enable-backend-asserts" clojure -M:dev:test`.
- For common JVM tests, run `clojure -M:dev:test` from `common/`.
- Same file-piping rule applies.
## Verification Checklist
After completing any implementation:
- [ ] Every new behavior has a corresponding test
- [ ] All tests pass for touched modules
- [ ] Bug fixes include a reproduction test that failed before the fix
- [ ] Test names describe the behavior being verified
- [ ] No tests were skipped or disabled
- [ ] Lint/formatter passes for touched modules
- [ ] New test files registered in the module's runner/entrypoint (see module testing memory)
- Never pipe test output directly to filters. Redirect it to a file first, inspect the full result and exit status, then search the file if needed. Use focused runs to narrow scope, not output filters to hide failures.
- Run the affected modules' tests, lint and format checks; use `mem:scripts/ci` for the local CI entry point. A focused test does not replace the required module checks. Register new test namespaces where the module's runner requires it.
- For backend direct and focused JVM runs, preserve existing `PENPOT_FLAGS` and append `enable-backend-asserts` before the process starts; otherwise assertions can compile out. Prefer `./scripts/ci backend` for the full checks.