Inconsistency in `rejects.toThrow()`
See original GitHub issue🐛 Bug Report
The doc says the argument of toThrow()
can be a class for the error, it works when a promise throws an Error
instance, but fails when the promise throws a custom object.
To Reproduce
describe('When a promise throws an expression, rejects.toThrow should match the expression by its type', () => {
it('when the expression is an error', async () => {
const p = new Promise(() => {
throw new Error('some-info');
});
await expect(p).rejects.toThrow(Error); // passed
});
it('when the expression is a custom object', async () => {
class Exception {
constructor(public readonly message: string) {
}
}
const p = new Promise(() => {
throw new Exception('some-info');
});
await expect(p).rejects.toThrow(Exception); // failed
});
});
The first test passes, while the second test fails with the following message:
Expected the function to throw an error of type:
"Exception"
But it didn't throw anything.
Expected behavior
Both the above two tests pass.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Better Error Handling In NodeJS With Error Classes
This article is for JavaScript and NodeJS developers who want to improve error-handling in their applications.
Read more >Paulo Freire's Pedagogy of the Oppressed
The justification for a pedagogy of the oppressed; the contradiction between the oppressors and the oppressed, and how it is overcome;.
Read more >Safari Technology Preview Release Notes - Apple Developer
toMatrix() to throw an exception if its length is not compatible with a px unit ... Fixed Navigator.share() rejecting with the wrong exception...
Read more >Considering When To Throw Errors, Why To Chain Them, And ...
Inconsistent error handling can lead to many problems in a system such as duplicated code, overly-complex algorithms, error logs that are too ...
Read more >Parenting Practices and Child Disruptive Behavior Problems ...
Correspondingly, inconsistency is a second parenting practice that has often been linked with elevated rates of child oppositional and aggressive behavior ( ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is also a problem when throwing objects in async/await that are not Error instances and expecting the
toThrow()
default with no arguments:This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.