Skip to content

Commit 8236779

Browse files
wsmdpedrottimark
authored andcommitted
Fix: Prevent maintaining RegExp state between multiple tests (#9289)
* fix: prevent maintaining RegExp state between calls * add changelog entry
1 parent f19adb1 commit 8236779

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
- `[expect]` Avoid incorrect difference for subset when `toMatchObject` fails ([#9005](https://github.com/facebook/jest/pull/9005))
5151
- `[expect]` Consider all RegExp flags for equality ([#9167](https://github.com/facebook/jest/pull/9167))
5252
- `[expect]` [**BREAKING**] Consider primitives different from wrappers instantiated with `new` ([#9167](https://github.com/facebook/jest/pull/9167))
53+
- `[expect]` Prevent maintaining RegExp state between multiple tests ([#9289](https://github.com/facebook/jest/pull/9289))
5354
- `[expect]` Fix subsetEquality false circular reference detection ([#9322](https://github.com/facebook/jest/pull/9322))
5455
- `[jest-config]` Use half of the available cores when `watchAll` mode is enabled ([#9117](https://github.com/facebook/jest/pull/9117))
5556
- `[jest-config]` Fix Jest multi project runner still cannot handle exactly one project ([#8894](https://github.com/facebook/jest/pull/8894))

packages/expect/src/__tests__/matchers.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,13 @@ describe('.toMatch()', () => {
16171617
it('escapes strings properly', () => {
16181618
jestExpect('this?: throws').toMatch('this?: throws');
16191619
});
1620+
1621+
it('does not maintain RegExp state between calls', () => {
1622+
const regex = /[f]\d+/gi;
1623+
jestExpect('f123').toMatch(regex);
1624+
jestExpect('F456').toMatch(regex);
1625+
jestExpect(regex.lastIndex).toBe(0);
1626+
});
16201627
});
16211628

16221629
describe('.toHaveLength', () => {

packages/expect/src/matchers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ const matchers: MatchersObject = {
835835
const pass =
836836
typeof expected === 'string'
837837
? received.includes(expected)
838-
: expected.test(received);
838+
: new RegExp(expected).test(received);
839839

840840
const message = pass
841841
? () =>

0 commit comments

Comments
 (0)