Moleculer - how to mockup method inside jest?

610 views Asked by At

To mockup using Jest, we usually use jest.spyOn. But how to mockup method inside Moleculer service?

1

There are 1 answers

0
Icebob On BEST ANSWER

Just overwrite it after the service created in your test case. Example:

describe("Test MyService", () => {
    const broker = new ServiceBroker({ logger: false });
    const service = broker.createService(MyService);

    // Mock my method in service instance
    service.myMethod = jest.fn();

    it("should call myMethod", () => {
        service.doSomething();
        expect(service.myMethod).toHaveBeenCalledTimes(1);
    });
});