A good API test suite covers business decisions and the HTTP contract without forcing every test to boot the full system. NestJS’s testing utilities can create a focused module with the providers a test needs.
Test the service logic directly
Unit tests should exercise validation outcomes, authorization decisions, and state transitions in services or use cases. Replace external dependencies with small typed fakes or mocks whose behavior is explicit. Avoid mocking the method under test or asserting implementation details that do not affect callers.
Test the route contract separately
An HTTP-level test can verify routing, guards, pipes, serialization, status codes, and error mapping together. Keep a small number of these tests for important endpoints. If the test uses a real PostgreSQL database, make that requirement explicit and ensure the schema is aligned; a fake repository test cannot verify database constraints or transaction behavior.
Cover invalid payloads, unauthenticated requests, unauthorized resource access, not-found behavior, and successful responses. For mutations, test duplicate submission and persistence semantics where they matter. Avoid sharing mutable database fixtures between tests unless each test cleans up safely.
Run tests in CI with deterministic setup and useful failure output. A test suite is valuable when failures explain which contract broke and help a maintainer reproduce it without starting every service locally.
Use Jest for the behavior covered by the title
Nest testing utilities create a TestingModule with the providers a test needs. The module can replace an injected dependency, helping service tests focus on policy and observable outputs. Nest's HTTP end-to-end testing guide shows how to initialize an application and use Supertest. Apply the same validation pipe, prefixes, and guards as the real server when the assertion is about the public contract. NestJS testing guide
Jest can import describe, expect, and it from @jest/globals in TypeScript tests. Start with a request from the owner and one from a second account. Then add malformed payloads and conflicting versions. Keep the handler and actual authorization policy in the HTTP integration test. If its database is replaced by a fake, add a targeted PostgreSQL test for constraints or transaction behavior. Jest API and TypeScript examples
Worked example
This example uses NestJS, Supertest and the Jest globals to exercise the route through the HTTP server. Keep the decorators and TypeScript transformer enabled in the test configuration.
tsimport { expect, it } from "@jest/globals"; import { Controller, Get, HttpStatus } from "@nestjs/common"; import { Test } from "@nestjs/testing"; import request from "supertest"; @Controller("health") class HealthController { @Get() getStatus() { return { status: "ok" }; } } it("returns the documented health response over HTTP", async () => { const module = await Test.createTestingModule({ controllers: [HealthController], }).compile(); const app = module.createNestApplication(); try { await app.init(); await request(app.getHttpServer()) .get("/health") .expect(HttpStatus.OK) .expect({ status: "ok" }); } finally { await app.close(); } });




Community
Discussion
Loading comments…