mercredi 31 janvier 2018

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' });
        });
      });
    });

  })

});

Aucun commentaire:

Enregistrer un commentaire