0

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!

fadingbeat
  • 355
  • 3
  • 16
  • You _don't_ handle the promise, no. Also your test is pretty pointless - you spy on a method, call the spy, then assert that you called the spy. It's even worse when you `mockRejectedValue`, because then the real implementation isn't even involved. You need to handle the rejection when you _call_ the method, see https://jestjs.io/docs/asynchronous for options. – jonrsharpe May 23 '22 at 12:57

1 Answers1

1

First off, your test is useless.

You spy a function, then call it, and expect it to be called ?

That's the real life equivalent of "I will call my mom in the other room, and when she's in the same room as me, I expect to have yelled MOOOOOM"

So, let's make a proper test :

it('Should return a given statement on error 22', (done) => {
  service.handleSignatureAppErrorCodes(22).catch((message) => {
    expect(message).toBe('Certificate not found');
    done();
  });
});

You do not need to mock anything here. The handleError is private, meaning you should not test it directly. This is called an implementation detail.

Also, since you catch the error here, you should not see it pop in the console either. 2 birds, one stone !

EDIT Bonus syntax to make it nicer to look at (switch hare syntax-heavy) :


const errorCodes = [];
errorCodes[22] = 'Certificate not found';
//  + The other ones
// ...
const errorMsg = errorCodes[code] ?? 'Default message';

return Promise.reject(errorMsg);

  • Thank you for your reply. Not sure why `message` is in curly brackets, I removed those and then I get the expected value. However, this does not resolve the issue. The test passes, but the console error is still there. – fadingbeat May 23 '22 at 13:25
  • 1
    My bad, I thought you had an object passed as the error, I am removing those from my snippet. But if the error is still in the console, it means you have written another test elsewhere, because caught promises do not produce an error message. –  May 23 '22 at 14:12
  • 1
    And that was my bad, the initial test wasn't deleted. Thank you for your help! – fadingbeat May 24 '22 at 12:54