Skip to content

Commit 9d5ba2e

Browse files
committed
fix: support some virtual contexts in toThrow
This adds support for VM situations where we pass a `RegExp` from another process. Note that we don't have a full fix for this stuff until `check-error` also supports `Error` being from another origin.
1 parent 61159d1 commit 9d5ba2e

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

lib/chai/core/assertions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,7 +2676,7 @@ function assertThrows (errorLike, errMsgMatcher, msg) {
26762676
, negate = flag(this, 'negate') || false;
26772677
new Assertion(obj, flagMsg, ssfi, true).is.a('function');
26782678

2679-
if (errorLike instanceof RegExp || typeof errorLike === 'string') {
2679+
if (_.isRegExp(errorLike) || typeof errorLike === 'string') {
26802680
errMsgMatcher = errorLike;
26812681
errorLike = null;
26822682
}
@@ -2709,7 +2709,7 @@ function assertThrows (errorLike, errMsgMatcher, msg) {
27092709
}
27102710

27112711
this.assert(
2712-
caughtErr
2712+
caughtErr !== undefined
27132713
, 'expected #{this} to throw ' + errorLikeString
27142714
, 'expected #{this} to not throw an error but #{act} was thrown'
27152715
, errorLike && errorLike.toString()
@@ -2760,7 +2760,7 @@ function assertThrows (errorLike, errMsgMatcher, msg) {
27602760
if (caughtErr && errMsgMatcher !== undefined && errMsgMatcher !== null) {
27612761
// Here we check compatible messages
27622762
var placeholder = 'including';
2763-
if (errMsgMatcher instanceof RegExp) {
2763+
if (_.isRegExp(errMsgMatcher)) {
27642764
placeholder = 'matching'
27652765
}
27662766

lib/chai/utils/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,7 @@ export {isNaN} from './isNaN.js';
9494

9595
// getOperator method
9696
export {getOperator} from './getOperator.js';
97+
98+
export function isRegExp(obj) {
99+
return Object.prototype.toString.call(obj) === '[object RegExp]';
100+
}

test/assert.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,9 @@ describe('assert', function () {
16441644
assert[throws](function() { throw new Error('bar'); }, Error, 'bar');
16451645
assert[throws](function() { throw new Error(''); }, Error, '');
16461646
assert[throws](function() { throw new Error('foo') }, '');
1647+
assert[throws](function() { throw ''; }, '');
1648+
assert[throws](function() { throw ''; }, /^$/);
1649+
assert[throws](function() { throw new Error(''); }, /^$/);
16471650

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

web-test-runner.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ const commonjs = fromRollup(rollupCommonjs);
55

66
export default {
77
nodeResolve: true,
8-
files: ["test/*.js"],
8+
files: [
9+
"test/*.js",
10+
"!test/virtual-machines.js"
11+
],
912
plugins: [
1013
commonjs({
1114
include: [

0 commit comments

Comments
 (0)