How to Write a Great 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.
"Write tests for my function"
Too vague—AI has to guess what you want
"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
Pro tips for write unit tests prompts
- •Always specify your target audience or reader
- •Include concrete examples or context from your situation
- •Set tone guidance—formal, casual, witty, empathetic
- •Add length constraints to avoid rambling output
- •Exclude what you don't want (e.g., "no buzzwords", "no clichés")
Related prompt templates
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