Home > Blockchain >  How to resolve console error unhandled promise rejection when unit testing?
How to resolve console error unhandled promise rejection when unit testing?

Time:05-25

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!

CodePudding user response:

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);

  • Related