NestJS Quality Control & Assurance 2 — Questions and Answers
Question 1: Which Jest matcher should you use to verify that a NestJS service method throws a specific exception type?
- expect(fn).toThrow(NotFoundException) (Correct answer)
- expect(fn).toEqual(NotFoundException)
- expect(fn).toReject(NotFoundException)
- expect(fn).toBe(NotFoundException)
Correct answer: expect(fn).toThrow(NotFoundException)
toThrow() checks that the function throws an error matching the given class or message when called.
Question 2: When writing an E2E test with Supertest in NestJS, which method initializes the application before the test suite runs?
- app.listen()
- app.init() (Correct answer)
- app.bootstrap()
- app.start()
Correct answer: app.init()
app.init() compiles the module graph and starts the application without binding a port, making it ready for Supertest requests.
Question 3: What is the purpose of the @nestjs/testing package's `overrideGuard` method?
- To disable all guards globally in tests
- To replace a guard with a custom test-only implementation (Correct answer)
- To spy on guard calls without replacing them
- To skip authorization checks for admin users
Correct answer: To replace a guard with a custom test-only implementation
overrideGuard() lets you swap a real guard with a mock that returns a fixed canActivate result, isolating the route handler under test.
Question 4: Which database strategy is recommended for NestJS integration tests that need a real database connection without polluting production data?
- Connecting to the production DB in read-only mode
- Using an in-memory SQLite instance or a dedicated test database (Correct answer)
- Mocking all repository methods with jest.fn()
- Disabling TypeORM synchronize during tests
Correct answer: Using an in-memory SQLite instance or a dedicated test database
An isolated test database or in-memory DB ensures tests are reproducible and don't affect real data.
Question 5: How do you assert that a NestJS controller returns HTTP status 201 in an E2E test using Supertest?
- .expect('status', 201)
- .status(201)
- .expect(201) (Correct answer)
- .assertStatus(201)
Correct answer: .expect(201)
Supertest's .expect(201) asserts that the HTTP response status code equals 201.
Question 6: What does jest.spyOn(service, 'findOne').mockResolvedValue(mockUser) accomplish in a NestJS unit test?
- It calls the real findOne and records its return value
- It replaces findOne with a spy that resolves a promise with mockUser (Correct answer)
- It blocks findOne from being called during the test
- It asserts that findOne was called with mockUser
Correct answer: It replaces findOne with a spy that resolves a promise with mockUser
mockResolvedValue wraps the mock in a resolved promise, simulating an async service method without hitting the database.
Question 7: In NestJS testing, what is the role of `TestingModule.get<T>()` after `Test.createTestingModule().compile()`?
- It starts the HTTP server for the module
- It retrieves an instance of a provider from the test module's DI container (Correct answer)
- It compiles TypeScript decorators for the provider
- It registers a new provider at runtime
Correct answer: It retrieves an instance of a provider from the test module's DI container
get<T>() resolves and returns the provider instance managed by the test module's injector, the same way the real app would.
Which Jest matcher should you use to verify that a NestJS service method throws a specific exception type?