jest-fetch-mock icon indicating copy to clipboard operation
jest-fetch-mock copied to clipboard

fetch.resetMocks not resetting fetch

Open boazhoch opened this issue 7 years ago • 4 comments

So as the title implies fetch.mockResets() isn't resetting the the fetch.

here is my full test.js:

import Worker from '../statistics-worker/worker';
jest.mock("../statistics-worker/worker");
const workerInitProps = {
  interactionId: '123',
  interactionSubmissionUrl: 'submission',
  fetchUrl: 'fetch'
};
const statService = Statistic.getNewInstance(new Worker(workerInitProps));
describe('Statistic serivce ', () => {
  beforeEach(() => {
    fetch.resetMocks();
  });
  it('Should init worker', () => {
    expect(statService).toBeInstanceOf(Statistic);
  });
  it('should fetch statistics', () => {
    fetch.mockResponseOnce(
      JSON.stringify({
        payload: {
          statistics: {
            1: 10,
            2: 11
          },
          count: 10,
          finalResults: []
        }
      })
    );
    statService
      .fetchStatistics()
      .then(res => {
        expect(res).toEqual({
          statistics: {
            1: {
              options: 10,
              totalVotes: 0
            },
            2: {
              options: 11,
              totalVotes: 0
            }
          },
          count: 10,
          finalResults: []
        });
      })
      .catch(err => {
        expect(err).toBeFalsy();
      });
  });

  it('should deadlock fetch statistics', () => {
    fetch.mockRejectOnce(new Error('fake error message'));

    const statistics = Statistic.getNewInstance(new Worker(workerInitProps));
    statService
      .fetchStatistics()
      .then(res => {
        console.log(res);
        expect(res).toEqual({
          count: 0,
          statistics: {},
          dead: true
        });
      })
      .catch(err => {
        expect(err).toBeFalsy();
      });
  });
});

The result i get is:

Expected value to equal:
  {"count": 0, "dead": true, "statistics": {}}
Received:
  {"count": 10, "finalResults": [], "statistics": {"1": {"options": 10, "totalVotes": 0}, "2": {"options": 11, "totalVotes": 0}}}

boazhoch avatar Jun 18 '18 08:06 boazhoch

Did you manage to fix this? I finally had some time recently to look through the example. I'm a bit confused why it's going into the .then when it should just hit the catch if you're rejecting.

jefflau avatar Aug 01 '18 16:08 jefflau

Struggling with this here as well, always returns the value provided in the first test with mockOnce(...) even for one of the tests where I use fetchMock.mockRejectOnce()

Looks like the request URL needs to be different to get different response. I find this quite confusing. Setting a mock response in one test should not be returned in another test. Even resetMocks() has no effect.

zoechi avatar Jan 19 '22 19:01 zoechi

Has anyone been able to work this one out? Running into this issue myself. The mocked response is always from the test run previously. resetMocks doesn't seem to do anything.

nizur avatar Sep 21 '23 21:09 nizur

For anyone stumbling upon this: Save yourself the hours of troubleshooting and just use msw: https://mswjs.io/docs/integrations/node/

resetMocks() in this lib definitely doesn't work which makes it useless for anyone developing real software tests.

Brawl345 avatar Jul 23 '24 10:07 Brawl345