How to resolve console error unhandled promise rejection when unit testing? -- [Question Asked]

Query asked by user

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!

Answer we found from sources

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

Answered By – temp_user

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0


What is Angular?

Angular is an open-source, JavaScript outline written in TypeScript. Google keeps up with it, and its basic role is to foster single-page activities. As an edge, Angular enjoys clear benefits while likewise outfitting a standard design for formulators to work with. It empowers stoners to deliver huge tasks in a viable way. textures overall lift web improvement viability and execution by outfitting an agreeable construction so that formulators do n't need to continue to modify regulation from scratch. textures are efficient devices that offer formulators a large group of extra elements that can be added to programming easily.

However, is JavaScript ideal for creating single-sprinter activities that bear particularity, testability, and trend-setter efficiency? maybe not.

JavaScript is the most by and large utilized client-side prearranging language. It's composed into HTML reports to empower relations with web sprinters in endless extraordinary ways. As a genuinely simple to-learn language with inescapable help, creating current operations is appropriate.

Nowadays, we have various textures and libraries intended to give essential outcomes. As for front end web advancement, Angular addresses incalculable, while possibly not all, of the issues formulators face while utilizing JavaScript all alone.
Who we are?

We are team of software engineers in multiple domains like Programming and coding, Fundamentals of computer science, Design and architecture, Algorithms and data structures, Information analysis, Debugging software and Testing software. We are working on Systems developer and application developer. We are curious, methodical, rational, analytical, and logical. Some of us are also conventional, meaning we're conscientious and conservative.

Answer collected from stackoverflow and other sources, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0