Unit Test Generator
Generate comprehensive unit tests with edge cases, mocks, and full coverage for your code.
Prompt Template
You are a QA engineer who specializes in writing thorough, maintainable unit tests. Generate comprehensive tests for the following code. **Language/Framework:** [e.g., TypeScript/Jest, Python/pytest] **Testing framework:** [Jest / Vitest / pytest / etc.] **Code to test:** ``` [paste your code here] ``` **What it does:** [brief description] **External dependencies:** [APIs, databases, file system, etc. that need mocking] Generate tests covering: 1. **Happy path** — normal expected usage (2-3 tests) 2. **Edge cases** — empty inputs, boundary values, null/undefined, max values 3. **Error cases** — invalid inputs, network failures, timeout scenarios 4. **Integration points** — mock external dependencies properly 5. **Type-specific** — type coercion issues (JS), None checks (Python), etc. For each test: - Use descriptive test names: `it('should return empty array when no items match filter')` - Follow AAA pattern: Arrange, Act, Assert - One assertion per test where practical - Include setup/teardown if needed Also provide: - Mock setup for external dependencies - Test data factory/fixtures - Coverage analysis: which lines/branches are covered vs. missing - Suggested edge cases I might not have considered
Example Output
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { UserService } from './user-service';
import { db } from './database';
vi.mock('./database');
describe('UserService', () => {
let service: UserService;
beforeEach(() => {
service = new UserService();
vi.clearAllMocks();
});
describe('findByEmail', () => {
it('should return user when email exists', async () => {
const mockUser = { id: '1', email: 'test@example.com', name: 'Test' };
vi.mocked(db.users.findFirst).mockResolvedValue(mockUser);
const result = await service.findByEmail('test@example.com');
expect(result).toEqual(mockUser);
expect(db.users.findFirst).toHaveBeenCalledWith({
where: { email: 'test@example.com' },
});
});
it('should return null when email does not exist', async () => {
vi.mocked(db.users.findFirst).mockResolvedValue(null);
const result = await service.findByEmail('nonexistent@example.com');
expect(result).toBeNull();
});
it('should throw on invalid email format', async () => {
await expect(service.findByEmail('not-an-email')).rejects.toThrow(
'Invalid email format'
);
});
});
});
Tips for Best Results
- 💡Include the full function with its imports so the AI knows what to mock
- 💡Mention any existing test utilities or factories in your codebase
- 💡Ask for property-based tests (fast-check/hypothesis) for complex logic
- 💡Request mutation testing suggestions to verify test quality
Frequently Asked Questions
What is the Unit Test Generator prompt?
Generate comprehensive unit tests with edge cases, mocks, and full coverage for your code. It's a free ChatGPT prompt template from our Coding collection — copy it, fill in the bracketed variables, and paste it into your AI tool.
Which AI tools work with this prompt?
It's written and tested for ChatGPT, Claude and Gemini. Any AI assistant that accepts free-form text prompts will handle it well.
How do I customize this ChatGPT prompt?
Replace the bracketed variables — such as [e.g., TypeScript/Jest, Python/pytest], [Jest / Vitest / pytest / etc.], [paste your code here] — with your own details before running it. Include the full function with its imports so the AI knows what to mock
Is this prompt free to use?
Yes. Every prompt on PromptAtlas is free to copy, customize, and use — no signup required.
Related Prompts
npm ERESOLVE Error Debugging Prompt
Debug an npm ERESOLVE dependency-tree error from the exact command, peer-dependency conflict, package metadata, runtime versions, lockfile state, and minimal reproduction.
Next.js Hydration Error Debugging Prompt
Debug a Next.js hydration error from the exact server HTML, first client render, component boundary, versions, console output, and minimal reproduction.
Python ModuleNotFoundError Debugging Prompt
Debug Python ModuleNotFoundError from the exact traceback, interpreter, environment, import path, project layout, packaging metadata, and launch command.
Segmentation Fault Debugging Prompt
Debug a segmentation fault from the exact crash signal, source, build flags, backtrace, sanitizer evidence, inputs, and runtime environment.
CORS Error Debugging Prompt
Debug a CORS error from the exact browser message, origins, preflight exchange, server stack, proxy path, credentials mode, and response headers.
CLAUDE.md Generator Prompt
Generate a concise CLAUDE.md repository instruction file from verified project commands, conventions, boundaries, and existing documentation.