I have a method that handles error codes:
handleSignatureAppErrorCodes(code) {
if (code === 10) {
return this.handleError({
message: 'Configuration error, contact administrator',
});
} else if (code === 11) {
return this.handleError({ message: '
Certificate not available' });
} else if (code === 20) {
return this.handleError({ message: 'Wrong PIN, try again' });
} else if (code === 21) {
return this.handleError({ message: 'Key not found' });
} else if (code === 22) {
return this.handleError({ message: 'Certificate not found' });
} else if (code === 23) {
return this.handleError({ message: 'Unable to catch the key' });
} else if (code === 99) {
return this.handleError({
message: 'Unknown error, contact administrator',
});
}
}
handleError method:
private handleError(error: any): Promise<any> {
return Promise.reject(error.message || error);
}
I am writing unit tests in Jest, and this is how I covered each if statement (example is just for one if):
it('handleSignatureAppErrorCode 22', () => {
const spy = jest.spyOn(service, 'handleSignatureAppErrorCodes');
service.handleSignatureAppErrorCodes(22);
expect(spy).toHaveBeenCalled();
});
Tests pass, but I have a console error that I don't understand how to solve:
console.error
Unhandled Promise rejection: Certificate not found ; Zone: ProxyZone ; Task: null ; Value: Certificate not found undefined
I have tried using mockRejectedValue method, like this, but I would get error pointing to "new Error" without any message.
it('handleSignatureAppErrorCode 22', () => {
const spy = jest.spyOn(service, 'handleSignatureAppErrorCodes').mockRejectedValue(new Error('Certificate not found'));
service.handleSignatureAppErrorCodes(22);
expect(spy).toHaveBeenCalled();
});
Was looking here: How to test the type of a thrown exception in Jest, https://www.guidefari.com/jest-unhandled-promise-rejection/ and in other places with similar examples, but with no luck. Help and guidance appreciated!