I am writing a jest unit test for a function that makes multiple different database calls. I am able to mock the first db call just fine but I am having trouble mocking the others.
My function:
const sample = async () => {
const resultsFromCallOne = await dbClient.makeCall('...');
const resultsFromCallTwo = await dbClient.makeCall('...');
const resultsFromCallThree = await dbClient.makeCall('...');
}
My test file:
const mock = jest.spyOn(dbClient, 'makeCall');
mock.mockImplementation(() => Promise.resolve({
return [1, 2, 3];
}));
mock.mockImplementation(() => Promise.resolve({
return [4, 5, 6];
}));
mock.mockImplementation(() => Promise.resolve({
return [7, 8, 9];
}));
sample();
When I run that test, the results of all 3 db calls is equal to the last mock [7, 8, 9]
. Could someone please guide me on how to properly mock the three calls?
Thank you in advance!