I'm trying to mock out a single function in a module with Jest and assert that I have called it with certain parameters. I have a file that roughly looks like so:
export const f = () => {
...
}
export const g = () => {
...
f(...)
}
export const h = () => {
...
g(...)
}
I'm trying to test out functions g
and h
, and one the assertions I'm trying to write is that f
gets called with certain parameters when calling g
and h
. So in my tests, I want to mock out f
and be able to assert what it was called with. However, when I do something like this in my tests:
import f, g, h from 'module'
jest.mock('module', () => {
const original = jest.requireActual('module')
return {
...original,
f: jest.fn()
}
})
test('g calls f correctly', () => {
g()
// I want to assert that f was called with some parameters
})
I have no reference to f
, and it seems like when g
gets called in the test, the actual function f
is being called rather than a mock. What do I need to change here to get this working?