Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Won't These Jest Mocks Reset?

I have test code that is effecting other tests and causing them to fail. When I run test cases in isolation everything passes, but when I run the entire suit there are many failures. If you look at both tests below you can see I override a mocked module within the test to cause an exception to be thrown.

HttpService.post = jest.fn(() => {
   return Promise.reject({ payload: 'rejected' });
});

after this line has been run, all tests that need the original HttpService.post mock fail because they aren't reset. How can I properly restore my mock to the imported mock after this test? I have tried jest.resetMock in a beforeEach and about every jest method like it but nothing has worked. I know the answer is probably straight forward but I am confused with all of the differences I read about online around how code is imported (es6 import, commonJs). Thanks!

import HttpService from '../../services/httpService';
import handleErrors from '../../utilities/handleErrors';

jest.mock('../../services/httpService');
jest.mock('../../utilities/handleErrors');

describe('async actions', () => {

  beforeEach(() => {
    store = mockStore({});
  });

  describe('some describe that wraps both tests', () => {

    describe('a describe that wraps just the first test', () => {
      test(`creates ${constants.actions.REQUEST_SAVE_NOTE_FAILURE}`, () => {
        HttpService.post = jest.fn(() => {
          return Promise.reject({ payload: 'rejected' });
        });
        const expectedActions = [
          { type: constants.actions.REQUEST_SAVE_NOTE },
          { type: constants.actions.REQUEST_SAVE_NOTE_FAILURE, data: { payload: 'rejected' } },
        ];
        return store.dispatch(actions.saveNote({
          id: 1,
          note: 'note',
        })).then(() => {
          expect(store.getActions()).toEqual(expectedActions);
        });
      });
    });

    describe('a describe that wraps just the second test', () => {
      test(`creates ${constants.actions.REQUEST_SAVE_NOTE}
        and ${constants.actions.RECEIVE_SAVE_NOTE}`, () => {
        params = {
          body: {
            prospects: [1],
            note: 'note',
          },
        };
        const expectedActions = [
          { type: constants.actions.REQUEST_SAVE_NOTE },
          { type: constants.actions.RECEIVE_SAVE_NOTE, data: { payload: 'payload' } },
        ];

        return store.dispatch(actions.saveNote({
          id: 1,
          note: 'note',
        })).then(() => {
          expect(store.getActions()).toEqual(expectedActions);
          expect(HttpService.post).toBeCalledWith({ ...params, url: '/api/prospect/add-note' });
        });
      });
    });

  })

});
like image 862
withintheruins14 Avatar asked Jan 31 '18 15:01

withintheruins14


People also ask

How do I reset my mocks in Jest?

To reset Jest mock functions calls count before every test with JavaScript, we can call mockClear on the mocked function or clearAllMocks to clear all mocks. afterEach(() => { local. getData. mockClear(); });

Does Jest reset mocks between files?

You don't have to reset the mocks, as the test are run in parallel, every test file run in its own sandboxed thread. Even mocking JavaScript globals like Date or Math. random only affects the actual test file.

How do you reset mock Before each test?

Take a look at the following code snippet. //reset mock reset(calcService); Here we've reset mock object. MathApplication makes use of calcService and after reset the mock, using mocked method will fail the test.


1 Answers

IF you are wanting to mock a module (using a mock factory) for some tests within a file, but unmock it for others - this works for me:

describe("some tests", () => {
  let subject;

  describe("with mocks", () => {
    beforeAll(() => {
      jest.isolateModules(() => {
        jest.doMock("some-lib", () => ({ someFn: jest.fn() })); // doMock isnt hoisted with babel
        subject = require('./module-that-imports-some-lib');
      });
    });

    // ... tests when some-lib is mocked
  });

  describe("without mocks - restoring mocked modules", () => {
    beforeAll(() => {
      jest.isolateModules(() => {
        jest.unmock("some-lib");
        subject = require('./module-that-imports-some-lib');
      });
    });

    // ... tests when some-lib is NOT mocked

  });
});
like image 193
Ben Avatar answered Sep 21 '22 12:09

Ben