expect .to.throw does not work with new Errors
See original GitHub issueThis is my test:
it('should throw a RangeError when the end date is before the start date', function () {
var dateRange = {
start: '2015-04-01',
end: '2014-04-29',
};
var error = new RangeError('Invalid date range');
var test = function () {
throw error;
};
expect(test).to.throw(new RangeError('Invalid date range'));
});
which fails with the message:
AssertionError: expected [Function] to throw 'RangeError: Invalid date range' but 'RangeError: Invalid date range' was thrown
at Context.<anonymous> (test/widgets/staff-planning-projection.spec.js:90:1)
I couldn’t find any explanations / solutions / workarounds for this, so I think it’s a bug with the expect(x).to.throw
assertion. As a redundancy check, I tried:
expect(test).to.throw(error);
and that passed. Any thoughts?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:3
- Comments:17 (11 by maintainers)
Top Results From Across the Web
How to test a function that's expected to throw error in jest…
Say, I want to write a test for the function below and want to ensure I test if it actually fails when the...
Read more >Jest: test that exception will be thrown isnt working
If you want to test that a specific error gets thrown, you can provide an argument to toThrow. The argument can be a...
Read more >Expect a function to throw an exception in Jest - eloquent code
First we define the async function in a module, then in the test code we use the rejects property to test for any...
Read more >How to Correctly Expect an Error in Jest
We can do this by simply passing the function to the expect without actually invoking it, and calling the toThrow method on it...
Read more >Testing exceptions in JavaScript with Jest - Codeleak.pl
The code. Let's consider a simple function that checks for equality of two passwords, and it throws an error when the first one...
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
Hi @suissa. What you’re running into is something different, and not a bug. You need to pass a function to
expect
instead of calling the function and passing its return value toexpect
. The way you have it now, your function is being called and throwing an error beforeexpect
is called, soexpect
doesn’t have a chance to catch the error.Working example:
@keithamus The behavior of checking for both the error and the message when they exist has been fixed, but we took the design decision of doing strict (
===
) comparisons when anError
instance is passed. So the correct assertion here would beexpect(test).to.throw(RangeError, 'Invalid date range');
, and not the one with thenew
operator (since passing an error instance triggers strict comparison).We did this in order to enable users to choose between both behaviors (strict comparisons and a comparison using the constructor and/or message).