Skip to main content

Testing

NestForge Pro ships with 21 test suites: 18 unit test suites and 3 end-to-end (E2E) test suites covering the critical paths of the application.

Running tests

All tests via Turborepo

npx turbo test

API tests only

cd apps/api && npm test

Run with coverage

cd apps/api && npx jest --coverage

Test structure

Each API module has a co-located .spec.ts file containing its unit tests:

apps/api/src/
├── modules/
│ ├── auth/
│ │ ├── auth.service.ts
│ │ └── auth.service.spec.ts
│ ├── users/
│ │ ├── users.service.ts
│ │ └── users.service.spec.ts
│ ├── billing/
│ │ ├── billing.service.ts
│ │ └── billing.service.spec.ts
│ ├── teams/
│ │ ├── teams.service.ts
│ │ └── teams.service.spec.ts
│ └── ...

Unit tests mock external dependencies (Prisma, Stripe, Resend) so they run fast and without network access.

E2E tests

End-to-end tests live in apps/api/test/ and exercise full request/response cycles against a running application:

apps/api/test/
├── auth.e2e-spec.ts # Login, registration, token refresh, password reset
├── teams.e2e-spec.ts # Team creation, member invitations, role management
└── admin.e2e-spec.ts # Admin user listing, metrics, impersonation

E2E prerequisites

E2E tests require running service containers:

  • PostgreSQL -- a test database (the test runner applies migrations automatically)
  • Redis -- required for queue-dependent flows

In CI, these are provided by service containers defined in the GitHub Actions workflow. See .github/workflows/ci.yml for the configuration.

Running E2E tests locally

cd apps/api && npx jest --config test/jest-e2e.config.ts

Make sure your .env points to a test database (not your development database) to avoid data conflicts.

CI integration

The CI pipeline (.github/workflows/ci.yml) runs the full test suite on every push and pull request:

  1. Spins up PostgreSQL and Redis service containers
  2. Installs dependencies and generates the Prisma client
  3. Runs migrations against the test database
  4. Executes jest --coverage for both unit and E2E suites
  5. Reports coverage results

Writing new tests

When adding a new module or feature:

  1. Create a .spec.ts file next to the service or controller you are testing
  2. Mock Prisma using the existing PrismaService mock factory in apps/api/src/database/prisma.service.mock.ts
  3. For E2E tests, add a new *.e2e-spec.ts file in apps/api/test/ and use supertest to make HTTP requests against the running app