diff --git a/CHANGELOG.md b/CHANGELOG.md index 68e11847ecfb..b2a023fa86dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,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)) - `[expect]` Fix subsetEquality false circular reference detection ([#9322](https://github.com/facebook/jest/pull/9322)) - `[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)) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index fb6223a9466f..f177a98ed932 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -1617,6 +1617,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 ? () =>