axios-mock-adapter
axios-mock-adapter copied to clipboard
Is it possible to add a delay for a specific response?
I mean something like this:
mock.onPost('/any').reply(201, null, { delay: 2000 })
Since you can reply with a promise, you can use that in combination with setTimeout to have per-request delays:
mock.onGet('/any').reply(function(config) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve([201, null]);
}, 2000);
});
});
If it's something you need to do often, you could write a helper function for it:
const withDelay = (delay, response) => config => {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(response);
}, delay);
});
});
// And then use it:
mock.onGet('/any').reply(withDelay(2000, [201, null]));
@ctimmerm Thanks for the tip! But don't you think it would be better to add this option to the adapter itself?
It would definitely be nice if we could do something like this:
mock.onGet('/any').delay(1000).reply((config) => {
return [200];
});
So we don't have to wrap the entire response function with a Promise...
@ctimmerm An attempt for a helper function for the delay: https://github.com/ctimmerm/axios-mock-adapter/pull/312 Thoughts?
I opened a PR to add types to replyWithDelay
#383