All quizzesHard
Testing Strategy
Preview — 3 of 10 questions
When testing a service that orchestrates several microservices via ClientProxy.send(), what is the key to a reliable distributed test without real brokers?
javascript
import { of, throwError } from 'rxjs';
import { delay } from 'rxjs/operators';
inventoryClient.send.mockReturnValue(of({ stock: 5 }).pipe(delay(10)));
paymentClient.send.mockReturnValue(throwError(() => new Error('declined')));
await expect(orderService.place(order)).rejects.toThrow('declined');ASpin up the full broker topology for every test
BReplace all RPC calls with synchronous local function calls in production code
CUse random data and accept flaky results
DMock each ClientProxy so send() returns a controlled of(response) (or a deferred observable to simulate latency/ordering), letting you assert orchestration logic deterministically
What does property-based testing with fast-check add over example-based unit tests for a NestJS pricing service?
javascript
import fc from 'fast-check';
it('total is never negative', () => {
fc.assert(
fc.property(fc.nat(), fc.float({ min: 0, max: 1 }), (price, discount) => {
const total = pricingService.apply(price, discount);
return total >= 0;
}),
);
});AIt generates many randomized inputs to verify invariants/properties hold across the input space, surfacing edge cases hand-picked examples miss
BIt replaces the need for the service entirely
CIt only tests UI components
DIt guarantees 100% branch coverage automatically
Why adopt consumer-driven contract testing (Pact) between NestJS microservices?
javascript
// Consumer side records expected interaction; provider verifies it:
await provider.addInteraction({
state: 'user 1 exists',
uponReceiving: 'a request for user 1',
withRequest: { method: 'GET', path: '/users/1' },
willRespondWith: { status: 200, body: { id: '1' } },
});ATo replace all unit tests with one contract
BTo encrypt traffic between services
CTo verify that a provider's API still satisfies the expectations (the contract) recorded by its consumers, catching breaking changes without full end-to-end environments
DTo measure code coverage across services
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.