Skip to content

Commit

Permalink
fix: don't mutate the sample in expect.objectContaining() (#10711)
Browse files Browse the repository at this point in the history
  • Loading branch information
ninevra committed Oct 26, 2020
1 parent 6dda424 commit f84a807
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@

### Fixes

- `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711))

### Chore & Maintenance

- `[jest-cli]` chore: standardize files and folder names ([#10698](https://github.com/facebook/jest/pull/1098))
Expand Down
8 changes: 8 additions & 0 deletions packages/expect/src/__tests__/asymmetricMatchers.test.ts
Expand Up @@ -211,6 +211,14 @@ test('ObjectContaining throws for non-objects', () => {
jestExpect(() => objectContaining(1337).asymmetricMatch()).toThrow();
});

test('ObjectContaining does not mutate the sample', () => {
const sample = {foo: {bar: {}}};
const sample_json = JSON.stringify(sample);
expect({foo: {bar: {}}}).toEqual(expect.objectContaining(sample));

expect(JSON.stringify(sample)).toEqual(sample_json);
});

test('ObjectNotContaining matches', () => {
[
objectNotContaining({}).asymmetricMatch('jest'),
Expand Down
11 changes: 4 additions & 7 deletions packages/expect/src/asymmetricMatchers.ts
Expand Up @@ -178,18 +178,15 @@ class ObjectContaining extends AsymmetricMatcher<Record<string, unknown>> {
return true;
} else {
for (const property in this.sample) {
if (
const expected =
typeof this.sample[property] === 'object' &&
!(this.sample[property] instanceof AsymmetricMatcher)
) {
this.sample[property] = objectContaining(
this.sample[property] as Record<string, unknown>,
);
}
? objectContaining(this.sample[property] as Record<string, unknown>)
: this.sample[property];

if (
!hasProperty(other, property) ||
!equals(this.sample[property], other[property])
!equals(expected, other[property])
) {
return false;
}
Expand Down

0 comments on commit f84a807

Please sign in to comment.