question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using "ctx.delay" without arguments should add a realistic server response delay

See original GitHub issue

Is your feature request related to a problem? Please describe. Quite often you would want a mocked response to behave similar to the actual response in terms of response time. Real servers rarely reply to requests instantly, while using res() would do so.

Describe the solution you’d like Calling ctx.delay() without arguments should implicitly create a random realistic response delay, similar to the one of the actual server.

rest.get('/numbers', (req, res, ctx) => {
  return res(ctx.delay(), ctx.json([1, 2, 3])
})

Possible implementation Check if the durationMs argument is given to the delay() function:

https://github.com/mswjs/msw/blob/4a49e733ed10cb2b11c1941160d9a46ec21182af/src/context/delay.ts#L8-L13

And if not, generate a random response delay number instead. Something similar to:

res.delay = durationMs || RANDOM_REALISTIC_RESPONSE_TIME

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
UsamaHameedcommented, Jun 8, 2020

I will work on it!

1reaction
andreawysscommented, Jun 8, 2020

default delay amount should be very short when running in node vs. when running in browser. Maybe 10ms and 600ms

Read more comments on GitHub >

github_iconTop Results From Across the Web

delay() - Api - Mock Service Worker Docs
Delays the response by the given duration (in ms). When no duration is provided, uses a random realistic server response time.
Read more >
Mocking Error, Empty and Loading states with MSW
In this article, we take a look at how we can use MSW to mock these states ... It will also add a...
Read more >
HAProxy version 2.6.7-5 - Configuration Manual
Defines a delay between SIGUSR1 and real soft-stop. Arguments : <time> is an extra delay (by default in milliseconds) after receipt of the ......
Read more >
HAProxy version 2.0.28 - Configuration Manual - GitHub Pages
This parameter is used to enforce an upper bound on delay between the first ... The real use is for keep-alive connections sent...
Read more >
API — Flask Documentation (2.2.x)
The function will be called without any arguments and its return value is ignored. ... When using flask run to start the development...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found