NestJS Quality Control & Assurance 3 — Questions and Answers
Question 1: Which NestJS testing utility lets you replace an interceptor with a no-op version for a specific test?
- overrideFilter()
- overrideInterceptor() (Correct answer)
- overridePipe()
- overrideMiddleware()
Correct answer: overrideInterceptor()
overrideInterceptor() in the TestingModule builder accepts a class and swaps it with a test double using .useValue() or .useClass().
Question 2: What Jest configuration option should you set to automatically clear mock call history between each test in a NestJS project?
- resetMocks: true
- clearMocks: true (Correct answer)
- restoreMocks: true
- mockReset: true
Correct answer: clearMocks: true
clearMocks: true in jest.config.ts resets mock.calls and mock.instances before every test without restoring the original implementation.
Question 3: When using class-validator in NestJS, which test approach correctly validates a DTO without starting the full application?
- Import ValidationPipe and call transform() directly on a plain object
- Call validate() from class-validator on a class instance created with plainToInstance() (Correct answer)
- Mock the ValidationPipe inside the controller test
- Rely on the E2E test to catch validation errors
Correct answer: Call validate() from class-validator on a class instance created with plainToInstance()
plainToInstance + validate() from class-transformer/class-validator lets you unit-test DTO validation rules in isolation.
Question 4: Which tool integrates with NestJS to generate an OpenAPI specification that can be used as a contract test artifact?
- @nestjs/graphql
- @nestjs/swagger (Correct answer)
- nest-openrpc
- @nestjs/terminus
Correct answer: @nestjs/swagger
@nestjs/swagger's SwaggerModule.createDocument() produces an OpenAPI 3 document that can be diffed across versions for breaking-change detection.
Question 5: What is the effect of passing `{ passthrough: true }` to `jest.spyOn()` in a NestJS test?
- It makes the spy skip the function and return undefined
- It delegates to the original implementation while still recording calls
- It converts a sync function to async
- It is not a valid jest.spyOn option; passthrough is used with mockReturnValue (Correct answer)
Correct answer: It is not a valid jest.spyOn option; passthrough is used with mockReturnValue
jest.spyOn() does not accept a passthrough option; to call through to the original, you simply don't add a mock implementation.
Question 6: In a NestJS project using Jest, which command runs only tests whose file names match a specific pattern?
- jest --filter=<pattern>
- jest --testPathPattern=<pattern> (Correct answer)
- jest --include=<pattern>
- jest --watch=<pattern>
Correct answer: jest --testPathPattern=<pattern>
--testPathPattern accepts a regex that is matched against the full path of each test file.
Question 7: How should you close a NestJS application after an E2E test suite to prevent open handle warnings?
- process.exit(0)
- await app.close() (Correct answer)
- app.destroy()
- TestingModule.reset()
Correct answer: await app.close()
app.close() triggers the OnApplicationShutdown lifecycle hooks and closes HTTP server connections, releasing the event loop.
Which NestJS testing utility lets you replace an interceptor with a no-op version for a specific test?