Skip to content

Commit

Permalink
fix: support some virtual contexts in toThrow
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
43081j committed Feb 26, 2024
1 parent 61159d1 commit 9d5ba2e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/chai/core/assertions.js
Expand Up @@ -2676,7 +2676,7 @@ function assertThrows (errorLike, errMsgMatcher, msg) {
, negate = flag(this, 'negate') || false;
new Assertion(obj, flagMsg, ssfi, true).is.a('function');

if (errorLike instanceof RegExp || typeof errorLike === 'string') {
if (_.isRegExp(errorLike) || typeof errorLike === 'string') {
errMsgMatcher = errorLike;
errorLike = null;
}
Expand Down Expand Up @@ -2709,7 +2709,7 @@ function assertThrows (errorLike, errMsgMatcher, msg) {
}

this.assert(
caughtErr
caughtErr !== undefined
, 'expected #{this} to throw ' + errorLikeString
, 'expected #{this} to not throw an error but #{act} was thrown'
, errorLike && errorLike.toString()
Expand Down Expand Up @@ -2760,7 +2760,7 @@ function assertThrows (errorLike, errMsgMatcher, msg) {
if (caughtErr && errMsgMatcher !== undefined && errMsgMatcher !== null) {
// Here we check compatible messages
var placeholder = 'including';
if (errMsgMatcher instanceof RegExp) {
if (_.isRegExp(errMsgMatcher)) {
placeholder = 'matching'
}

Expand Down
4 changes: 4 additions & 0 deletions lib/chai/utils/index.js
Expand Up @@ -94,3 +94,7 @@ export {isNaN} from './isNaN.js';

// getOperator method
export {getOperator} from './getOperator.js';

export function isRegExp(obj) {
return Object.prototype.toString.call(obj) === '[object RegExp]';
}
3 changes: 3 additions & 0 deletions test/assert.js
Expand Up @@ -1644,6 +1644,9 @@ describe('assert', function () {
assert[throws](function() { throw new Error('bar'); }, Error, 'bar');
assert[throws](function() { throw new Error(''); }, Error, '');
assert[throws](function() { throw new Error('foo') }, '');
assert[throws](function() { throw ''; }, '');
assert[throws](function() { throw ''; }, /^$/);
assert[throws](function() { throw new Error(''); }, /^$/);

var thrownErr = assert[throws](function() { throw new Error('foo'); });
assert(thrownErr instanceof Error, 'assert.' + throws + ' returns error');
Expand Down
5 changes: 4 additions & 1 deletion web-test-runner.config.js
Expand Up @@ -5,7 +5,10 @@ const commonjs = fromRollup(rollupCommonjs);

export default {
nodeResolve: true,
files: ["test/*.js"],
files: [
"test/*.js",
"!test/virtual-machines.js"
],
plugins: [
commonjs({
include: [
Expand Down

0 comments on commit 9d5ba2e

Please sign in to comment.