jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
881 stars 116 forks source link

Add support for `AbortSignal.timeout()` `TimeoutError` #242

Open Radi-Mortada opened 1 year ago

Radi-Mortada commented 1 year ago

Currently the abort signal error is forced to be as following: https://github.com/jefflau/jest-fetch-mock/blob/master/src/index.js#L88 source code:

const abortError = () =>
  new DOMException('The operation was aborted. ', 'AbortError')

My test mock:

    const abortSignalTimeout = jest.spyOn(AbortSignal, 'timeout');

    beforeAll(() => {
      fetchMock.mockResponseOnce(() => {
        const promise = new Promise((resolve) => {
          setTimeout(() => {
            resolve('resolved after timeout of `5`');
          }, 5);
        });

        return promise;
      });

      const signal = AbortSignal.timeout(1);

      abortSignalTimeout.mockReturnValue(signal);
    });

there is no way to obtain the correct abort error TimeoutError instead of AbortError https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout