'How to test an exception was not thrown with Jest?
The Jest docs do not demonstrate a way of asserting that no exception was thrown, only that one was.
expect(() => ...error...).toThrow(error)
How do I assert if one was not thrown?
Solution 1:[1]
You can always use the .not
method, which will be valid if your initial condition is false. It works for every jest test:
expect(() => ...error...).not.toThrow(error)
Solution 2:[2]
In my case the function being tested was asynchronous and I needed to do further testing after calling it, so I ended up with this:
await expect(
foo(params),
).resolves.not.toThrowError();
// My other expects...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | agilgur5 |
Solution 2 |