diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index acb5c7971..18ef404e2 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -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( @@ -1076,6 +1077,7 @@ module.exports = function (chai, _) { return this; } } + // next, check message var message = 'object' === _.type(err) && "message" in err ? err.message @@ -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( @@ -1100,8 +1102,8 @@ module.exports = function (chai, _) { , errMsg , message ); - flag(this, 'object', err); + flag(this, 'object', err); return this; } else { thrown = true; @@ -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); diff --git a/lib/chai/interface/assert.js b/lib/chai/interface/assert.js index ec5260618..7447802d8 100644 --- a/lib/chai/interface/assert.js +++ b/lib/chai/interface/assert.js @@ -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'); }; /** diff --git a/test/assert.js b/test/assert.js index 29f37edc1..62260ede0 100644 --- a/test/assert.js +++ b/test/assert.js @@ -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")