mardi 30 juin 2020

Expected mock function to have been called one time, but it was called zero times - Jest unit test

I'm trying to write unit test(jest) for my status change API (MolecularJs).Therefor i have tried to mock my db .but i'm getting below error.

expect(jest.fn()).toBeCalledTimes(1)

Expected mock function to have been called one time, but it was called zero times.
  26 |          
> 27 |             expect(mockStatus).toBeCalledTimes(1);
     |                                ^
  28 |             expect(mockStatus).toBeCalledWith({ campaignId: "5ef5746bcb5485210fa1f0fe",status: 0 });
  29 |         });

test.js

describe("Test service", () => {
    let broker = new ServiceBroker();
    let moService=broker.createService(MoService);  

    const mockStatus = jest.fn(params =>    
        Promise.resolve({ campaignId: params.campaignId, status: params.name })
    );

    beforeAll(() => broker.start());
    afterAll(() => broker.stop());
    
    describe("Test Campaign status change API", () => {
        it("should return inactive", async () => {
            moService.adapter.insert = mockStatus;

            let result = await broker.call("inbox.changeStatus", { campaignId: "5ef5746bcb5485210fa1f0fe",status: 0  });
            
        expect(result.status).toBe(0);
            
            expect(mockStatus).toBeCalledTimes(1);
            expect(mockStatus).toBeCalledWith({ campaignId: "5ef5746bcb5485210fa1f0fe",status: 0 });
        });
    });
});

Aucun commentaire:

Enregistrer un commentaire