Write thorough unit tests for a function or component
FreeA fill-in prompt that turns any function or component into a runnable unit test suite covering happy paths, edge cases, boundaries, and error handling in your chosen framework.
A complete, runnable unit test suite for your code — with edge cases, boundaries, and error paths covered, plus a map of exactly what is and isn't tested.
This prompt
You are a senior software engineer who specializes in automated testing and writes test suites that reviewers approve without changes.
Your task: write a thorough, runnable unit test suite for the code below. The suite succeeds when it (a) runs without edits in the target framework, (b) covers every meaningful behavior and branch, and (c) fails loudly if the code's behavior later regresses.
Code under test — treat everything between the fences as the exact source and do not modify it:
```{{language}}
{{code}}
```
Target language: {{language}}
Test framework: {{test_framework}}
Extra context (dependencies, conventions, intended behavior, cases to prioritize): {{additional_context}}
Follow these steps:
1. Restate in one or two sentences what the code is supposed to do, and name its inputs and outputs.
2. List the distinct behaviors and branches to test: the happy path(s), boundary values, empty/zero/null inputs, invalid inputs and error handling, and any async, state, or side-effect behavior.
3. Where the intended behavior is ambiguous, write down the assumption you are testing against instead of guessing silently. If a case genuinely can't be determined from the code and context, say so rather than inventing a passing assertion.
4. Write one focused test per behavior, and give each a descriptive name that states the input condition and the expected result.
5. Assert on observable outputs and behavior — return values, thrown errors, rendered output, calls made to dependencies — rather than on private internals.
6. Replace external dependencies (network, current time, randomness, filesystem, databases) with mocks, fakes, or fixtures so each test is deterministic and isolated.
7. For UI components or async code, wait for the code to settle — rendered content present, promises resolved, pending requests idle — before asserting, so the tests don't flake.
Produce your output in this order:
1. The complete test file in {{test_framework}}, ready to run, including imports and any setup/teardown.
2. A coverage map: a short table listing each behavior and the test that covers it.
3. Assumptions you made and any behaviors you could not test, each with a one-line reason.
Constraints:
- Keep tests independent — each one passes on its own and in any order.
- Use clear, literal test data so a reader can see the expected result at a glance.
- Give the failure and edge cases the same care as the happy path.
- Match the naming and structure conventions in the extra context when they are provided.6 fields still empty — the {{like_this}} slots fill in as you type.
See it in action
What this prompt produces with example details filled in:
**What the code does:** `applyDiscount(price, percentOff)` takes a non-negative price and a percentage from 0–100, returns the price reduced by that percentage, rounded to 2 decimal places, and throws a `RangeError` for out-of-range inputs.
**Test file — `applyDiscount.test.ts`:**
```ts
import { describe, it, expect } from 'vitest';
import { applyDiscount } from './applyDiscount';
describe('applyDiscount', () => {
// Happy path
it('applies a standard percentage to a whole price', () => {
expect(applyDiscount(100, 25)).toBe(75);
});
it('rounds the result to the nearest cent', () => {
// 9.99 * 0.85 = 8.4915 -> 8.49
expect(applyDiscount(9.99, 15)).toBe(8.49);
});
// Boundaries
it('returns the full price when percentOff is 0', () => {
expect(applyDiscount(49.95, 0)).toBe(49.95);
});
it('returns 0 when percentOff is 100', () => {
expect(applyDiscount(80, 100)).toBe(0);
});
it('returns 0 when the price is 0', () => {
expect(applyDiscount(0, 40)).toBe(0);
});
// Invalid input / error handling
it('throws RangeError when price is negative', () => {
expect(() => applyDiscount(-1, 10)).toThrow(RangeError);
});
it('throws RangeError when percentOff is below 0', () => {
expect(() => applyDiscount(100, -5)).toThrow(RangeError);
});
it('throws RangeError when percentOff exceeds 100', () => {
expect(() => applyDiscount(100, 150)).toThrow(RangeError);
});
});
```
**Coverage map:**
| Behavior | Covered by |
|---|---|
| Standard discount applied | applies a standard percentage to a whole price |
| Result rounded to 2 decimals | rounds the result to the nearest cent |
| 0% -> full price | returns the full price when percentOff is 0 |
| 100% -> zero | returns 0 when percentOff is 100 |
| Zero price | returns 0 when the price is 0 |
| Negative price rejected | throws RangeError when price is negative |
| percentOff below range rejected | throws RangeError when percentOff is below 0 |
| percentOff above range rejected | throws RangeError when percentOff exceeds 100 |
**Assumptions and gaps:**
- Assumed `Math.round` half-up rounding is the intended behavior, so `8.4915 -> 8.49`; I tested one rounding case rather than exhaustive floating-point values.
- Did not test `NaN` or non-numeric inputs — the signature is typed `number`, and the code has no guard for `NaN`. Flagging it as a potential gap if untyped callers exist.Tips
- Paste the exact source — the more real the code, the more specific the tests. Trim unrelated code so the model focuses on one unit.
- Name your framework precisely (Vitest vs Jest, pytest vs unittest) so imports and matchers come out runnable.
- Use the extra-context slot to state intended behavior for anything the code doesn't make obvious — it turns silent guesses into named assumptions.
- Run the generated suite once and paste any failures back in; real failures often reveal a genuine ambiguity in the code, not just a bad test.
- For components with heavy dependencies, list what to mock (API client, clock, router) in the extra context so tests stay deterministic.
Built by Web Innoventix
Want the work done, not just prompted? We design, build and rank websites that get found on Google and cited by AI.
Get a free quoteMore prompts
Browse all →Build a React component from a description
Turn a plain-English description into a production-ready, accessible React + Tailwind component with a usage example.
Senior code review that catches real bugs
Paste a snippet or PR diff and get a senior-engineer review: real issues ranked by severity, each with the exact input that breaks it and a concrete fix — not a wall of style nitpicks.
Debug an error by root cause, not guesswork
Paste an error and its context to get a traced root-cause diagnosis, a fix that addresses the real cause, and a concrete way to verify the bug is gone.
Refactor a messy function into clean, maintainable code
A copy-paste ChatGPT prompt that refactors a messy function into clean, maintainable code without changing its behavior — it audits the code, rewrites it, explains each change, and suggests tests to prove nothing broke.
Build a regex from a plain-English description (with test cases)
A fill-in-the-blank ChatGPT prompt that turns a plain-English rule into a working regex for your exact language — with a line-by-line breakdown and a pass/fail table of positive and negative test cases.
Write a clear, conventional git commit message from a diff
A fill-in prompt that turns any git diff into a clean, convention-compliant commit message with a tight subject line and a what-and-why body a reviewer can trust.

