← All templates

Web Development

Unit Test Writer

Ask a coding agent to cover behavior, edge cases, and failure paths using your existing test setup.

EXAMPLE

From prompt to result

Sample prompt

Built from example inputs for Claude Code. Choose “Try an example” above to load these inputs and adjust them.

# Unit Test Writer

Act as a test engineer. Write unit tests that describe the intended behavior of the code, including edge cases and failure paths.

## Task details

**Code to test**
> parseDuration(text) in src/time.js turns strings like "1h 30m" or "45s" into seconds and throws a RangeError on invalid input.

**Test framework**
> Vitest

## Expected output

Provide the test file, a list of covered behaviors, any bugs or ambiguous behavior found, and the command to run the tests.

## Quality checks

Follow the project’s existing test conventions. Test behavior rather than implementation details, avoid flaky timing, and never change production code just to make a test pass.

## Working in Claude Code

If a working project is available, read its instructions and nearby code first. Make focused changes, follow existing conventions, and run relevant checks. State any verification gaps.

Sample result

An illustrative answer written by xuvo. to show the format and depth to expect. It is not live AI output, and names, figures and sources are placeholders. Always review real AI output.

Test file: src/time.test.js

import { describe, it, expect } from 'vitest';
import { parseDuration } from './time.js';

describe('parseDuration', () => {
  it('parses hours and minutes', () => {
    expect(parseDuration('1h 30m')).toBe(5400);
  });
  it('parses seconds only', () => {
    expect(parseDuration('45s')).toBe(45);
  });
  it('ignores extra whitespace', () => {
    expect(parseDuration('  2m   5s ')).toBe(125);
  });
  it.each(['', 'abc', '5x', '-3m'])('throws RangeError for %j', input => {
    expect(() => parseDuration(input)).toThrow(RangeError);
  });
});

Covered behavior

Combined units, a single unit, whitespace handling, and invalid input.

Ambiguous behavior found

parseDuration('90m') returns 5400. Should minutes above 59 be allowed? I left this untested, pending a decision.

Run: npx vitest run src/time.test.js

What you’ll get

Provide the test file, a list of covered behaviors, any bugs or ambiguous behavior found, and the command to run the tests.

Built-in quality checks

Follow the project’s existing test conventions. Test behavior rather than implementation details, avoid flaky timing, and never change production code just to make a test pass.

Works with

Codex, Claude Code, ChatGPT, Claude, Gemini, Universal / Other

See all