Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TigerWolf opened this issue Aug 13, 2021 · 6 comments
Open

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

TigerWolf opened this issue Aug 13, 2021 · 6 comments

Comments

@TigerWolf
Copy link

TigerWolf commented Aug 13, 2021

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))
}
@yinzara
Copy link
Collaborator

yinzara commented Sep 2, 2021

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

@jcollum
Copy link

jcollum commented Sep 2, 2021

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 [email protected] to 3.x didn't fix it

Concerned about this though:

✗ npm ls | grep fetch
├─┬ @types/[email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
├─┬ [email protected]
│ └─┬ [email protected]

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

@ryanrhee
Copy link

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

@nandi95
Copy link

nandi95 commented Feb 16, 2022

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)

@leegee
Copy link

leegee commented Jun 9, 2022

Is this library still being maintained?

@nandi95
Copy link

nandi95 commented Jul 1, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants