Unit Testing
Structure (Arrange-Act-Assert)
describe('ServiceName', () => {
it('should [expected behavior] when [condition]', () => {
// Arrange: setup
// Act: call function
// Assert: verify result
});
});
What to Test
- Happy path (normal operation)
- Edge cases (empty, null, boundary values)
- Error cases (invalid input, network failure)
- Business logic (calculations, transformations)
Mocking
- Mock external dependencies (DB, API, filesystem)
- Never mock the thing you're testing
- Use dependency injection for testability
Coverage Target
- New code: 80%+ line coverage
- Critical paths: 100%