I'm having trouble mocking the catch block in jest for the code snippet
throw Error(JSON.stringify(studentErrorRes));
. While I can partially verify that an error is thrown, I'm unable to mock the error message properly. Typically, I use .mockRejectedValue
to mock errors, but it's not working in this scenario. Can someone assist me on how to properly mock this?
During jest mocking, I can confirm that an error is thrown, but how can I verify the exact error message? If there are multiple keys within const studentErrorRes
, how can I ensure that all keys have the expected values in my mock? I hope I'm not overcomplicating things.
import { SNSEvent } from 'aws-lambda';
export const studentAPIGetHandler = async (event: SNSEvent): Promise<any> => {
try {
const studentID = event.studentInfo.studentID;
const studentPortal = StudentService.getStudentInfo(studentID);
} catch (error) {
const studentErrorRes = {
apiName: SuudentAPIName.Student_Message,
myMessage: 'Unable to get student API response',
};
logger.error(studentErrorRes.myMessage, error);
throw Error(JSON.stringify(studentErrorRes));
}
};
Part of test case for catch block
it("Catch block test for error", async () => {
try {
await studentAPIGetHandler(event);
} catch(e) {
expect(e).toThrowError;
// this verifies that error is thrown , but not exact error message
}
});