---
title: Testing
summary: Tests are how an agent proves its work, and how you keep it from breaking yesterday's work.
section: quality
phase: verify
order: 1
last_reviewed: 2026-11-20
---

# Testing

When an agent writes the code, tests become your main way to check it. A good test suite lets an agent work autonomously, because it can check itself.

## You'll be able to
- Know which kinds of tests to ask for, and how many
- Use tests to drive agent work (test-first prompting)
- Spot tests that pass without actually testing anything

## The mental model
The **testing trophy / pyramid**: lots of fast checks at the bottom (types, lint), a solid middle of **integration tests** (real database, real HTTP), and a few **end-to-end** tests covering critical user flows. Each layer catches different bugs.

## Key concepts
- **Static checks:** TypeScript and linters (ESLint/Biome, Ruff). They're free, instant and catch whole classes of bugs.
- **Unit tests:** A single function in isolation. Best for pure logic (pricing, parsing, permissions).
- **Integration tests:** Several pieces working together, such as an API route plus a real test database. **The best value for most apps.**
- **End-to-end (E2E):** A real browser clicking through the app (Playwright). Slow but realistic. Reserve these for critical paths like signup, checkout and login.
- **Mocks / fakes:** Stand-ins for external services. Overusing mocks produces tests that test the mocks.
- **Fixtures / factories:** Reusable test data builders.
- **Coverage:** The percentage of lines executed by tests. Treat it as a hint, not a goal.
- **Regression test:** A test written for a bug, so the bug can never come back.
- **TDD with agents:** Write (or have the agent write) failing tests from the spec first, then let the agent implement until they pass.
- **Evals:** The equivalent of tests for LLM-powered features: graded examples of model behaviour.

## Decisions you make (not the agent)
| Decision | Sensible default | Choose otherwise when… |
|---|---|---|
| Runner | Vitest (JS/TS), pytest (Python) | Existing Jest setup → keep it |
| E2E | Playwright for 3–10 critical flows | — |
| DB in tests | Real Postgres (Docker/Testcontainers or a branch DB) | Pure logic → no DB |
| CI | All checks block merge | — |
| Bug workflow | Reproduce with a failing test first, then fix | — |

## What good output looks like

- Tests describe behaviour in plain names and check results, not implementation details
- Every test would fail if the behaviour it covers were broken
- Edge cases and boundaries (the exact limit, empty input, one past the limit) are tested
- Integration tests use a real database, and end-to-end tests cover only the critical flows
- No skipped tests, no assertions that can't fail

## Red flags
- Tests that assert the mock was called and nothing else
- `expect(true).toBe(true)`, or snapshot tests nobody reads
- The agent "fixing" a failing test by changing the assertion to match the buggy output
- Skipped tests (`.skip`, `xit`) appearing in a diff
- No tests for permission and ownership rules
- **Common agent mistakes:** a test that recomputes the formula it is testing, so it agrees with the code even when the code is wrong; only "typical" values, with no boundary cases; end-to-end tests that use fragile CSS selectors and fixed waits (Playwright recommends user-facing locators such as roles, and assertions that wait); tests that call a real third-party service that you don't control

## Worked example

**Task:** Add a rule: orders of **100 or more** get a 10% discount.

You prompt: *"Add a discount for orders of 100 or more, and write tests."* The agent writes `if (total > 100)` and tests with totals of 50 and 150. Both pass. Coverage is 100%.

**What a good result contains:** the comparison written as the spec says (`>= 100`), and tests at the boundary: exactly 100, just under (99.99) and just over, plus zero and negative input.

**The wrong turn:** the code is wrong at exactly 100, and the tests can't tell. Coverage measures that lines ran, not that anything was checked. Mutation testing makes the problem visible: a tool such as Stryker changes the code (turning a boundary comparison into something else) and reruns the tests. If every test still passes, the mutant "survived", which means the tests aren't covering that code well. A surviving boundary mutant points straight at the missing boundary test.

**How review catches it:** ask *what does each test check that would break if the rule were wrong?* Try the sabotage by hand: change `>=` to `>` and run the tests. Nothing fails, so add the boundary tests. For important logic, run a mutation tool and read the survivors. Re-prompt for tests from the spec first, including every boundary.

## Sample prompts

