close

[Solved] How to test the type of a thrown exception in Jest

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error How to test the type of a thrown exception in Jest in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How How to test the type of a thrown exception in Jest Error Occurs?

Today I get the following error How to test the type of a thrown exception in Jest in javascript.

How To Solve How to test the type of a thrown exception in Jest Error ?

  1. How To Solve How to test the type of a thrown exception in Jest Error ?

    To Solve How to test the type of a thrown exception in Jest Error In case you are working with Promises:
    await expect(Promise.reject(new HttpException('Error message', 402))) .rejects.toThrowError(HttpException);

  2. How to test the type of a thrown exception in Jest

Solution 1

I use a slightly more concise version:

expect(() => {
  // Code block that should throw error
}).toThrow(TypeError) // Or .toThrow('expectedErrorMessage')

Solution 2

In case you are working with Promises:

await expect(Promise.reject(new HttpException('Error message', 402)))
  .rejects.toThrowError(HttpException);

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment