mirror of
https://github.com/chaijs/chai
synced 2024-11-15 08:17:14 +00:00
improved throw
type detection and messaging
This commit is contained in:
parent
0165fc068a
commit
89b107ca77
1 changed files with 13 additions and 9 deletions
|
@ -766,21 +766,25 @@ Assertion.prototype.keys = function(keys) {
|
|||
Assertion.prototype.throw = function (constructor) {
|
||||
new Assertion(this.obj).is.a('function');
|
||||
|
||||
constructor = constructor || Error;
|
||||
var name = constructor.name
|
||||
, thrown = false;
|
||||
var thrown = false;
|
||||
|
||||
try {
|
||||
this.obj();
|
||||
} catch (err) {
|
||||
thrown = true;
|
||||
this.assert(
|
||||
err instanceof constructor
|
||||
, 'expected ' + this.inspect + ' to throw ' + name
|
||||
, 'expected ' + this.inspect + ' to not throw ' + name);
|
||||
return this;
|
||||
if (constructor) {
|
||||
this.assert(
|
||||
err instanceof constructor && err.name == constructor.name
|
||||
, 'expected ' + this.inspect + ' to throw ' + constructor.name + ' but a ' + err.name + ' was thrown'
|
||||
, 'expected ' + this.inspect + ' to not throw ' + constructor.name );
|
||||
|
||||
return this;
|
||||
} else {
|
||||
thrown = true;
|
||||
}
|
||||
}
|
||||
|
||||
var name = (constructor ? constructor.name : 'an error');
|
||||
|
||||
this.assert(
|
||||
thrown === true
|
||||
, 'expected ' + this.inspect + ' to throw ' + name
|
||||
|
|
Loading…
Reference in a new issue