I'm trying to spyOn exported function that another function in the same file calls.
I can't seem to get Jest to use the spy at all.
Tried with a mixture of mockImplementation and mockReturnValue.
utils.ts:
export const func1 = (x: number) => {
const y = x + 1
return y
}
export const func2 = (a: number) => {
const b = a + 2
return b
}
export const func3 = (x: number, y: number) => {
const foo = func1(x)
const bar = func2(y)
return foo + bar
}
utils.test.ts:
import * as sut from './utils'
describe('test', () => {
test('func3 spys', () => {
let func1Spy: jest.SpyInstance
let func2Spy: jest.SpyInstance
func1Spy = jest.spyOn(sut, 'func1').mockImplementation(() => 8)
func2Spy = jest.spyOn(sut, 'func2').mockImplementation(() => 10)
const result = sut.func3(1, 2)
expect(func1Spy).toHaveBeenCalled()
expect(func2Spy).toHaveBeenCalled()
expect(result).toBe(6)
})
})
Expected behavior: Jest sees that the spy'd function has been called Actual output:
Expected number of calls: >= 1
Received number of calls: 0
Commenting out the spy call assertions give me a passing test so the function is running correctly. Just without the spys