Testing
The SSP Backend API uses Jest as its testing framework for both unit tests and end-to-end (e2e) tests. This guide covers how to write and run tests.Testing Stack
The application uses the following testing tools:- Jest - Testing framework
- @nestjs/testing - NestJS testing utilities
- ts-jest - TypeScript support for Jest
- supertest - HTTP assertions for e2e tests
Test Scripts
Available test commands (frompackage.json:16):
Running Tests
Run All Unit Tests
*.spec.ts in the src/ directory.
Watch Mode
Run tests continuously as you make changes:- Watch for file changes
- Re-run related tests automatically
- Provide interactive menu for test filtering
Coverage Report
Generate code coverage report:coverage/ directory.
End-to-End Tests
Run e2e tests:test/ directory using the e2e configuration.
Debug Tests
Run tests with debugging enabled:Jest Configuration
Jest is configured inpackage.json:71:
Configuration Options
- rootDir: Tests are located in
src/directory - testRegex: Match files ending with
.spec.ts - transform: Use ts-jest to compile TypeScript
- testEnvironment: Use Node.js environment (not browser)
Unit Tests
Unit tests test individual components in isolation.Example Unit Test
Fromsrc/app.controller.spec.ts:
Writing Unit Tests
Testing Services
Example service test:Testing Controllers
Example controller test:End-to-End Tests
E2E tests test the entire application flow from HTTP request to response.E2E Configuration
Fromtest/jest-e2e.json:
Example E2E Test
Fromtest/app.e2e-spec.ts:
Writing E2E Tests
Example authentication e2e test:Mocking
Mocking Repositories
Mocking Services
Mocking ConfigService
Test Database
For integration tests, use a separate test database:.env.test:
Best Practices
Test Isolation
Each test should be independent and not rely on other tests
Use Mocks
Mock external dependencies to isolate the code under test
Descriptive Names
Use clear, descriptive test names that explain what’s being tested
Arrange-Act-Assert
Structure tests: setup (arrange), execute (act), verify (assert)
Test Edge Cases
Test error conditions, null values, and boundary conditions
Maintain Tests
Keep tests up to date as code changes
Common Testing Patterns
Testing Async Operations
Testing Exceptions
Testing with Guards
Troubleshooting
Tests timing out
Tests timing out
Increase Jest timeout:
Database connection errors in tests
Database connection errors in tests
Ensure test database exists and is configured:
Module not found errors
Module not found errors
Ensure all dependencies are installed:
What’s Next?
Running Locally
Set up your development environment
Deployment
Deploy your application to production
Database Setup
Configure test databases
API Reference
Test your API endpoints
