Unit & Integration Test Generator
Generate comprehensive test suites with edge cases, mocking strategies, and coverage for any function or API endpoint.
You are a test engineering expert. Generate a complete test suite for:
**Code to test:**
```
[PASTE FUNCTION / CLASS / API ROUTE HERE]
```
**Test framework:** [Jest / Vitest / Pytest / Go test]
**Testing library:** [React Testing Library / Supertest / httpx]
Generate:
### 1. Unit Tests
For each function/method:
- Happy path (expected input → expected output)
- Edge cases (empty, null, boundary values)
- Error cases (invalid input, network failure)
### 2. Integration Tests (if applicable)
- API endpoint: request → response cycle
- Database: actual queries with test database
### 3. Mocking Strategy
```typescript
// What to mock and how
// jest.mock() / vi.mock() patterns
// Database mocks vs in-memory alternatives
```
### 4. Test Data Factories
```typescript
// Builder pattern for creating test fixtures
const createUser = (overrides = {}) => ({
id: 'test-id', name: 'Test User', ...overrides
});
```
### 5. Coverage Checklist
Which lines/branches are covered and which aren't
### 6. Testing Anti-Patterns to Avoid
What NOT to test in this codebaseResult Text
```typescript // Testing a user authentication function import { describe, it, expect, vi, beforeEach } from 'vitest'; import { authenticateUser } from './auth'; import { db } from '@/lib/db'; import { hashPassword } from './crypto'; // Mock database vi.mock('@/lib/db', () => ({ db: { user: { findUnique: vi.fn() } } })); const createUser = (overrides = {}) => ({ id: 'user-123', email: 'test@example.com', passwordHash: '$2b$10$hashedpassword', status: 'active', ...overrides }); describe('authenticateUser', () => { beforeEach(() => vi.clearAllMocks()); it('returns user on valid credentials', async () => { const user = createUser(); vi.mocked(db.user.findUnique).mockResolvedValue(user); const result = await authenticateUser('test@example.com', 'password123'); expect(result).toEqual({ id: user.id, email: user.email }); expect(result).not.toHaveProperty('passwordHash'); // never expose hash }); it('throws on wrong password', async () => { vi.mocked(db.user.findUnique).mockResolvedValue(createUser()); await expect(authenticateUser('test@example.com', 'wrong')) .rejects.toThrow('Invalid credentials'); }); it('throws on suspended account', async () => { vi.mocked(db.user.findUnique).mockResolvedValue(createUser({ status: 'suspended' })); await expect(authenticateUser('test@example.com', 'password123')) .rejects.toThrow('Account suspended'); }); it('returns null for non-existent user', async () => { vi.mocked(db.user.findUnique).mockResolvedValue(null); const result = await authenticateUser('ghost@example.com', 'any'); expect(result).toBeNull(); }); }); ```
Comments
to leave a comment.
No comments yet. Be the first to comment!
Related Prompts
Code Reviewer — Security, Performance & Best Practices
Get thorough code reviews covering bugs, security vulnerabilities, performance bottlenecks, and clean refactoring.
Python Data Analysis — EDA to ML Pipeline
Complete Python workflow from data loading and EDA through feature engineering to ML model selection and evaluation.
REST API Designer — OpenAPI 3.0 Spec
Design RESTful APIs with proper resource modeling, status codes, auth strategy, and complete OpenAPI 3.0 documentation.