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' });
});
});
});
})
});
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(); });
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.
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.
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
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With