Back to prompts
CodingChatGPTClaudeGemini

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