Chai: should.throw and expect().to.throw assertions broken

Created on 28 Oct 2015  路  1Comment  路  Source: chaijs/chai

Both of the following assertions fail

'use strict';

var chai = require('chai');
var expect = chai.expect;
chai.should();

function throwError() {
throw Error('an error');
}

it('should doesnt consider equivalent errors equivalent?', function() {
(function() {
throwError();
}).should.throw(Error('an error'));
});

it('expect doesnt consider equivalent errors equivalent?', function() {
expect(function() {
throwError();
}).to.throw(Error('an error'));
});

with

1) should doesnt consider equivalent errors equivalent?:
AssertionError: expected [Function] to throw 'Error: an error' but 'Error: an error' was thrown
at Context. (index.js:14:18)

2) expect doesnt consider equivalent errors equivalent?:
AssertionError: expected [Function] to throw 'Error: an error' but 'Error: an error' was thrown
at Context. (index.js:20:14)

Both assertions should pass according to the docs
http://chaijs.com/api/bdd/#-throw-constructor-
http://chaijs.com/guide/styles/#should-extras

bug docs fun-size pull-request-wanted

Most helpful comment

Hey @sgen thanks for the issue!

Sadly the docs are slightly misleading here. We should definitely fix that.

If you pass in an instance (either new Error('foo') or Error('foo')) then the error is checked for referential equality (see here). The doc example:

expect(fn).to.not.throw(new RangeError('Out of range.'));

Is wrong - because fn throws a different RangeError (just because they have the same type and message doesn't matter, they're two different instances). The right way to do this would be, as demonstrated in the docs just above that:

expect(fn).to.throw(ReferenceError, /bad function/);

I'm really sorry that the docs are wrong here - I can imagine that must be frustrating :angry:. Let's get the docs fixed up and remove the incorrect example. If you'd like to make the change, and take the credit for it - then you just need to submit a PR deleting this line and we'll get round to updating the website soon :wink:

>All comments

Hey @sgen thanks for the issue!

Sadly the docs are slightly misleading here. We should definitely fix that.

If you pass in an instance (either new Error('foo') or Error('foo')) then the error is checked for referential equality (see here). The doc example:

expect(fn).to.not.throw(new RangeError('Out of range.'));

Is wrong - because fn throws a different RangeError (just because they have the same type and message doesn't matter, they're two different instances). The right way to do this would be, as demonstrated in the docs just above that:

expect(fn).to.throw(ReferenceError, /bad function/);

I'm really sorry that the docs are wrong here - I can imagine that must be frustrating :angry:. Let's get the docs fixed up and remove the incorrect example. If you'd like to make the change, and take the credit for it - then you just need to submit a PR deleting this line and we'll get round to updating the website soon :wink:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

liborbus picture liborbus  路  4Comments

danthegoodman picture danthegoodman  路  3Comments

kharandziuk picture kharandziuk  路  4Comments

andipavllo picture andipavllo  路  3Comments

zzzgit picture zzzgit  路  3Comments