I'm using TypeORM.
It works fine at the local with real DB connection.
But when I run the test with Jest, the test gets failed.
I mocked the TypeORM method I needed, and expected all these methods to be called.
Test is failing on expect(typeorm.getRepository(Covid).save).toHaveBeenCalled();
it is not being called.
I cannot understand why this test code is not calling.
Does anyone know the solution?
Here is the code.
describe('getCoronaData', () => {
it('should get a Covid Status data and return it', async () => {
typeorm.getRepository = jest.fn().mockReturnValue({
findOne: jest.fn().mockResolvedValue(null),
create: jest.fn(),
save: jest.fn(),
});
await getCoronaData();
expect(typeorm.getRepository).toHaveBeenCalledWith(Covid);
expect(typeorm.getRepository(Covid).findOne).toHaveBeenCalled();
expect(typeorm.getRepository(Covid).save).toHaveBeenCalled(); // this expect result is making an error
// Expected number of calls: >= 1
// Received number of calls: 0
});
});
question from:https://stackoverflow.com/questions/65918365/jest-unit-test-failing-on-the-function-which-returns-a-asyncronous-method