jefflau/jest-fetch-mock

jest-fetch-mock fails in Jest 27 and NodeJS 16

TigerWolf opened this issue · 6 comments

After upgrading to NodeJS 16 I got some wierd issues in my tests that used jest-fetch-mock.

With this simple test:

  it('throws error on bad response from server', () => {
    fetch.mockResponseOnce('{}', { status: 418 })
    expect(apiFetchUser('1234')).resolves.toEqual(new Error('Bad response from server'))
  })

If I run with Jest 26:

jest requests/__test__/users.test
 PASS  src/requests/__test__/users.test.js
  requests/users/apiFetchUser
    ✓ returns user on successful response (5 ms)
    ✓ throws error on bad response from server (2 ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        3.923 s
Ran all test suites matching /requests\/__test__\/users.test/i.
✨  Done in 6.20s.

With Jest 27 I get:

jest requests/__test__/users.test

 RUNS  src/requests/__test__/users.test.js
node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Error: expect(received).resolves.toEqual()

Received promise rejected instead of resolved
Rejected to value: {"message": "I'm a Teapot", "status": 418, "type": "generic", "url": ""}".] {
  code: 'ERR_UNHANDLED_REJECTION'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Is this an issue with something I am doing that has changed in Jest or is jest-fetch-mock not working correctly?

It looks like fetch isn't being mocked and is returning an error which is failing the test (I could be wrong).

The code it is testing is quite simple:

export const apiFetchUser = (ssoUserId: string) => {
  return fetch(fetchGet(`${ROOT_API_PATH}users/${ssoUserId}`))
    .then((response) => handleResponse(response))
    .then((response) => userAdapter(response))
}

Did you also upgrade to "node-fetch" 3.x or was this only a change in Jest versions?

I'm also seeing fetch not being mocked in Jest 27 but Node 14.

When I debug and look at fetch global right after imports are done, it's a mock. But then when my functions are running it's no longer a mock.

Updating from node-fetch@2.x to 3.x didn't fix it

Concerned about this though:

✗ npm ls | grep fetch
├─┬ @types/node-fetch@2.5.12
├─┬ jest-fetch-mock@3.0.3
│ ├─┬ cross-fetch@3.1.4
│ │ └── node-fetch@2.6.1
├─┬ node-fetch@3.0.0
│ └─┬ fetch-blob@3.1.2

Two versions of node-fetch, one is required by jest-fetch-mock?

npm find-dupes may help with multiple node-fetch versions.

Hmm, for me it's very different:

ReferenceError: Response is not defined

const ActualResponse = Response

That's with Node Version: v17.5.0
If i remember correctly, i had the same issue on v16 (This been blocking me for a while in the upgrade)

Is this library still being maintained?

On node 17 it fails with --experimental-fetch flag as well as it fails on v18.
Tried assigning globalThis.Response myself using cross-fetch and node-fetch without luck.

Is this library still being maintained?

Would be nice if this gets resolved as this blocking a lot of people from upgrading