# Let the Agent Write the Tests (and the Fixes)
<!--
ref: @content/youtube/yifan-bth/let-the-agent-write-the-tests-and-the-fixes
tags: [agentic testing, end-to-end tests, Playwright, Claude Code hooks, CLAUDE.md, self-debugging agents, test quality]
version: 2
updated_at: 2026-11-24T07:20:17.684Z
-->

**[Watch on YouTube](https://www.youtube.com/watch?v=LEFFnVsY_t8)**

# Let the Agent Write the Tests (and the Fixes)

*Synthesized from Yifan on Beyond the Hype, 2025-09-08.*

Yifan, of the Beyond the Hype channel, shows how he got Claude Code to write, run and debug end-to-end tests on his own GitHub repo-health site, which runs on Next.js, Supabase and Cloudflare Workers.

## Core Thesis
Yifan argues that agents become autonomous only when they can write end-to-end tests that mirror real user behaviour and then read the failure output themselves. He gets there in three steps: make the agent write and run tests consistently, make the tests high quality, and make failures debuggable.

## Key Insights
- **The baseline problem.** Across 100 tasks, Claude Code proactively wrote tests only 10 times, and he saw similar results with Cursor and Windsurf.
- **Consistency through instructions and hooks.** A one-line project description, the daily test commands and the workflow in CLAUDE.md raised test writing to about 50 to 60% of tasks. A Stop hook, which fires before Claude Code returns to the user and receives the transcript, can block the stop and send the agent back to write or run tests. He built this into his Claude Code Boost tool.
- **Rules that the system prompt does not enforce.** He notes that code generated by the agent still carries many comments, despite system instructions against them, so instructions need reinforcing with hooks.
- **Playwright quality rules.** Prefer user-facing locators, avoid brittle locators unless necessary, and do not run tests in parallel when they share state, because correctness matters more than speed.
- **Review command.** A custom slash command (a review-tests prompt in the commands folder) runs a review pass against the testing checklist, with good and bad examples. He still recommends checking the tests by hand.
- **Debugging settings.** Set reuseExistingServer to false with a fixed port, so server logs interleave with test logs. Keep screenshots and traces only on failure. The error-context file gives browser console output and the page's accessibility tree, so the agent can see what rendered.
- **Example.** During a profile-page refactor, the agent used the error-context file to find that an authenticated user was being redirected to login.

## Actionable Takeaways
1. Put a one-line project description, the day-to-day test commands and the required test workflow in CLAUDE.md.
2. Add a Stop hook that blocks the agent from finishing until tests were written and run.
3. Write rules for user-facing locators and serial test runs, and include good and bad examples.
4. Configure Playwright to keep server logs in the same output, and keep screenshots and traces only on failure.
5. Point the agent at the full Playwright output and the error-context file whenever a test fails.

## Notable Quotes
> "Are you also tired of being your agent's clipboard?" — Yifan

> "By default, agents don't really write tests unless you instruct them to." — Yifan

---

*Related: `@content/youtube/yifan-bth/how-to-build-robust-applications-with-cursor-ai-playwright` · `@content/youtube/yifan-bth/5x-your-cursor-ai-coding-quality-with-these-pro-tips` · `@content/youtube/yifan-bth/the-complete-claude-code-workflow-90-skip-this` · `@content/youtube/yifan-bth/my-day-to-day-ai-dev-workflow-git-cursor-windsurf-cline-playwright-supabase-replit` · `@content/youtube/langchain/agent-that-can-browse-the-web-with-managed-deep-agents-and-browserbases-stagehand`*

---

*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*

*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*