Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

fetchMock.mock.calls never updates (possibly jest 26) #26

Open
green-arrow opened this issue Dec 22, 2020 · 4 comments · May be fixed by #28
Open

fetchMock.mock.calls never updates (possibly jest 26) #26

green-arrow opened this issue Dec 22, 2020 · 4 comments · May be fixed by #28
Labels
bug Something isn't working

Comments

@green-arrow
Copy link

After upgrading our codebase to jest 26 and this library to 1.5.1, our tests that used the .toHaveBeenCalled assertions stopped working. It appears that the .mock property is never updated with the calls to the spy.

I tried digging into the code a bit, and it seems like this wrapper does the Object.assign(jestifiedInstance.mock, spy.mock) only once, so those aren't kept in sync. Unfortunately fetchMock.mock is also a method, so we can't just assign jestifiedInstance.mock = spy.mock.

I tinkered around with the code locally and got something working by handling the fetchHandler and mock inside the proxy. It seems to run on our codebase without issue. I'd be happy to spin up a PR if you're interested!

@wheresrhys
Copy link
Owner

Yep that would be good - thanks

@green-arrow green-arrow linked a pull request Dec 22, 2020 that will close this issue
@alexpovh
Copy link

alexpovh commented Mar 5, 2021

Same issue here, happens only when clearMocks is set to true in jest configuration.

@SpencerKaiser
Copy link

Just to add onto the content above, I'm having the exact same issue... I have clearMocks enabled in my Jest config and I spent the last two hours debugging this:

// ✅
expect(fetchMock).toHaveLastFetched(
  onboardingEndpoint,
  expect.objectContaining({
    method: 'POST',
    headers: {
      'content-type': 'application/json',
    },
    body: JSON.stringify({ name }),
  }),
);

// ❌
expect(fetch).toHaveBeenCalled();

// ✅
expect(fetchMock).toHaveLastFetched(onboardingEndpoint);

// ❌
expect(fetchMock).toHaveLastFetched(expect.anything());

So yea... that was quite rough 😅 @wheresrhys do you think that last example is related? If not or if you aren't sure, I'll open up a new issue for it.

@th3fallen
Copy link

same here, but removing clearMocks in our config is a no go as our tests go BANANAS

@wheresrhys wheresrhys added the bug Something isn't working label Jul 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants