I'm trying to mock a function used by another function which is being tested.
Unfortunately, the original function is called and not the mock.
Attached is a minimal reimplementation of the problem:
sandbox.ts:
export function* myGen(): Generator<string> {
yield "Hello";
yield "World";
}
export async function* myAsyncGen() {
yield* myGen();
}
export async function myFn() {
const gen = myAsyncGen();
for await (const chunk of gen) {
console.log("chunk", chunk);
}
}
sandbox.test.ts:
import * as sandboxMod from "./sandbox";
const { myFn } = sandboxMod;
describe("sandbox", () => {
it("should call the mock", async () => {
let called = false;
const fakeAsyncGen = async function* () {
called = true;
yield "Yo!";
};
jest.spyOn(sandboxMod, "myAsyncGen").mockImplementation(fakeAsyncGen);
await expect(myFn()).resolves.not.toThrow();
expect(called).toBe(true);
});
});
Any idea how I can make it work?