Questions
5 of 25
1What built-in HTTP exceptions does NestJS provide out of the box?
2What interface must an exception filter implement and what are the two parameters of catch()?
3How do you apply an exception filter at the method, controller, and global level in NestJS?
4How do you add custom response headers or set cookies inside an exception filter in NestJS?
5How do you unit test an exception filter in NestJS?
6How do you implement a filter that adds a request correlation ID to every error response in NestJS?
7How do getResponse() and getStatus() work on an HttpException and what can getResponse() return?
8How do you create a domain-specific custom exception class in NestJS?
9How does exception filter resolution order work when multiple filters are registered at different levels in NestJS?
10How do you register multiple global filters that each handle a different exception type in NestJS?
11What does the @Catch() decorator do and what happens when you omit its argument?
12How do you extend the built-in BaseExceptionFilter to add logging while keeping default NestJS behaviour?
13How do you handle exceptions differently in a filter depending on whether the request is HTTP, WebSocket, or RPC?
14What are the two ways to register a global exception filter in NestJS and what is the key difference?
15How do interceptors and exception filters divide error-handling responsibility in NestJS?
16How do you write an exception filter that handles GraphQL errors differently from REST errors in NestJS?
17How do you prevent sensitive information from leaking through exception responses in production NestJS apps?
18How do you handle async operations inside an exception filter in NestJS, for example logging to a database?
19What is HttpException in NestJS and what two arguments does its constructor take?
20What does NestJS's built-in global exception layer do when no custom filter is registered?
21What is the cause option on HttpException and how should you use it for error tracing?
22How do you write a production-ready global all-exceptions filter that handles HttpException and unexpected errors differently?
23How do you integrate a third-party error tracker like Sentry inside a global exception filter in NestJS?
24How do you map database and ORM exceptions to HTTP exceptions using a global filter in NestJS?
25How do you re-throw an exception from within a filter to let a higher-scoped filter handle it in NestJS?
05 / 25

How do you unit test an exception filter in NestJS?

Create a mock ArgumentsHost as a plain object cast to the interface and call catch() directly — no HTTP server required. Use Test.createTestingModule() to resolve the filter with its injected dependencies. Assert that res.status() and res.json() were called with the expected arguments.

Unit test for an exception filter with mocked ArgumentsHost
Exception filter testing strategy:
  1. 1

    Call catch() directly — no need to spin up an HTTP server for unit tests.

  2. 2

    Create the mock host as a plain object cast to ArgumentsHost using as unknown as ArgumentsHost.

  3. 3

    Mock switchToHttp() to return mock getResponse() and getRequest() functions.

  4. 4

    Use jest.fn().mockReturnValue() chaining to simulate res.status(n).json(body) fluent API.

  5. 5

    Test both HttpException branches and non-HttpException (unexpected error) branches separately.

Difficulty: 6/10
Topics: Exception Filters, Unit Testing, NestJS Testing

Scenario Questions

0-2 years experience
  1. 1

    Suppose you have a simple NestJS exception filter that catches NotFoundException and returns a 404 response. How would you write a unit test to verify that the filter sets the correct status code and message?

  2. 2

    If you run a test for your exception filter and the response body is undefined, what could be missing in your test setup?

2-5 years experience
  1. 1

    You added a custom exception filter that logs errors to an external service. During a unit test, the test hangs. What might be causing the hang and how would you adjust the test?

  2. 2

    When refactoring a filter to use the NestJS ArgumentsHost, a teammate reports that the unit test now fails with a type error. How would you debug and fix the test?

5-8 years experience
  1. 1

    In a large microservice, multiple exception filters are applied globally and per-controller. How would you structure your unit tests to ensure each filter’s behavior is isolated and doesn’t interfere with others, especially regarding shared mocks?

  2. 2

    Your team wants to achieve high test coverage for exception handling without slowing CI pipelines. What strategies would you use to keep the unit tests for filters fast and reliable at scale?

8+ years experience
  1. 1

    Your organization is migrating from a monolith to a NestJS-based microservice architecture, and you need a consistent testing strategy for exception filters across many services. How would you design a shared testing library or pattern that balances flexibility and maintainability?

  2. 2

    There’s a proposal to replace NestJS exception filters with a centralized error handling middleware to simplify testing. What are the trade‑offs of that approach, and how would you convince stakeholders based on testing considerations?

Follow-up Questions

  • What would you change in the test if the filter also logs the error?
  • How do you handle testing a filter that performs asynchronous operations?
  • Can you describe how you would verify that the filter respects different exception types?