All quizzesMedium
Mocking & E2E Tests — Series 3
Preview — 3 of 10 questions
When is the factory form preferable to useValue?
javascript
.overrideProvider(MailService)
.useFactory({
factory: (config: ConfigService) => new RecordingMailer(config.get('MAIL_FROM')),
inject: [ConfigService],
})AAlways, since useValue cannot provide an object with methods
BWhen the double itself needs something from the container — configuration, a shared recorder, another test helper — because useFactory is resolved through DI with its own inject list, while useValue can only supply an object you built by hand
CWhen the override must apply to several tests at once
DWhen the original provider is request-scoped
What does the second approach cover that the first does not?
javascript
// (1) call intercept() directly with a fabricated context and CallHandler
// (2) register it on a route and assert on the HTTP responseANothing; the direct call is strictly more precise
BOnly the response status, which the direct call cannot produce
CThe interceptor's internal logic, which is inaccessible otherwise
DThat it is actually bound where you think, that it composes correctly with the other enhancers on that route, and that its transformation survives serialisation — the parts most likely to be wrong, since the interceptor's own logic is usually the simple half
When is this appropriate?
javascript
expect(response.body).toMatchSnapshot();AWhen the shape is large and genuinely stable, and you want any change to it to be visible in review — the snapshot then acts as a reviewed contract; it works badly when the payload contains ids, timestamps or ordering that vary, since every run produces a diff and people learn to update snapshots reflexively
BAlways, since a snapshot covers more of the response than hand-written assertions
COnly for responses under 1 kB, which is Jest's limit
DNever — snapshots are unsuitable for API responses
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.