I am in the process of converting my project from Vue 2 to 3 and also testing library to Vue test utils 2. Before, mocking methods works like magic:
export default {
methods: {
greet(text) {
sayHello('hello world')
},
sayHello(text) {
console.log(text)
}
}
}
test('should sample', async () => {
const spy = jest.spyOn(wrapper.vm, 'sayHello')
wrapper.vm.greet('hello world')
expect(spy).toHaveBeenCalledWith('hello world')
})
I tried converting it to composition api:
export default defineComponent({
setup() {
function greet(text) {
sayHello(text)
}
function sayHello(text) {
console.log(text)
}
return {
greet,
sayHello
}
}
})
and now testing is not working at all
expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: "hello world"
Number of calls: 0
How do I mock functions that call another functions in Vue 3 + Vue test utils 2?