Replies: 1 comment 12 replies
-
Hey, @Geddard. Thanks for raising this question. MSW strongly encourages its users to compose, instead of relying on any configuration options. The latter is almost always limiting and won't satisfy each and every use-case, while the composition will. When it comes to composing MSW logic, you need to figure out on which level you wish to introduce the customization. In your case, you wish to add a fixed response delay to all response resolvers you write. I'd suggest creating a custom // ./resWithDelay.js
import { createResponseComposition } from 'msw'
export const resWithDelay = createResponseComposition({
delay: 500
}) Then use the // ./handlers.js
import { resWithDelay } from './resWithDelay'
export const handlers = [
rest.get('/user', (req, _, ctx) => {
return resWithDelay(ctx.json({ firstName: 'John' }))
})
] This type of customization scales well and gives you control over the custom logic you wish to have. In a similar fashion, you can create custom request handlers and context utilities. |
Beta Was this translation helpful? Give feedback.
-
Hi there!
I've been trying to figure out a nice and maintainable way of setting up some global controls for all my mocks.
The first one that can be a good example would be to add a global delay for all responses, or maybe all get responses.
I can't find any case like this anywhere :/ And i don't want to have to go and add
ctx.delay(x)
to each mock handler i created... In my head the ideal would be to create either some global handler that adds the delay to all other mocks at some point in the msw lifecycle, or by adding it to .start() or .use()But i can't find documentation and can't figure it out myself...
Any ideas on how to achieve this?
Beta Was this translation helpful? Give feedback.
All reactions