Skip to content

Commit

Permalink
Revert "fix: don't mutate the sample in expect.objectContaining() (je…
Browse files Browse the repository at this point in the history
…stjs#10711)"

This reverts commit f84a807.
  • Loading branch information
SimenB committed Nov 2, 2020
1 parent 038d8be commit 46b0838
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Expand Up @@ -9,7 +9,6 @@
- `[babel-plugin-jest-hoist]` Preserve order of hoisted mock nodes within containing block ([#10536](https://github.com/facebook/jest/pull/10536))
- `[babel-plugin-jest-hoist]` Hoist pure constants to support experimental JSX transform in hoisted mocks ([#10723](https://github.com/facebook/jest/pull/10723))
- `[babel-preset-jest]` Update `babel-preset-current-node-syntax` to support top level await ([#10747](https://github.com/facebook/jest/pull/10747))
- `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711))
- `[jest-circus, jest-jasmine2]` fix: don't assume `stack` is always a string ([#10697](https://github.com/facebook/jest/pull/10697))
- `[jest-config]` Fix bug introduced in watch mode by PR [#10678](https://github.com/facebook/jest/pull/10678/files#r511037803) ([#10692](https://github.com/facebook/jest/pull/10692))
- `[jest-config]` Throw correct error for missing preset modules ([#10737](https://github.com/facebook/jest/pull/10737))
Expand Down
8 changes: 0 additions & 8 deletions packages/expect/src/__tests__/asymmetricMatchers.test.ts
Expand Up @@ -210,14 +210,6 @@ 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: 7 additions & 4 deletions packages/expect/src/asymmetricMatchers.ts
Expand Up @@ -177,15 +177,18 @@ class ObjectContaining extends AsymmetricMatcher<Record<string, unknown>> {
return true;
} else {
for (const property in this.sample) {
const expected =
if (
typeof this.sample[property] === 'object' &&
!(this.sample[property] instanceof AsymmetricMatcher)
? objectContaining(this.sample[property] as Record<string, unknown>)
: this.sample[property];
) {
this.sample[property] = objectContaining(
this.sample[property] as Record<string, unknown>,
);
}

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

0 comments on commit 46b0838

Please sign in to comment.