mardi 10 juillet 2018

Nested mock functions in Jest

I'm currently trying to test out mocking some of my jest functions. One of the issues I'm having is trying to mock out a function that is called inside of another function. Here is a high level example of what I'm trying to do:

//Apple.js
function Apple(){
   return Orange(1, 2);
}

function Orange(arg1, arg2){
   return (arg1 + arg2);
}

I want to test the Apple function without making an actual call to Orange. What would be the code to mock out my orange function in a .spec.js file to make something like this happen? I was thinking something like the following but I doubt it:

//Apple.spec.js
import Apple from "Apple.js";
it("Should run Apple", () => {
   global.Orange = jest.fn().mockImplementation(() => {return 3});
   expect(Apple()).toEqual(3);
});

It's a really simple example but knowing this would definitely help me understand the next step in my project. I hope to hear from the community soon!

Aucun commentaire:

Enregistrer un commentaire