Programming & DevelopmentIntermediate
Write Unit Tests for a Function
Generate a focused set of unit tests covering normal cases, edge cases, and errors, in your testing framework of choice.
Make it yours
Fill in each variable and watch the prompt update. Anything still in {{braces}} needs your input before you use it.
Fill in the blanks
0 of 5 filledProgramming language.
Testing library you use.
What the function should do, in your words.
Team conventions or extra requirements.
The function to test.
You are a meticulous software engineer who writes clear, maintainable tests. Write unit tests for the {{language}} function below using {{test_framework}}. What the function is supposed to do: {{intended_behavior}} Steps: 1. First, list the test cases you plan to cover as a short checklist, grouped into: normal cases, edge cases (empty, boundary, unusual input), and error cases. 2. Then write the tests. Rules: - Test the intended behavior I described, not just what the code currently does. If the code seems to disagree with the intended behavior, write the test for the intended behavior and point out the mismatch. - One behavior per test, with descriptive test names. - No external network or file access; mock anything like that. - {{style_notes}} Function: """ {{code}} """
Example
Example input
You are a meticulous software engineer who writes clear, maintainable tests.
Write unit tests for the TypeScript function below using Vitest.
What the function is supposed to do: Converts a title into a lowercase URL slug: words separated by single hyphens, no leading/trailing hyphens, and only letters and numbers kept. Throws an error for an empty or whitespace-only title.
Steps:
1. First, list the test cases you plan to cover as a short checklist, grouped into: normal cases, edge cases (empty, boundary, unusual input), and error cases.
2. Then write the tests.
Rules:
- Test the intended behavior I described, not just what the code currently does. If the code seems to disagree with the intended behavior, write the test for the intended behavior and point out the mismatch.
- One behavior per test, with descriptive test names.
- No external network or file access; mock anything like that.
- Use describe/it blocks and keep each test under 5 lines.
Function:
"""
export function slugify(title: string): string {
return title
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-|-$/g, "");
}
"""Example output
Planned cases
- Normal: simple title; multiple words; numbers kept
- Edge: extra spaces; punctuation; leading/trailing symbols
- Error: empty string; whitespace only
import { describe, it, expect } from "vitest";
import { slugify } from "./slugify";
describe("slugify", () => {
it("lowercases and hyphenates words", () => {
expect(slugify("Hello World")).toBe("hello-world");
});
it("keeps numbers", () => {
expect(slugify("Top 10 Tips")).toBe("top-10-tips");
});
it("collapses repeated spaces and punctuation", () => {
expect(slugify("Wait... what?!")).toBe("wait-what");
});
it("trims leading and trailing symbols", () => {
expect(slugify("--Intro--")).toBe("intro");
});
it("throws for an empty title", () => {
expect(() => slugify("")).toThrow();
});
it("throws for a whitespace-only title", () => {
expect(() => slugify(" ")).toThrow();
});
});
Mismatch found: The intended behavior says empty or whitespace-only titles should throw, but the current code returns "". The last two tests will fail until a guard like if (!title.trim()) throw new Error("Title is required"); is added.
Why it works
- Planning cases before writing code (a light form of prompt chaining) produces broader coverage than asking for tests directly, especially for edge and error cases.
- Describing intended behavior separately from the code is the key move: otherwise the AI tends to write tests that simply mirror the current implementation, bugs included.
- Naming the framework and style conventions means the output drops into your project with little editing.
Techniques used
- Instruction promptingWrite clear, explicit, positive instructions — action, scope, constraints, format — so the AI knows exactly what done looks like.Learn the technique
- Prompt chainingBreak a big task into smaller prompts where each step's output feeds the next, for better control and quality.Learn the technique
- Role promptingAsk the AI to respond as a specific role or expert to shape its focus, vocabulary, and tone for your audience.Learn the technique