Testing Pyramid — Unit, Integration & E2E
The testing pyramid defines three layers: many fast unit tests at the base, fewer integration tests in the middle, and very few E2E tests at top. For backend APIs, the most valuable layer is integration tests — they test the actual HTTP behavior without needing a browser.
Testing Strategy
- **Unit tests** (70%): Test individual functions and classes in isolation with all dependencies mocked. Fast (~1ms each). Test: pure functions, service business logic, error handling branches, utility helpers
- **Integration tests** (25%): Test the full HTTP request → middleware → route handler → real database flow. Use a test database (SQLite or Postgres in Docker). Test: REST endpoints, auth flows, database constraints
- **E2E tests** (5%): Test full user journeys in a real environment. Expensive and slow. Reserve for critical paths: checkout flow, auth flow, payment
- **Test doubles**: Mock (fake object verifying calls), Stub (canned responses), Spy (wraps real function and records calls), Fake (working implementation — e.g., in-memory database)
- **Test isolation**: Each test must be independent. No shared state between tests. Use beforeEach to reset mocks and database state. Never depend on test execution order
Tip
Tip
Practice Testing Pyramid Unit Integration E2E in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Unit → Integration → E2E. Vitest = fastest.
Practice Task
Note
Practice Task — (1) Write a working example of Testing Pyramid Unit Integration E2E from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Testing Pyramid Unit Integration E2E is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready node code.
Key Takeaways
- The testing pyramid defines three layers: many fast unit tests at the base, fewer integration tests in the middle, and very few E2E tests at top.
- *Unit tests** (70%): Test individual functions and classes in isolation with all dependencies mocked. Fast (~1ms each). Test: pure functions, service business logic, error handling branches, utility helpers
- *Integration tests** (25%): Test the full HTTP request → middleware → route handler → real database flow. Use a test database (SQLite or Postgres in Docker). Test: REST endpoints, auth flows, database constraints
- *E2E tests** (5%): Test full user journeys in a real environment. Expensive and slow. Reserve for critical paths: checkout flow, auth flow, payment