jefflau / jest-fetch-mock

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

Promise .finally not working #151

Closed EthanML closed 4 years ago

EthanML commented 4 years ago

When attempting to test code that uses .finally on a Promise chain I get the following error:

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));

I came across https://github.com/jefflau/jest-fetch-mock/pull/58 which appeared to be intended to fix exactly this issue, so I'm confused why this is happening for me. Does anyone know why I'd still be seeing this error given the fix in that PR?

jefflau commented 4 years ago

Just looking at this now. I went and checked the PR and I checked the current package.json

Seems to be all there, so not sure why it wouldn't work

jefflau commented 4 years ago

@EthanML Did it work for you in the end?

EthanML commented 4 years ago

@jefflau It didn't, I just ended up with an implementation that didn't use finally 😅 Still not sure what the issue is/was.

gusa4grr commented 3 years ago

@EthanML it can be related to NodeJS version. If you're using lover that 10, finally keyword is not supported there and it will throw error