β Integration Testing β
The backend has an integration testing infrastructure that boots real MongoDB, Redis, and Firebase Auth containers via Testcontainers, seeds a realistic dataset, and runs tests against the full NestJS application.
IMPORTANT
We need to write more tests! Integration tests are incredibly valuable for catching regressions and validating business logic. If you are working on a feature, consider adding a test for it.
Architecture overview β
Containers start once and stay alive between runs. Each test case gets a fresh DB snapshot restored by beforeEach. Stop containers manually when done for the day.
File structure β
apps/agri-backend/
βββ jest.config.js # Jest config β timeout, coverage, reporters
βββ test/
β βββ global-setup.ts # Starts MongoDB, Redis, Firebase containers once per run
β βββ fixtures/ # Static test data: users, companies, configurations
β βββ helpers/ # Reusable test helpers (e.g. user registration flows)
β βββ utils/
β βββ base-integration.spec.ts # BaseIntegrationTest β extend this for all integration tests
β βββ seed-test-data.ts # Full DB seeding logic
β βββ *.utils.ts # DB snapshot, Redis, Firebase emulator helpersRequirements β
Commands β
Login β
# login CLI
gcloud auth login
# logins docker
gcloud auth configure-docker europe-docker.pkg.devAll commands run from apps/agri-backend/.
# Start containers (keep running in a separate terminal)
pnpm test:up
# Run all tests
pnpm test
# Run only backend
pnpm --filter agri-backend test
# Run a single file (fastest during development)
pnpm --filter agri-backend test -- src/firebase/auth.controller.spec.ts
# Run tests matching a name pattern
pnpm test:run -- --testNamePattern="registration"
# Stop containers
pnpm test:downTIP
Start containers once with test:up, iterate with test:run, stop at end of day. Avoids ~10s of container startup on every run.
Firebase Auth Emulator β
Auth tests run against the Firebase Auth Emulator β managed by Testcontainers like MongoDB and Redis. It starts on a dynamic port and behaves identically to production Firebase Auth.
GCP authentication (required) β
The Firebase Auth Emulator image is hosted on a private GCP Artifact Registry (europe-docker.pkg.dev). Before running the tests for the first time, authenticate Docker with GCP:
# Log in to your GCP account
gcloud auth login
# Configure Docker to pull from GCP Artifact Registry
gcloud auth configure-docker europe-docker.pkg.devWithout this step, test:up will fail with an authentication error when pulling the Firebase emulator image.
HTML test report β
After every run, an HTML report is written to apps/agri-backend/test-report/index.html. Open it in a browser for a navigable view of all suites, results, durations, and stack traces. The folder is git-ignored.
Troubleshooting β
Containers not starting β make sure Docker (Desktop or OrbStack) is running.
Port conflicts / stale state β delete container-info.json and restart:
rm test/container-info.json && pnpm test:upTests timing out β check docker ps for container health and your network (LLM calls need internet).
Open handles warning β usually unclosed DB/Redis connections. BaseIntegrationTest.afterAll() handles the standard case; close any custom connections you create.
Further reading β
- Jest Expect API β full matcher reference
- Jest mock functions β
jest.spyOn,mockReturnValue,mockRejectedValueOnce - Testcontainers for Node.js β how containers are managed in global-setup
- NestJS testing β
Test.createTestingModule, module compilation - Supertest β HTTP assertions used in integration tests