Skip to content

πŸ§ͺ Testing Strategies ​

Building confidence in your code through effective testing.

Testing pyramid ​

        /  E2E  \        Few, slow, expensive
       / Integration \   Some, moderate
      /    Unit Tests   \ Many, fast, cheap

Test types ​

TypeScopeSpeedWhen to use
UnitSingle function/classmsBusiness logic, pure functions
IntegrationMultiple componentssecondsAPI endpoints, DB queries
E2EFull user flowseconds-minutesCritical paths, smoke tests
ContractAPI boundariesmsMicroservices, external APIs
SnapshotUI outputmsComponent rendering
PerformanceLoad/response timevariesBefore release, critical paths

Writing good tests ​

  • Arrange, Act, Assert β€” Setup, execute, verify
  • Test behavior, not implementation
  • One assertion per test (when practical)
  • Use descriptive test names: should_return_404_when_user_not_found
  • Tests should be independent β€” no shared state between tests
  • Tests should be deterministic β€” same result every time

What to test ​

  • Happy path (expected behavior)
  • Edge cases (empty input, null, max values)
  • Error cases (invalid input, network failure)
  • Boundary conditions (first, last, off-by-one)

What NOT to test ​

  • Third-party library internals
  • Trivial getters/setters
  • Framework boilerplate
  • Implementation details that may change

Test doubles ​

TypePurpose
MockVerify interactions (was this called?)
StubReturn predetermined data
SpyRecord calls for later assertion
FakeSimplified working implementation (in-memory DB)

Coverage ​

  • Aim for 80%+ on business logic
  • 100% coverage β‰  bug-free code
  • Measure coverage to find gaps, not as a target

Pergame Knowledge Base