### Weak vs strong
```compare
## Weak
Write tests for the discount function.
## Why it fails
It doesn't say what the rules are, which cases to cover or how to know the tests are any good. The agent reads the code and writes tests that agree with it, bugs included.
## Strong
Before implementing <feature>, write failing tests from this spec: <spec>.
Cover the happy path, validation errors, permission denials, and edge cases
(<list>). Show me the tests and wait. After I approve, implement until all tests
pass without modifying the tests.
## Why it works
The tests become the spec, and "without modifying the tests" stops the agent from cheating.
## What changed
- **Spec first**: tests come from the rules, not from the existing code
- **Cases** are named: happy path, validation errors, permission denials and edge cases
- **Review point**: the agent stops so you can read the tests
- **No editing tests** to get them green
```

```text
Set up testing for this project: Vitest for unit/integration with a real Postgres
via Testcontainers, Playwright for E2E of signup → create project → invite
member, and a GitHub Actions workflow that runs type-check, lint, and all tests
on every PR.
```
*Why it works:* It names the runner, the database approach and the critical flow for each layer, and puts all of it into CI, so the tests protect every future change.

### Find tests that can't fail
```text
Review the tests for <module>. For each test, say what behaviour it would catch if broken, and whether it would still pass if the implementation were wrong. Flag tests that only assert a mock was called, recompute the code's own formula, or check things that cannot fail. Then list the boundaries and edge cases that have no test. Don't change anything yet.
```
*Why it works:* It asks the question that coverage can't answer: would this test notice a bug?

### Sabotage the code on purpose
```text
For <module>, make five small deliberate breakages one at a time (for example change >= to >, flip a condition, remove a check, return early, change a constant). After each one, run the tests and record whether anything failed, then revert it. Report which breakages no test caught and write the tests that would catch them.
```
*Why it works:* This is mutation testing done by hand. A breakage that no test notices is a hole in the suite.

### Fix a flaky browser test properly
```text
This end-to-end test is flaky: <paste>. Rewrite it to use user-facing locators (by role or visible text) instead of CSS selectors or XPath, replace fixed waits with assertions that wait for the expected state, and make the test independent of other tests by creating its own data. Explain what made it flaky.
```
*Why it works:* Playwright's best practices recommend exactly these changes: user-facing locators, assertions that wait, and isolated tests.

## Check your understanding

```quiz
Q: A suite has 100% line coverage. What does that tell you?
- [ ] The code is correct
- [x] Every line ran during the tests, but not that the tests check anything meaningful
- [ ] There are no bugs at the boundaries
> Why: Coverage measures execution. Tests that assert too little, or only typical values, can run every line and still miss bugs.

---

Q: In mutation testing, what does a surviving mutant mean?
- [ ] The code is well tested
- [x] A change to the code didn't make any test fail, so that behaviour probably isn't tested well
- [ ] The tool failed
> Why: A mutant is a small deliberate change. If every test still passes, the tests can't tell the changed code from the original.

---

Q: Which locator is preferable in an end-to-end test?
- [ ] A long CSS selector copied from the browser
- [x] A user-facing one, such as the element's role and visible name
- [ ] An XPath expression
> Why: Playwright's guidance is to prefer locators that reflect how users find things, which survive restyling.

---

Q: You tell the agent to implement until the failing tests pass. What must you add?
- [ ] Nothing
- [x] "Don't modify the tests", so it can't make them pass by changing the assertions
- [ ] "Be quick"
> Why: Otherwise the easiest path to green can be to edit the test to match the buggy output.
```

## Checklist
- [ ] Type-check and lint run in CI
- [ ] Integration tests cover API routes, including auth and ownership
- [ ] E2E tests cover the critical flows
- [ ] Every fixed bug has a regression test
- [ ] Boundaries (the exact limit, one below, one above) are tested
- [ ] I have checked that the tests can fail, by sabotaging the code or running a mutation tool
- [ ] End-to-end tests use role-based locators and assertions that wait

## Go deeper
- [Kent C. Dodds: The Testing Trophy](https://kentcdodds.com/blog/the-testing-trophy-and-testing-classifications)
- [Playwright docs](https://playwright.dev)
- [Vitest docs](https://vitest.dev)
- [Playwright: Best practices](https://playwright.dev/docs/best-practices): user-facing locators, isolation and assertions that wait
- [Stryker: What is mutation testing?](https://stryker-mutator.io/docs/): killed and surviving mutants
- [Reviewing and verifying AI output](/docs/ai-agents/reviewing-ai-output.md): checking that tests can fail
- [Building with LLM APIs](/docs/ai-agents/building-with-llm-apis.md): evals, the tests for model behaviour
