question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

expect .to.throw does not work with new Errors

See original GitHub issue

This 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:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:17 (11 by maintainers)

github_iconTop GitHub Comments

17reactions
meebercommented, Jul 18, 2016

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 to expect. The way you have it now, your function is being called and throwing an error before expect is called, so expect doesn’t have a chance to catch the error.

Working example:

  describe('An Immutable',  () => {
    it(' cannot accept a value different than OBJECT', () => {
      expect(() => require('../lib/iammutable')(1)).to.throw(TypeError, 'You need to send an OBJECT!')
    })
  })
5reactions
lucasfcostacommented, Jun 24, 2016

@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 an Error instance is passed. So the correct assertion here would be expect(test).to.throw(RangeError, 'Invalid date range');, and not the one with the new 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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found