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 '../myFunctions.js'; describe('resetModal', () => { it('calls the clearSomethingInModal function', () => { const clearSomethingInModal = jest.fn(); resetModal(); expect(clearSomethingInModal.mock.calls.length).toBe(1); }) })
myFunctions.js
export resetModal() { clearSomethingInModal() }
However, Jest output says that it has not been called. How can I best do this?
To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.
You can create a namespace that you export as the default object and call b using the namespace. This way, when you call jest. mock it will replace the b function on the namespace object. const f = require('./f'); jest.
Calling a function from within itself is called recursion and the simple answer is, yes.
You can mock functions in two ways: either you create a mock function to use in test code, or you write a manual mock that overrides a module dependency. Let's take for example the case where we're testing an implementation of a function forEach, which will invoke a callback for each item in a supplied array.
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() }
Another way is to use done
and mock or spy on the implementation of the last function and check if the previous function was called by then.
it('should call function2 after function1', (done) => { expect.assertions(2) function2.mockImplementationOnce(() => { expect(function1).toHaveBeenCalled() done() }) act() // This is where you run function you are testing })
The drawback of this solution is that the structure of the test is not
// arrange // act // assert
but rather
// arrange & assert // act
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