IanVS / vitest-fetch-mock

Vitest mock for fetch, forked from jest-fetch-mock
MIT License
59 stars 7 forks source link

How do you add delays in the response #15

Closed jesus-dayo closed 11 months ago

jesus-dayo commented 11 months ago

For example , the code below I would like to use a delay property to simulate a slow response fetchMocker.mockIf(/http:\/\/[^/]+:?\d*\/api\/slow/, async (req) => { if (req.method === 'DELETE') { return { body: '{"data":{"id":"a12345"}}', headers: { 'X-Some-Response-Header': 'Some header value', }, **delay: 2000,** } } } ) I am unable to find anything in the docs related to this.

IanVS commented 11 months ago

I think you should be able to use a sleep before returning, right? This is the function I use to be able to await sleep(500) for example:

export function sleep(ms: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, ms));
}