I would like to test a Vue method that uses the return value of another method in an if-statement. I would like to do this in isolation, ie not test both methods at the same time, so it seems like creating a mock function with a static return value is the right thing to do.
However using jest.fn().mockReturnValue(SomeValueToReturn)
is not working for me. In the simplified example below methodIWantToTest
still captures the return value of the component method, not the mocked one I created.
// MyComponent.vue
{
// component implementation ...
methods: {
testCondition() {
return true; /* some logic with boolean return */
}
methodIWantToTest() {
if(this.testCondition()) {
return 'condition_passed';
}
return 'condition_failed';
}
}
}
// MyComponent.spec.js
it('should return "condition_failed"', () => {
// changing this to be different than the `true` returned in the component
// method for demonstration purposes
wrapper.vm.testCondition = jest.fn().mockReturnValue(false);
const received = wrapper.vm.methodIWantToTest();
expect(received).toBe('condition_failed');
})
received
here is the return value of the component method, not the mocked return value