This directory contains comprehensive documentation for the Online Shopping Store backend API, with a focus on our three-tier testing architecture.
- Testing Guide - Complete guide to our testing architecture
- Testing Best Practices - Best practices and guidelines
- Quick Reference - Quick reference for daily development
- Unit Test Template - Template for unit tests
- Integration Test Template - Template for integration tests
- E2E Test Template - Template for end-to-end tests
Our testing follows the testing pyramid principle:
/\ E2E Tests (Few, Slow, High Confidence)
/ \ - Complete user workflows
/____\ - Real server + database
/ \ - HTTP requests + responses
/__________\
Integration Tests (Some, Medium Speed, Medium Confidence)
- Component interactions
- Real database connections
- Service layer testing
Unit Tests (Many, Fast, Low Confidence)
- Individual functions/classes
- Mocked dependencies
- Business logic validation
# Run all tests
npm run test:all
# Fast feedback during development
npm test
# Run specific test types
npm run test:unit # Unit tests (< 10s)
npm run test:integration # Integration tests (< 60s)
npm run test:e2e # End-to-end tests (< 120s)-
Choose the right test type:
- Unit: Testing individual functions/classes
- Integration: Testing component interactions
- E2E: Testing complete user workflows
-
Use the appropriate template:
- Copy from
docs/test-templates/ - Follow the AAA pattern (Arrange-Act-Assert)
- Use descriptive test names
- Copy from
-
Place in correct directory:
- Unit:
src/__tests__/unit/ - Integration:
src/__tests__/integration/ - E2E:
src/__tests__/e2e/
- Unit:
- Total Tests: 344 tests
- Unit Tests: 284 tests (100% passing)
- Integration Tests: 11 tests (100% passing)
- E2E Tests: 49 tests (100% passing)
| Test Type | Target | Actual | Status |
|---|---|---|---|
| Unit | < 10s | ~10.3s | ✅ |
| Integration | < 60s | ~2.3s | ✅ |
| E2E | < 120s | ~18.8s | ✅ |
.env.test.unit- Unit test environment (minimal config).env.test.integration- Integration test environment (real database).env.test.e2e- E2E test environment (complete setup)
DatabaseTestHelper- Database setup/cleanup utilitiesE2ETestHelper- Server and HTTP request utilitiesMockFactory- Mock object creation utilitiesTestDataBuilder- Test data creation utilities
- Red: Write a failing test
- Green: Write minimal code to make it pass
- Refactor: Improve code while keeping tests green
# Run linting
npm run lint
# Run all tests
npm run test:all
# Check coverage
npm test -- --coverage- Tests timing out: Increase timeout or optimize test setup
- Database connection errors: Check test database configuration
- Mock not working: Ensure mocks are set up before imports
- Flaky tests: Review test isolation and cleanup
# Verbose output
npm test -- --verbose
# Run specific test
npm test -- --testNamePattern="test name"
# No cache
npm test -- --no-cache
# Watch mode
npm test -- --watch- Choose appropriate test type based on what you're testing
- Use the relevant template from
test-templates/ - Follow naming conventions and best practices
- Ensure tests are isolated and independent
- Add appropriate documentation for complex test scenarios
- Keep documentation in sync with code changes
- Update examples when APIs change
- Add new patterns and best practices as they emerge
- Review and update performance targets periodically
For questions about testing:
- Check the Testing Guide first
- Review Best Practices
- Look at existing tests for examples
- Use the Quick Reference for common patterns
This documentation is maintained as part of our commitment to code quality and developer experience. Please keep it updated as the codebase evolves.