IanVS / vitest-fetch-mock

Vitest mock for fetch, forked from jest-fetch-mock
MIT License
68 stars 9 forks source link

How do you add delays in the response #15

Closed jesus-dayo closed 1 year ago

jesus-dayo commented 1 year 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 1 year 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));
}