Writing Tests

Ask Claude to generate unit tests, including edge cases you might not think of.

Claude can write tests for your functions and components, covering the happy path and the edge cases. Good tests give you the confidence to refactor and ship.

What to tell Claude

  • The test framework — Jest, Vitest, Playwright, etc.
  • The code under test — paste the function or component.
  • What matters — "cover empty input, negative numbers, and very large lists".

Ask for edge cases

A useful prompt is: "list the edge cases first, then write a test for each". Claude often surfaces cases you would have missed.

Example

Example · javascript
const prompt = `Write Vitest unit tests for this function.
First list the edge cases you will cover, then write a test for each.

export function slugify(text) {
  return text.trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
}`;

const response = await client.messages.create({
  model: "claude-opus-4-8",
  max_tokens: 3072,
  messages: [{ role: "user", content: prompt }],
});

When to use it

  • Generate Vitest unit tests for a price-calculation utility including edge cases like zero and negative inputs.
  • Ask Claude to add integration tests for an Express route that reads from a database, using mock data.
  • Have Claude write accessibility tests for a custom dropdown component using Testing Library queries.

More examples

Generate unit tests for a utility

Asks for Vitest tests specifically and requests edge-case coverage beyond the happy path.

Example · javascript
const fn = `
function clamp(value, min, max) {
  return Math.min(Math.max(value, min), max);
}`;

const msg = await client.messages.create({
  model: "claude-sonnet-4-5",
  max_tokens: 512,
  messages: [{ role: "user",
    content: `Write Vitest unit tests for this function, including edge cases:\n${fn}` }],
});

Tests for an async fetch helper

Specifies the mocking strategy (global.fetch) and the cases to cover so tests are immediately runnable.

Example · javascript
const helper = `
export async function fetchUser(id) {
  const res = await fetch(\`/api/users/\${id}\`);
  if (!res.ok) throw new Error('Not found');
  return res.json();
}`;

const msg = await client.messages.create({
  model: "claude-sonnet-4-5",
  max_tokens: 768,
  messages: [{ role: "user",
    content: `Write Jest tests for this async function. Mock global.fetch. Cover 200 and 404 cases:\n${helper}` }],
});

Ask for tests with coverage checklist

Providing a numbered checklist of cases forces Claude to write tests for each specific scenario.

Example · javascript
const prompt = `Here is my validateEmail(str) function.
Write Vitest tests covering:
1. Valid address returns true
2. Missing @ returns false
3. Empty string returns false
4. Multiple @ signs returns false`;

const msg = await client.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 512,
  messages: [{ role: "user", content: prompt }],
});

Discussion

  • Be the first to comment on this lesson.