Skip to content

Commit

Permalink
assert: .throws() returns thrown error. Closes #185
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalparadox committed Jan 29, 2014
1 parent b01eba7 commit c3c1fcc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1057,10 +1057,11 @@ module.exports = function (chai, _) {
, (desiredError instanceof Error ? desiredError.toString() : desiredError)
, (err instanceof Error ? err.toString() : err)
);
flag(this, 'object', err);

flag(this, 'object', err);
return this;
}

// next, check constructor
if (constructor) {
this.assert(
Expand All @@ -1076,6 +1077,7 @@ module.exports = function (chai, _) {
return this;
}
}

// next, check message
var message = 'object' === _.type(err) && "message" in err
? err.message
Expand All @@ -1089,8 +1091,8 @@ module.exports = function (chai, _) {
, errMsg
, message
);
flag(this, 'object', err);

flag(this, 'object', err);
return this;
} else if ((message != null) && errMsg && 'string' === typeof errMsg) {
this.assert(
Expand All @@ -1100,8 +1102,8 @@ module.exports = function (chai, _) {
, errMsg
, message
);
flag(this, 'object', err);

flag(this, 'object', err);
return this;
} else {
thrown = true;
Expand All @@ -1127,7 +1129,8 @@ module.exports = function (chai, _) {
, (desiredError instanceof Error ? desiredError.toString() : desiredError)
, (thrownError instanceof Error ? thrownError.toString() : thrownError)
);
flag(this, 'object', null);

flag(this, 'object', thrownError);
};

Assertion.addMethod('throw', assertThrows);
Expand Down
3 changes: 2 additions & 1 deletion lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,8 @@ module.exports = function (chai, util) {
errt = null;
}

new Assertion(fn, msg).to.Throw(errt, errs);
var assertErr = new Assertion(fn, msg).to.Throw(errt, errs);
return flag(assertErr, 'object');
};

/**
Expand Down
4 changes: 4 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ describe('assert', function () {
assert.throws(function() { throw new Error('bar'); }, Error);
assert.throws(function() { throw new Error('bar'); }, Error, 'bar');

var thrownErr = assert.throws(function() { throw new Error('foo'); });
assert(thrownErr instanceof Error, 'assert.throws returns error');
assert(thrownErr.message === 'foo', 'assert.throws returns error message');

err(function () {
assert.throws(function() { throw new Error('foo') }, TypeError);
}, "expected [Function] to throw 'TypeError' but 'Error: foo' was thrown")
Expand Down

0 comments on commit c3c1fcc

Please sign in to comment.