I am mocking up some function:
export const natsWrapper = {
client: {
publish: jest
.fn()
.mockImplementation(
(subject: string, data: string, callback: () => void) => {
console.log("Mock function is called");
callback();
}
),
},
};
and then I am calling that in my test:
it("publishes an event when ticket is created", async () => {
await request(app)
.post("/api/tickets")
.set("Cookie", await (global as any).signin())
.send({ title: "title", price: 20 })
.expect(201);
expect(natsWrapper.client.publish).toHaveBeenCalled();
});
Jest logs in console “Mock function is called” but test still failing:
expect(jest.fn()).toHaveBeenCalled()
Expected number of calls: >= 1
Received number of calls: 0
2
It seems I imported mock function from __mock__
folder actually so it is not working that way, I had to import actual function which I want to mock.
2