Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(expect): do not rely on 'instanceof RegExp'
`instanceof RegExp` will not be true for RegExp objects created inside of a different NodeJS VM or Window.

This commit fixes this issue by checking for 'regexp.test' function instead.
  • Loading branch information
niieani committed Mar 5, 2018
commit 6de97739b3500d881bd684873efd28520790191c
17 changes: 17 additions & 0 deletions integration-tests/__tests__/expect_in_vm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

const runJest = require('../runJest');

test('expect works correctly with RegExps created inside a VM', () => {
const result = runJest('expect-in-vm');
expect(result.status).toBe(0);
});
33 changes: 33 additions & 0 deletions integration-tests/expect-in-vm/__tests__/expect-in-vm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

const vm = require('vm');

it('correctly expects RegExp inside a new VM context', () => {
const fn = vm.runInNewContext(
`(function(require, module, exports, __dirname, __filename, expect) {
expect('ab12cd').toMatch(/ab12cd/);
})`,
global
);

const module = {
exports: {},
};

fn.call(
module.exports,
require,
module,
module.exports,
__dirname,
__filename,
expect
);
});
5 changes: 5 additions & 0 deletions integration-tests/expect-in-vm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
5 changes: 4 additions & 1 deletion packages/expect/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,10 @@ const matchers: MatchersObject = {
);
}

if (!(expected instanceof RegExp) && !(typeof expected === 'string')) {
if (
!(expected && typeof expected.test === 'function') &&
!(typeof expected === 'string')
) {
throw new Error(
matcherHint('[.not].toMatch', 'string', 'expected') +
'\n\n' +
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/to_throw_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const createMatcher = (matcherName: string, fromPromise?: boolean) => (

if (typeof expected === 'function') {
return toThrowMatchingError(matcherName, error, expected);
} else if (expected instanceof RegExp) {
} else if (expected && typeof expected.test === 'function') {
return toThrowMatchingStringOrRegexp(matcherName, error, expected, value);
} else if (expected && typeof expected === 'object') {
return toThrowMatchingErrorInstance(matcherName, error, expected);
Expand Down