| name | testing-patterns |
| description | Testing patterns including AAA, mocking, and test data management. Use when writing unit tests, integration tests, or test suites. |
| license | Apache 2.0 |
Testing Patterns
When to Use This Skill
- Writing unit tests
- Writing integration tests
- Setting up test infrastructure
Core Concepts
AAA Pattern (Arrange-Act-Assert)
describe('UserService', () => {
describe('createUser', () => {
it('should create user with valid data', async () => {
const createDto = { email: 'test@example.com', name: 'Test' };
const result = await service.createUser(createDto);
expect(result).toBeDefined();
expect(result.email).toBe(createDto.email);
});
});
});
Mocking
const mockRepository = {
findById: jest.fn(),
create: jest.fn(),
} as jest.Mocked<UserRepository>;
mockRepository.findById.mockResolvedValue({ id: '123' });
Test Data Factory
const userFactory = {
valid: () => ({ email: 'test@example.com', name: 'Test' }),
withEmail: (email: string) => ({ ...userFactory.valid(), email }),
invalid: () => ({ email: 'invalid', name: '' }),
};
Test Naming
it('should return 404 when user not found', async () => { ... });
it('should reject invalid email format', () => { ... });
it('should work', () => { ... });
References