jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
886 stars 118 forks source link

fix #54: allow Promise to support Promise.finally #58

Closed AllenFang closed 6 years ago

AllenFang commented 6 years ago

Fix #54

@jefflau

EthanML commented 4 years ago

@AllenFang I came across this fix because I'm running into this error when trying to run tests on code that makes use of finally.

TypeError: fetch(...).then(...).then(...).catch(...).finally is not a function

Code in question:

fetch(myUrl)
      .then(response => {
        if (response.status === 200) {
          return response.json()
        }
        throw new Error('request failed.');
      })
      .then(({ data }) => doStuff(data))
      .catch(e => { setError(e.message) })
      .finally(() => doOtherStuff());

The test makes use of the following fetch mocking function:

fetchMock.mockResponseOnce(JSON.stringify(myResponseBody));

Does anyone know why I'd still be seeing this error given the fix in this PR?