At the moment, I am in the process of writing tests for a helper function that I created. This function takes a client and an array as parameters. Essentially, the function is designed to retrieve all tasks for all sites on a website. So far, the method is functioning correctly.
export async function getAllTasks(
sites: Site[],
client: Client
): Promise<Task[]> {
try {
return (
await Promise.all(
sites.map(async (site) => {
return client.getTasks(site.id);
})
)
).flat();
} catch (e) {
throw new createError(400, 'Bad Request: Unable to get all site tasks');
}
}
jest.mock('@src/client');
const mockClient = <jest.Mock<Client>>Client;
describe('Helpers', () => {
const mockTask = createMock<Task>();
mockClient.mockImplementation(() => {
return {
getTasks: jest.fn().mockReturnValue([mockTask]),
...mockClient.prototype
};
});
it('getAllTasks()', async () => {
const mockSites = [createMock<Site>()];
const mockClient = new mockClient({
// constructor params here
});
const tasks = await getAllTasks(mockSites, mockClient);
expect(tasks).toEqual([mockTask]);
});
});
A challenge has arisen during testing, resulting in failure with the following error:
expect(received).toEqual(expected) // deep equality
- Expected - 5
+ Received + 1
Array [
- Object {
- "customerId": 0,
- "id": "",
- "siteId": "",
- },
+ undefined,
]
The issue seems to be related to the mocked method returning an array with only one undefined element, causing the test to fail. Unraveling this problem might require some attention to detail, but I am struggling to identify the root cause. Any insights or assistance would be greatly appreciated. Thank you!