The handler module has the following structure:
handler.js
const isToBeUpdated = async (itemInputToSave) => {
// func implementation;
}
const handler = async (event) => {
// isToBeUpdated is used here within the try-catch block;
// on `catch` there's only console.error logging the error;
}
module.exports = { handler, isToBeUpdated };
I want to write a test the purpose of which is to check that if isToBeUpdated throws an error then the console.error in the catch part is called with the expected error.
I tried a few solutions, none of them worked. For example I tried putting this into a test:
const mockIsToBeUpdated = jest.fn().mockRejectedValueOnce(Error('isToBeUpdated Error'));
jest.mock('/path/to/the/module', () => {
return jest.fn().mockImplementationOnce(() => {
return {
isToBeUpdated: mockIsToBeUpdated,
};
});
});
await vec.handler({ Records: [record] });
expect(consoleError).toHaveBeenCalledWith(Error('isToBeUpdated Error'));
The consoleError function is a mocked console.error and it was done this way:
const consoleError = jest.spyOn(console, 'error').mockImplementation();
But how do I mock the isToBeUpdated function and make it return an error. It's an async function.