Questions
24 of 25
1What is the difference between request-response and event-based (fire-and-forget) message patterns in NestJS microservices?
2What is the @Payload() decorator and how do you use @Ctx() to access transport-specific context in NestJS microservices?
3How do you connect a NestJS microservice to RabbitMQ?
4How do you implement manual message acknowledgement with RabbitMQ in NestJS?
5How do you implement a RabbitMQ topic exchange for routing messages to multiple queues based on routing keys in NestJS?
6How does gRPC differ from REST and when would you choose it for inter-service communication in NestJS?
7How do you implement an API Gateway pattern in NestJS that aggregates multiple microservices?
8How do you implement distributed tracing across NestJS microservices?
9What transport layers does NestJS support for microservices and how do you bootstrap a microservice?
10How do you connect a NestJS microservice to Apache Kafka?
11How do you handle Kafka message patterns and access message headers and partition info via KafkaContext in NestJS?
12How do you set up gRPC in a NestJS microservice?
13How do you implement a circuit breaker pattern when calling a downstream microservice in NestJS?
14How do you inject and use a ClientProxy to communicate with another microservice in NestJS?
15How do you handle errors in microservice request-response patterns and propagate them to the caller in NestJS?
16How do you implement Kafka consumer groups and what guarantees does partition assignment give you in NestJS?
17How do you implement the outbox pattern with Kafka in NestJS to guarantee exactly-once message delivery?
18How do you implement gRPC streaming including server-side, client-side, and bidirectional streaming in NestJS?
19What is the difference between @EventPattern() and @MessagePattern() in NestJS microservices?
20How do you implement the Saga pattern for distributed transactions across NestJS microservices?
21How do you implement idempotent event handlers to safely handle duplicate message delivery in NestJS?
22How do you implement event sourcing with NestJS microservices?
23How do you implement retry logic with exponential backoff for failed microservice calls in NestJS?
24How do you test a NestJS microservice controller in isolation without a real message broker?
25How do you implement service discovery and load balancing across NestJS microservice instances?
24 / 25

How do you test a NestJS microservice controller in isolation without a real message broker?

Difficulty: 6/10
NestJS testing, microservice controllers, mocking message brokers

Create a TestingModule with the controller and mocked service providers. Call the handler methods directly — no NestFactory.createMicroservice(), no broker connection, and no transport is needed. The message pattern decorators are metadata only and do not affect how the method executes when called directly.

Microservice controller unit test without a broker
Microservice controller testing strategy:
  1. 1

    Call handler methods directly — @MessagePattern and @EventPattern decorators are metadata, not runtime wrappers.

  2. 2

    No broker connection is needed — the transport layer is completely irrelevant to handler logic.

  3. 3

    Mock injected services with useValue and jest.fn() — same approach as HTTP controller tests.

  4. 4

    For integration tests that verify the transport, use a test Docker Compose with a real broker.

  5. 5

    Test error paths by making mocked services throw RpcException and asserting the re-thrown exception.

Scenario Questions

0-2 years experience

  1. 1You need to unit test a NestJS microservice controller that listens to a RabbitMQ pattern, but you don't want to spin up RabbitMQ. How would you set up the test?
  2. 2What steps would you take to mock the ClientProxy in a NestJS microservice controller test?

2-5 years experience

  1. 1We added a new message pattern to our NestJS microservice, and the existing controller tests started failing. Walk me through how you'd debug the issue without a real broker.
  2. 2Explain the trade‑offs between using Nest's TestingModule with a mocked ClientProxy versus using an in‑memory broker like NATS for controller tests.

5-8 years experience

  1. 1Our system has dozens of microservice controllers, and the CI pipeline is taking too long because each test spins up a mock broker. How would you redesign the testing approach to keep isolation but improve speed and reliability?
  2. 2When testing a controller that performs complex transformations on incoming messages, what edge cases would you cover, and how would you structure your mocks to avoid false positives at scale?

8+ years experience

  1. 1Looking ahead, we plan to migrate several services from RabbitMQ to Kafka while keeping the same controller interfaces. How would you evolve our testing strategy to support both brokers without duplicating test code?
  2. 2Across multiple teams, there’s inconsistency in how microservice controllers are unit‑tested. Propose a reusable testing library or pattern that enforces best practices while allowing team autonomy.

Follow-up Questions

  • What assertions would you place on the mock's send method?
  • How do you prevent mock state from leaking between test cases?
  • Can you sketch the TestingModule setup code for this controller?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.