Universal testing principles - test pyramid, AAA pattern, mocking strategies, and coverage targets
/ E2E \ - Few, slow, high confidence
/ Integration \ - Some, medium speed
/ Unit Tests \- Many, fast, focused
Every test follows this structure:
test('should calculate total with tax', () => {
// Arrange: set up test data
const items = [{ price: 10 }, { price: 20 }];
const taxRate = 0.1;
// Act: execute the function
const total = calculateTotal(items, taxRate);
// Assert: verify the result
expect(total).toBe(33);
});
Always test:
Don't test:
| What | When to Mock |
|---|---|
| External APIs | Always. They're slow and unreliable |
| Database | Integration tests use real DB, unit tests mock |
| Time/Date | When testing time-dependent logic |
| File system | When testing file operations |
| Environment | When testing env-dependent behavior |
Rules:
beforeEach / afterEach)Use descriptive names that explain the scenario:
// Good
"should return 404 when user does not exist"
"should hash password before saving to database"
"should retry failed request up to 3 times"
// Bad
"test1"
"works correctly"
"handles error"