NestJS Quality Control & Assurance 5 — Questions and Answers
Question 1: Which NestJS health-check library integrates with @nestjs/terminus to verify database connectivity as part of a readiness probe?
- TypeOrmHealthIndicator from @nestjs/terminus (Correct answer)
- DatabaseHealthCheck from @nestjs/core
- PrismaHealthIndicator from @nestjs/prisma
- SequelizeHealthChecker from @nestjs/sequelize
Correct answer: TypeOrmHealthIndicator from @nestjs/terminus
TypeOrmHealthIndicator provides a pingCheck() method that tests the database connection and is included in @nestjs/terminus.
Question 2: What testing pattern should be used to verify that a NestJS EventEmitter listener is triggered when a specific event is emitted?
- Spy on the listener method and emit the event through the EventEmitter2 instance (Correct answer)
- Mock EventEmitter2 entirely and assert it was called
- Use process.nextTick to flush the event queue
- Disable async listeners and call the listener directly
Correct answer: Spy on the listener method and emit the event through the EventEmitter2 instance
Spying on the listener and emitting the event through the real EventEmitter2 instance verifies the full publish-subscribe wiring.
Question 3: When testing a NestJS Microservice consumer, which testing approach avoids requiring a real message broker like RabbitMQ?
- Use a real RabbitMQ instance in a Docker container for all tests
- Inject a mock ClientProxy and assert that send() or emit() was called with correct args (Correct answer)
- Disable the @MessagePattern decorator in test mode
- Use the HTTP transport as a drop-in replacement
Correct answer: Inject a mock ClientProxy and assert that send() or emit() was called with correct args
Mocking ClientProxy isolates the microservice logic from transport concerns, making tests fast and deterministic.
Question 4: How does Pact contract testing complement NestJS E2E tests when multiple microservices are involved?
- Pact replaces E2E tests entirely by simulating the full network stack
- Pact verifies consumer-driven contracts between services without requiring all services to be running simultaneously (Correct answer)
- Pact generates NestJS guards automatically based on API schemas
- Pact only works with REST and cannot be used with NestJS GraphQL
Correct answer: Pact verifies consumer-driven contracts between services without requiring all services to be running simultaneously
Consumer-driven contract testing lets each service team verify their side of the API contract independently, catching integration issues early.
Question 5: Which approach correctly tests a NestJS middleware function without starting the HTTP server?
- Call middleware.use() with mock Request, Response, and next function objects (Correct answer)
- Register the middleware in the test module and send requests via Supertest
- Use overrideMiddleware() in the TestingModule builder
- Wrap the middleware in an interceptor for easier mocking
Correct answer: Call middleware.use() with mock Request, Response, and next function objects
Middleware functions are plain functions; passing mock req, res, and next objects lets you unit-test the logic directly.
Question 6: In a NestJS CI pipeline, what is the purpose of setting `maxWorkers` in jest.config.ts?
- It limits how many modules can be imported per test file
- It controls how many test files run in parallel, preventing resource contention on CI machines (Correct answer)
- It sets the maximum number of mock functions per test
- It caps the number of describe blocks allowed in a single file
Correct answer: It controls how many test files run in parallel, preventing resource contention on CI machines
maxWorkers limits Jest's worker pool size, which is important on constrained CI runners to avoid OOM or CPU contention.
Question 7: What does enabling `collectCoverageFrom` in jest.config.ts ensure in a NestJS project?
- Only spec files are included in the coverage report
- Coverage is collected from specified source files even if no test imports them, revealing untested code (Correct answer)
- Coverage is only collected from files that changed in the current PR
- It automatically generates stub tests for uncovered files
Correct answer: Coverage is collected from specified source files even if no test imports them, revealing untested code
Without collectCoverageFrom, files never imported by tests are excluded from reports, making coverage metrics misleadingly high.
Which NestJS health-check library integrates with @nestjs/terminus to verify database connectivity as part of a readiness probe?