I am trying to mock a function call, and expect it to have called another function within it once.
myFunctions.test.js
import { resetModal } from \'
Your approach does not work because you mock clearSomethingInModal
only in the context of your test file, so clearSomethingInModal
in the myFunctions.js
is still the original. The main point is that you can't mock something that is directly created in myFunctions.js
. The only thing that you can mock are:
myFunctions.js
, like import clearSomethingInModal from 'clearSomethingInModal'
;This makes sense if you think about myFunctions.js
as a blackbox, where you can control what goes in, like imports or function arguments, and where you can test what comes out. But you can not test the stuff that happens inside the box.
Here are two example that reflect the 2 points in the list:
myFunctions.test.js
import { resetModal } from '../myFunctions.js';
import clearSomethingInModal from 'clearSomethingInModal';
jest.mock('clearSomethingInModal', () => jest.fn())
describe('resetModal', () => {
it('calls the clearSomethingInModal function', () => {
resetCreationModal();
expect(clearSomethingInModal.mock.calls.length).toBe(1);
})
})
myFunctions.js
import clearSomethingInModal from 'clearSomethingInModal';
export resetModal() {
clearSomethingInModal()
}
myFunctions.test.js
import { resetModal } from '../myFunctions.js';
describe('resetModal', () => {
it('calls the clearSomethingInModal function', () => {
const clearSomethingInModal = jest.fn();
resetCreationModal(clearSomethingInModal);
expect(clearSomethingInModal.mock.calls.length).toBe(1);
})
})
myFunctions.js
export resetModal(clearSomethingInModal) {
clearSomethingInModal()
}