NestJS NestJS Testing & Debugging 3 — Questions and Answers
Question 1: How do you test a NestJS `CanActivate` guard in isolation?
- Mount it in an e2e test and check the HTTP response status
- Instantiate the guard directly and call its `canActivate()` method with a mock `ExecutionContext` (Correct answer)
- Use `app.useGlobalGuards()` inside the test module
- Decorate the test controller with `@UseGuards()` and verify route access
Correct answer: Instantiate the guard directly and call its `canActivate()` method with a mock `ExecutionContext`
Guards expose a `canActivate(context)` method; you can unit test it by instantiating the guard class and passing a crafted mock `ExecutionContext` to verify its logic.
Question 2: What is the purpose of the `mockReturnValue()` method on a Jest mock function in NestJS tests?
- It runs the real implementation once and then returns undefined
- It sets the value the mock function will return on every subsequent call (Correct answer)
- It asserts the mock was called with the specified value
- It resets the mock's call history and sets a new implementation
Correct answer: It sets the value the mock function will return on every subsequent call
`mockReturnValue(val)` configures a `jest.fn()` mock to return `val` synchronously on every call, allowing you to control dependent service behavior in tests.
Question 3: Which Jest method should you use when a NestJS service method returns a Promise and you want to mock its resolved value?
- mockReturnValue(Promise.resolve(data))
- mockResolvedValue(data) (Correct answer)
- mockImplementation(async () => data)
- mockReturnValueOnce(data)
Correct answer: mockResolvedValue(data)
`mockResolvedValue(data)` is shorthand for `mockReturnValue(Promise.resolve(data))` and is the idiomatic way to mock async service methods in NestJS tests.
Question 4: In NestJS testing, what is the recommended way to test an `ExceptionFilter` that transforms `HttpException` into a custom response?
- Import the filter in a test module and call the route that throws
- Instantiate the filter and call `catch(exception, host)` with mock arguments
- Use `app.useGlobalFilters()` in an e2e test and assert on the response body
- Both B and C are valid approaches depending on whether you want unit or e2e coverage (Correct answer)
Correct answer: Both B and C are valid approaches depending on whether you want unit or e2e coverage
Exception filters can be unit tested by calling `catch()` directly with mock `ArgumentsHost`, or e2e tested by checking the full HTTP response — both approaches provide complementary coverage.
Question 5: What does the `--coverage` flag do when running `jest` in a NestJS project?
- Runs only tests marked with @CoverageTest() decorator
- Generates a code coverage report showing which lines/branches are tested (Correct answer)
- Limits test execution to files in the coverage whitelist
- Enables verbose output for each test case
Correct answer: Generates a code coverage report showing which lines/branches are tested
The `--coverage` flag instruments the code and produces a report (HTML, text, lcov) showing the percentage of statements, branches, functions, and lines covered by tests.
Question 6: Which NestJS feature allows you to log detailed request/response information during development to help with debugging?
- The built-in `Logger` class with `app.useLogger()` (Correct answer)
- The `@Debug()` decorator applied to controllers
- The `NestDevTools` package integrated via `NestFactory`
- Setting `NODE_ENV=debug` automatically enables verbose output
Correct answer: The built-in `Logger` class with `app.useLogger()`
NestJS ships with a built-in `Logger` class; you can enable it globally via `app.useLogger(new Logger())` or inject it into any provider to log contextual messages.
Question 7: What is the role of `TestingModule.resolve()` as opposed to `TestingModule.get()` in NestJS testing?
- `resolve()` is synchronous; `get()` is asynchronous
- `resolve()` creates a new scoped/transient instance; `get()` returns the singleton (Correct answer)
- `resolve()` retrieves global providers; `get()` retrieves module-local providers
- `resolve()` compiles the module lazily; `get()` compiles it eagerly
Correct answer: `resolve()` creates a new scoped/transient instance; `get()` returns the singleton
`resolve()` returns a new instance for REQUEST or TRANSIENT scoped providers on each call, while `get()` always returns the same singleton instance registered in the DI container.
How do you test a NestJS `CanActivate` guard in isolation?