axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

Helper for delay in milliseconds

Open prashanth-92 opened this issue 3 years ago • 11 comments

#232 Attempts to provide a helper for adding a delay in milliseconds. The test shows a way to use the method.

prashanth-92 avatar Sep 06 '21 18:09 prashanth-92

Hi @prashanth-92! Thanks for your PR, but unfortunately your change doesn't actually introduce per-request delays. The added method instead sets the delay for all requests, so for example:

mock.delayInMs(200).onGet("/foo").reply(200);
mock.delayInMs(0).onGet("/bar").reply(200);

will cause no delay when a request to /foo is made.

ctimmerm avatar Sep 07 '21 07:09 ctimmerm

Hey @ctimmerm, yes my solution is setting the delay in the MockAdapter directly. So it will work for one endpoint but if you set it again, it will get overridden

Just to see if I get this right, if we need to add it at an endpoint level, then we might need to introduce a state in the handlers per verb and then use delay it with a promise approach you mentioned in one of the comments.

prashanth-92 avatar Sep 07 '21 09:09 prashanth-92

@ctimmerm I have a revised approach with delay per request. Can you please review it?

prashanth-92 avatar Sep 07 '21 19:09 prashanth-92

Ping @ctimmerm If you have some time, please review this pull request

prashanth-92 avatar Sep 19 '21 06:09 prashanth-92

Hey @ctimmerm when you have time, please review this pull request. I would really appreciate it.

prashanth-92 avatar Oct 14 '21 11:10 prashanth-92

Please re-review @ctimmerm

prashanth-92 avatar Nov 13 '21 17:11 prashanth-92

Please re review.

prashanth-92 avatar Nov 27 '21 12:11 prashanth-92

@ctimmerm please review.

prashanth-92 avatar Jan 01 '22 05:01 prashanth-92

@ctimmerm Can you please re review?

prashanth-92 avatar Jan 24 '22 12:01 prashanth-92

I would also be interested to have withDelay() accept a function. This would allow me to create a function that generates a random delay in a specific range.

jshall avatar Feb 03 '22 23:02 jshall

I would also be interested to have withDelay() accept a function. This would allow me to create a function that generates a random delay in a specific range.

The idea behind this pull request is to give a helper for introducing a delay time alone. Your new feature request can be addressed in another pull request. Feel free to open one.

prashanth-92 avatar Feb 11 '22 05:02 prashanth-92

Hey guys, is this getting merged or nah? I just found a use case where I need a delay for a specific request.

phiter avatar Jan 31 '23 17:01 phiter

@ctimmerm If there are no concerns, can we merge this PR? I think it will be useful.

prashanth-92 avatar Mar 21 '23 05:03 prashanth-92

hmm, a force push merged this accidentally and can't reopen. I'll merge this manually

marcbachmann avatar Sep 11 '23 08:09 marcbachmann

hah. reopen button shows up again.

edit: nope. didn't work

marcbachmann avatar Sep 11 '23 08:09 marcbachmann

Released as v1.22.0

marcbachmann avatar Sep 11 '23 08:09 marcbachmann

marcbachmann, prashanth-92 - I'm a bit late to the party but there's no documentation and I think the typescript definitions are missing...

jfstephe avatar Sep 14 '23 08:09 jfstephe

Also IMHO the API for this is now unintuitive (it looked like @ctimmerm had a good suggestion). Why the change to make this return the reply function rather than the request handler?

jfstephe avatar Sep 14 '23 08:09 jfstephe

Interested about that too. Need a doc & types up to date plz.

Glideh avatar Oct 17 '23 13:10 Glideh

Interested about that too. Need a doc & types up to date plz.

I opened a PR to add types to replyWithDelay https://github.com/ctimmerm/axios-mock-adapter/pull/383

seriouslag avatar Apr 11 '24 18:04 seriouslag