Skip to content

Commit

Permalink
Increasing Throws error message verbosity
Browse files Browse the repository at this point in the history
'cause we actually want to know what was thrown.
  • Loading branch information
scottnonnenberg committed Sep 5, 2012
1 parent e383a6e commit 941f992
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
16 changes: 11 additions & 5 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,8 @@ module.exports = function (chai, _) {

var thrown = false
, desiredError = null
, name = null;
, name = null
, thrownError = null;

if (arguments.length === 0) {
errMsg = null;
Expand Down Expand Up @@ -926,8 +927,8 @@ module.exports = function (chai, _) {
if (constructor) {
this.assert(
err instanceof constructor
, 'expected #{this} to throw ' + name + ' but a ' + err.name + ' was thrown'
, 'expected #{this} to not throw ' + name );
, 'expected #{this} to throw ' + name + ' but ' + _.inspect(err) + ' was thrown'
, 'expected #{this} to not throw ' + name + ' but ' + _.inspect(err) + ' was thrown');
if (!errMsg) return this;
}
// next, check message
Expand All @@ -949,15 +950,20 @@ module.exports = function (chai, _) {
return this;
} else {
thrown = true;
thrownError = err;
}
}

var expectedThrown = name ? name : desiredError ? _.inspect(desiredError) : 'an error';
var actuallyGot = ''
if (thrown) {
actuallyGot = ' but ' + _.inspect(thrownError) + ' was thrown'
}

this.assert(
thrown === true
, 'expected #{this} to throw ' + expectedThrown
, 'expected #{this} to not throw ' + expectedThrown
, 'expected #{this} to throw ' + expectedThrown + actuallyGot
, 'expected #{this} to not throw ' + expectedThrown + actuallyGot
);
};

Expand Down
2 changes: 1 addition & 1 deletion test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ suite('assert', function () {

err(function () {
assert.doesNotThrow(function() { throw new Error('foo'); });
}, 'expected [Function] to not throw an error');
}, 'expected [Function] to not throw an error but [Error: foo] was thrown');
});

test('ifError', function() {
Expand Down
14 changes: 7 additions & 7 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,35 +562,35 @@ suite('expect', function () {

err(function(){
expect(badFn).to.not.throw();
}, "expected [Function] to not throw an error");
}, "expected [Function] to not throw an error but [Error: testing] was thrown");

err(function(){
expect(badFn).to.throw(ReferenceError);
}, "expected [Function] to throw ReferenceError but a Error was thrown");
}, "expected [Function] to throw ReferenceError but [Error: testing] was thrown");

err(function(){
expect(badFn).to.throw(specificError);
}, "expected [Function] to throw [RangeError: boo] but [Error: testing] was thrown");

err(function(){
expect(badFn).to.not.throw(Error);
}, "expected [Function] to not throw Error");
}, "expected [Function] to not throw Error but [Error: testing] was thrown");

err(function(){
expect(refErrFn).to.not.throw(ReferenceError);
}, "expected [Function] to not throw ReferenceError");
}, "expected [Function] to not throw ReferenceError but [ReferenceError: hello] was thrown");

err(function(){
expect(badFn).to.throw(PoorlyConstructedError);
}, "expected [Function] to throw PoorlyConstructedError but a Error was thrown");
}, "expected [Function] to throw PoorlyConstructedError but [Error: testing] was thrown");

err(function(){
expect(ickyErrFn).to.not.throw(PoorlyConstructedError);
}, "expected [Function] to not throw PoorlyConstructedError");
}, "expected [Function] to not throw PoorlyConstructedError but { name: 'PoorlyConstructedError' } was thrown");

err(function(){
expect(ickyErrFn).to.throw(ReferenceError);
}, "expected [Function] to throw ReferenceError but a PoorlyConstructedError was thrown");
}, "expected [Function] to throw ReferenceError but { name: 'PoorlyConstructedError' } was thrown");

err(function(){
expect(specificErrFn).to.throw(new ReferenceError('eek'));
Expand Down
14 changes: 7 additions & 7 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,35 +530,35 @@ suite('should', function() {

err(function(){
(badFn).should.not.throw();
}, "expected [Function] to not throw an error");
}, "expected [Function] to not throw an error but [Error: testing] was thrown");

err(function(){
(badFn).should.throw(ReferenceError);
}, "expected [Function] to throw ReferenceError but a Error was thrown");
}, "expected [Function] to throw ReferenceError but [Error: testing] was thrown");

err(function(){
(badFn).should.throw(specificError);
}, "expected [Function] to throw [RangeError: boo] but [Error: testing] was thrown");

err(function(){
(badFn).should.not.throw(Error);
}, "expected [Function] to not throw Error");
}, "expected [Function] to not throw Error but [Error: testing] was thrown");

err(function(){
(refErrFn).should.not.throw(ReferenceError);
}, "expected [Function] to not throw ReferenceError");
}, "expected [Function] to not throw ReferenceError but [ReferenceError: hello] was thrown");

err(function(){
(badFn).should.throw(PoorlyConstructedError);
}, "expected [Function] to throw PoorlyConstructedError but a Error was thrown");
}, "expected [Function] to throw PoorlyConstructedError but [Error: testing] was thrown");

err(function(){
(ickyErrFn).should.not.throw(PoorlyConstructedError);
}, "expected [Function] to not throw PoorlyConstructedError");
}, "expected [Function] to not throw PoorlyConstructedError but { name: 'PoorlyConstructedError' } was thrown");

err(function(){
(ickyErrFn).should.throw(ReferenceError);
}, "expected [Function] to throw ReferenceError but a PoorlyConstructedError was thrown");
}, "expected [Function] to throw ReferenceError but { name: 'PoorlyConstructedError' } was thrown");

err(function(){
(specificErrFn).should.throw(new ReferenceError('eek'));
Expand Down

0 comments on commit 941f992

Please sign in to comment.