c/agents › command
0votes

Claude Code subagent: test writer

TemplateLow risk Tested
submitted by CommandVault editors to c/agents · 3 uses

A .claude/agents/test-writer.md subagent that writes focused tests in your project's style.

Template · 3 variables
---
name: test-writer
description: Writes and fixes tests for recently changed code. Use after implementing a feature or fixing a bug.
tools: Read, Grep, Glob, Edit, Write, Bash
---

You write tests for {{project_name}} using {{test_framework}}.

Process:
1. Find what changed: `git status --short` and `git diff --name-only HEAD` (new, untracked files only show in git status). If the project has no test runner yet, say so and propose the smallest setup before writing tests.
2. Read the changed code and the nearest existing tests to copy their style, helpers and fixtures.
3. Write tests for: the main behavior, each error path, and one edge case per input.
4. Run `{{test_command}}`. If a test fails because the code is wrong, report it; don't change the code.
5. Never weaken an assertion or skip a test to make it pass.

Return: the tests you added, the command output, and any bugs you found.
0 commentsreport

Guide

What you get: A test writer that adds focused tests for changed code, runs them, and reports bugs it finds instead of changing the code. Best for Developers who just wrote or changed a function and want tests for it..

Before you start

  • The code to test is committed or staged (step 1 uses `git diff --name-only HEAD`)
  • A test command; with no test framework installed, Node's built-in `node --test` works on Node 22.18+/24 for .ts files
  • Fill in project_name, test_framework and test_command

Worked example (Oct 17, 2026, Claude Code 2.1.296; Codex 0.151.0 (gpt-5.6-sol))

Input

New pure function src/lib/truncate.ts (staged, no tests). The project has no test runner, so blanks were filled with test_framework = "node:test (Node's built-in test runner; no test framework is installed)" and test_command = `node --test "src/**/*.test.ts"`. Request: "Use the test-writer subagent to write tests for src/lib/truncate.ts."

Result

Claude Code: the test-writer subagent added src/lib/truncate.test.ts with 30 node:test tests, installed nothing and left truncate.ts unchanged. My run: 28 pass, 2 fail, and both failures are real bugs it reported: truncate("hello world foo", 12) returns "hello…" although "hello world…" fits, and emoji can be split in half. Codex: the test-writer agent added 9 tests; my run: 8 pass, 1 fail (the same emoji bug); truncate.ts unchanged.

Test project: fresh create-next-app (Next.js 16.4.2, TypeScript), Node 24.14.1. Blanks filled with project_name = "demo-app (Next.js 16 + TypeScript)". Claude Code: body saved as .claude/agents/<name>.md, run headless with `claude -p "Use the <name> subagent to ..." --permission-mode acceptEdits`. Codex: converted to .codex/agents/<name>.toml (name, description, developer_instructions) and run with `codex exec -m gpt-5.6-sol -s workspace-write "Use the <name> agent to ..."`. Subagent use was confirmed from Claude Code's stream-json log (Agent call with subagent_type) and from Codex session logs (spawn_agent with agent_type, and the agent's instructions present in the child session). Claude Code allowed tools: Bash(node *), Bash(git diff *), Bash(git status *). The failing tests are intentional: the template says to report code bugs, not change the code. `npx tsc --noEmit` rejects the test's `./truncate.ts` import (TS5097) because tsconfig lacks allowImportingTsExtensions; Claude's subagent warned about this, and I confirmed it. After this test step 1 also checks git status for new files, and it asks before adding a test runner.

How to check it worked

  • Run the test command yourself and read any failures: decide whether the test or the code is wrong
  • Check `git diff` shows no change to the code under test
  • Check there is at least one test per error path

Common problems

  • With node:test and TypeScript, importing `./file.ts` works in Node but fails `tsc`/`next build` type checks unless tsconfig allows it or excludes test files
  • A brand-new file that is not staged or committed is invisible to step 1 (`git diff --name-only HEAD`), so name the file in your request
  • Coverage depth varies: one run wrote 30 tests, the other 9

Add this subagent to your AI tool

Converted for each tool's agent format. Fill in the blanks above first.

One command
npx commandvault add claude-code-subagent-test-writer-dd7b50c1 --tool claude
Save as .claude/agents/test-writer.md
---
name: test-writer
description: Writes and fixes tests for recently changed code. Use after implementing a feature or fixing a bug.
tools: Read, Grep, Glob, Edit, Write, Bash
---

You write tests for {{project_name}} using {{test_framework}}.

Process:
1. Find what changed: `git status --short` and `git diff --name-only HEAD` (new, untracked files only show in git status). If the project has no test runner yet, say so and propose the smallest setup before writing tests.
2. Read the changed code and the nearest existing tests to copy their style, helpers and fixtures.
3. Write tests for: the main behavior, each error path, and one edge case per input.
4. Run `{{test_command}}`. If a test fails because the code is wrong, report it; don't change the code.
5. Never weaken an assertion or skip a test to make it pass.

Return: the tests you added, the command output, and any bugs you found.

Use it: Claude delegates to it when a task matches, or ask: "Use the test-writer subagent to …".

From Claude Code's docs , checked Oct 17, 2026.

More from c/agents

See all of c/agents →
0 comments

Log in to join the discussion.