I've tried a couple of approaches but I'm not sure if it's possible to mock a nested function in Jest. I read the official docs where they use axios, but I'm trying to mock a function that is imported, not a module.
for example, I have some function A, and within A I call B. I want to mock B so that it returns true once and then false once.
B is a function I imported that I wrote in another file, and I'm testing A.
so far I have tried to import function B into my test file and create a mock of it, but I don't think it's working how I thought it would
for example I have tried this
import { b } from "../util"
jest.mock("b", () => jest.fn().mockReturnValue(true));
and then my tested file would be
function a() => {
while b() {
do something;
}
}
should I rewrite my test or function I'm testing?
Not sure what kind of results you are getting, but for mocking b you could do:
jest.mock("../util", () => ({
b:jest.fn().mockReturnValueOnce(true).mockReturnValueOnce(false),
}));