Custom Error Exceptions properties are not evaluated for strict equality
See original GitHub issue🐛 Bug Report
Custom properties on objects constructed from a Class
that extends Error
are not evaluated when matched with either .toThrow
or .toThrowError
.
To Reproduce
- Create a custom class that extends the
Error
object.
Class CustomError extends Error {
constructor(message, code) {
super(message);
this.code = code;
}
}
- Create a custom error object using class constructor.
const customError = new CustomError('something wrong happened', 'ERR_CUSTOM_CODE');
- In the code that needs to be tested, throw that custom error.
function fnThatThrowNoMatterWhat() {
throw customError;
}
- In the test, try to pass a new instance of that custom error object (created in the test file) and pass it to
.toThrowError
.
const expectedError = new CustomError('something wrong happened', 'WRONG_ERR_CUSTOM_CODE');
expect(() => main()).toThrowError(expectedError); // pass when it should fail
Expected behavior
All properties added to the object are checked strictly and not just message
.
Acceptance criteria:
- https://repl.it/repls/CurvyFrightenedNumber must fail
- https://repl.it/repls/ThirstySwelteringAggregators must pass
Link to repl or repo (highly encouraged)
Run npx envinfo --preset jest
Paste the results here:
System:
OS: Linux 4.15 Ubuntu 18.04.1 LTS (Bionic Beaver)
CPU: x64 Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
Binaries:
Node: 8.10.0 - ~/.nvm/versions/node/v8.10.0/bin/node
Yarn: 1.9.2 - /usr/bin/yarn
npm: 6.3.0 - ~/.nvm/versions/node/v8.10.0/bin/npm
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:5
Top Results From Across the Web
Strict equality (===) - JavaScript - MDN Web Docs
The strict equality (===) operator checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality...
Read more >How do I create a custom Error in JavaScript? - Stack Overflow
This confirmes the "problem" I ran into was the stack property of the error was the line number where new Error() was created,...
Read more >JavaScript Custom Errors, Extending Error - W3docs
In this chapter, we are going to dive into more details about errors in JavaScript. Learn what custom errors and extending error are...
Read more >Improving TypeScript error handling with exhaustive type ...
Discover an improved method for handling errors in TypeScript that solves problems that arise from returning null and throwing try...catch.
Read more >Custom errors, extending Error - The Modern JavaScript Tutorial
Low-level exceptions sometimes become properties of that object like err.cause in the examples above, but that's not strictly required. Tasks ...
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
Note:
.throws
returns the error object so you can do arbitrary tests on it..throw
expectation can be chained with other assertions like.with.property('code', 42)
.Here is a stack overflow answer for overcoming this limitation of jest.
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.