Home/Prompt Templates/Write Unit Tests
CodingClaude

AI Prompt to Write Unit Tests

Most people ask AI to “Write tests for my function”—and wonder why the output is mediocre. Here's how to write a prompt that actually gets you what you want on the first try.

Last updated · By the Prompt Orange team

Before

Write tests for my function

Too vague—AI has to guess what you want

After

Write Jest unit tests for the following JavaScript function. Cover: happy path, edge cases (empty input, null, unexpected types), and one integration scenario. Use describe/it blocks with descriptive test names. Mock any external dependencies. Add a comment above each test explaining what it validates.

Specific, clear, ready to use

What makes the strong prompt better?

Specifies the testing framework so the syntax is immediately usable

Lists the types of cases to cover so no gaps are left

Requires descriptive names and explanatory comments for maintainable tests

Related prompt templates

Frequently asked questions

What is the best AI prompt to write unit tests?

+
A great prompt to write unit tests is specific about the output you want. Use this template as a starting point: "Write Jest unit tests for the following JavaScript function. Cover: happy path, edge cases (empty input, null, unexpected types), and one integration scenario. Use describe/it blocks with descriptive test names. Mock any external dependencies. Add a comment above each test exp..." Adjust the specifics — audience, tone, length — to match your situation.

Which AI tool works best to write unit tests?

+
Claude handles this prompt well, but the same structure works in Claude, Gemini, and other modern LLMs. The reason it works isn't the tool — it's the level of context and constraint in the prompt itself.

Why does my AI unit tests come out generic?

+
Generic output almost always traces back to a vague prompt like "Write tests for my function". To fix it: add audience, tone, length, examples to follow, and things to avoid. The strong prompt above shows each of those in action.

Can I use this prompt commercially?

+
Yes — these are templates, not protected content. Adapt them freely for client work, internal use, or production tools. Output rights depend on the AI tool's terms (ChatGPT, Claude etc each set their own).

Ready to build your perfect prompt?

Try Prompt Orange free—get a scored, refined prompt in under 2 minutes. No signup required.

Build my prompt now
Get started free