Skip to content

Commit

Permalink
fix: support throwing of unusual errors
Browse files Browse the repository at this point in the history
Adds support for throwing things like `undefined`, functions, etc.
  • Loading branch information
43081j committed May 4, 2024
1 parent 696fabd commit 23b4dc2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2681,10 +2681,12 @@ function assertThrows (errorLike, errMsgMatcher, msg) {
errorLike = null;
}

var caughtErr;
let caughtErr;
let errorWasThrown = false;
try {
obj();
} catch (err) {
errorWasThrown = true;
caughtErr = err;
}

Expand All @@ -2708,14 +2710,26 @@ function assertThrows (errorLike, errMsgMatcher, msg) {
errorLikeString = _.checkError.getConstructorName(errorLike);
}

let actual = caughtErr;
if (caughtErr instanceof Error) {
actual = caughtErr.toString();
} else if (typeof caughtErr === 'string') {
actual = caughtErr;
} else if (caughtErr && (typeof caughtErr === 'object' || typeof caughtErr === 'function')) {
try {
actual = _.checkError.getConstructorName(caughtErr);
} catch (_err) {
// somehow wasn't a constructor, maybe we got a function thrown
// or similar
}
}

this.assert(
caughtErr !== undefined
errorWasThrown
, 'expected #{this} to throw ' + errorLikeString
, 'expected #{this} to not throw an error but #{act} was thrown'
, errorLike && errorLike.toString()
, (caughtErr instanceof Error ?
caughtErr.toString() : (typeof caughtErr === 'string' ? caughtErr : caughtErr &&
_.checkError.getConstructorName(caughtErr)))
, actual
);
}

Expand Down
6 changes: 6 additions & 0 deletions lib/chai/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ export {isNaN} from './isNaN.js';
// getOperator method
export {getOperator} from './getOperator.js';

/**
* Determines if an object is a `RegExp`
* This is used since `instanceof` will not work in virtual contexts
* @param {*} obj Object to test
* @return {boolean}
*/
export function isRegExp(obj) {
return Object.prototype.toString.call(obj) === '[object RegExp]';
}
5 changes: 5 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,8 @@ describe('assert', function () {
});

it('throws / throw / Throw', function() {
class CustomError extends Error {}

['throws', 'throw', 'Throw'].forEach(function (throws) {
assert[throws](function() { throw new Error('foo'); });
assert[throws](function() { throw new Error(''); }, '');
Expand All @@ -1647,6 +1649,9 @@ describe('assert', function () {
assert[throws](function() { throw ''; }, '');
assert[throws](function() { throw ''; }, /^$/);
assert[throws](function() { throw new Error(''); }, /^$/);
assert[throws](function() { throw undefined; });
assert[throws](function() { throw new CustomError('foo'); });
assert[throws](function() { throw (() => {}); });

var thrownErr = assert[throws](function() { throw new Error('foo'); });
assert(thrownErr instanceof Error, 'assert.' + throws + ' returns error');
Expand Down

0 comments on commit 23b4dc2

Please sign in to comment.