From 08ca5b2436be3efbee5c0eb3a058ef6e056a788d Mon Sep 17 00:00:00 2001 From: Waseem Dahman Date: Mon, 9 Dec 2019 21:15:38 -0500 Subject: [PATCH 1/2] fix: prevent maintaining RegExp state between calls --- packages/expect/src/__tests__/matchers.test.js | 7 +++++++ packages/expect/src/matchers.ts | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 97ad88b3333f..d4543b1766e1 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -1615,6 +1615,13 @@ describe('.toMatch()', () => { it('escapes strings properly', () => { jestExpect('this?: throws').toMatch('this?: throws'); }); + + it('does not maintain RegExp state between calls', () => { + const regex = /[f]\d+/gi; + jestExpect('f123').toMatch(regex); + jestExpect('F456').toMatch(regex); + jestExpect(regex.lastIndex).toBe(0); + }); }); describe('.toHaveLength', () => { diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index ca51e52291ba..58c015992aeb 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -835,7 +835,7 @@ const matchers: MatchersObject = { const pass = typeof expected === 'string' ? received.includes(expected) - : expected.test(received); + : new RegExp(expected).test(received); const message = pass ? () => From 88a8cbdec456abe993251d46387883be3b7b749a Mon Sep 17 00:00:00 2001 From: Waseem Dahman Date: Mon, 9 Dec 2019 21:46:06 -0500 Subject: [PATCH 2/2] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dc41106eef2..0a0295aa30b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ - `[expect]` Avoid incorrect difference for subset when `toMatchObject` fails ([#9005](https://github.com/facebook/jest/pull/9005)) - `[expect]` Consider all RegExp flags for equality ([#9167](https://github.com/facebook/jest/pull/9167)) - `[expect]` [**BREAKING**] Consider primitives different from wrappers instantiated with `new` ([#9167](https://github.com/facebook/jest/pull/9167)) +- `[expect]` Prevent maintaining RegExp state between multiple tests ([#9289](https://github.com/facebook/jest/pull/9289)) - `[jest-config]` Use half of the available cores when `watchAll` mode is enabled ([#9117](https://github.com/facebook/jest/pull/9117)) - `[jest-config]` Fix Jest multi project runner still cannot handle exactly one project ([#8894](https://github.com/facebook/jest/pull/8894)) - `[jest-console]` Add missing `console.group` calls to `NullConsole` ([#9024](https://github.com/facebook/jest/pull/9024))