Skip to content

Commit

Permalink
Merge pull request #202 from andreineculau/patch-2
Browse files Browse the repository at this point in the history
test: add tests for throwing custom errors
  • Loading branch information
logicalparadox committed Oct 23, 2013
2 parents 1a4d35d + b1c7d71 commit f82f43f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
10 changes: 10 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,22 @@ describe('assert', function () {
});

it('doesNotThrow', function() {
function CustomError(message) {
this.name = 'CustomError';
this.message = message;
}
CustomError.prototype = Error.prototype;

assert.doesNotThrow(function() { });
assert.doesNotThrow(function() { }, 'foo');

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

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

it('ifError', function() {
Expand Down
13 changes: 12 additions & 1 deletion test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,20 @@ describe('expect', function () {
};
PoorlyConstructedError.prototype = Object.create(Error.prototype);

function CustomError(message) {
this.name = 'CustomError';
this.message = message;
}
CustomError.prototype = Error.prototype;

var specificError = new RangeError('boo');

var goodFn = function () { 1==1; }
, badFn = function () { throw new Error('testing'); }
, refErrFn = function () { throw new ReferenceError('hello'); }
, ickyErrFn = function () { throw new PoorlyConstructedError(); }
, specificErrFn = function () { throw specificError; };
, specificErrFn = function () { throw specificError; }
, customErrFn = function() { throw new CustomError('foo'); };

expect(goodFn).to.not.throw();
expect(goodFn).to.not.throw(Error);
Expand Down Expand Up @@ -687,6 +694,10 @@ describe('expect', function () {
err(function () {
expect(badFn).to.throw(Error, 'hello', 'blah');
}, "blah: expected [Function] to throw error including 'hello' but got 'testing'");

err(function () {
(customErrFn).should.not.throw();
}, "expected [Function] to not throw an error but 'CustomError: foo' was thrown");
});

it('respondTo', function(){
Expand Down
13 changes: 12 additions & 1 deletion test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,14 +503,21 @@ describe('should', function() {
};
PoorlyConstructedError.prototype = Object.create(Error.prototype);

function CustomError(message) {
this.name = 'CustomError';
this.message = message;
}
CustomError.prototype = Error.prototype;

var specificError = new RangeError('boo');

var goodFn = function () { 1==1; }
, badFn = function () { throw new Error('testing'); }
, stringErrFn = function () { throw 'testing'; }
, refErrFn = function () { throw new ReferenceError('hello'); }
, ickyErrFn = function () { throw new PoorlyConstructedError(); }
, specificErrFn = function () { throw specificError; };
, specificErrFn = function () { throw specificError; }
, customErrFn = function() { throw new CustomError('foo'); };

(goodFn).should.not.throw();
(goodFn).should.not.throw(Error);
Expand Down Expand Up @@ -640,6 +647,10 @@ describe('should', function() {
err(function () {
(badFn).should.throw(Error, 'hello', 'blah');
}, "blah: expected [Function] to throw error including 'hello' but got 'testing'");

err(function () {
(customErrFn).should.not.throw();
}, "expected [Function] to not throw an error but 'CustomError: foo' was thrown");
});

it('respondTo', function(){
Expand Down

0 comments on commit f82f43f

Please sign in to comment.