From 6ff06e6392ecef8da941f6b9ccc4432b603f8c9c Mon Sep 17 00:00:00 2001 From: Sebastian Good Date: Mon, 26 Oct 2020 20:02:43 +0000 Subject: [PATCH 1/2] Add tests that will fail --- .../expect/src/__tests__/asymmetricMatchers.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/expect/src/__tests__/asymmetricMatchers.test.ts b/packages/expect/src/__tests__/asymmetricMatchers.test.ts index dac1a7c1b804..843e51b8719c 100644 --- a/packages/expect/src/__tests__/asymmetricMatchers.test.ts +++ b/packages/expect/src/__tests__/asymmetricMatchers.test.ts @@ -169,6 +169,17 @@ test('ObjectContaining matches', () => { objectContaining({first: objectContaining({second: {}})}).asymmetricMatch({ first: {second: {}}, }), + objectContaining({foo: Buffer.from('foo')}).asymmetricMatch({ + foo: Buffer.from('foo'), + jest: 'jest', + }), + objectContaining({foo: {bar: [Buffer.from('foo')]}}).asymmetricMatch({ + foo: { + bar: [Buffer.from('foo'), 1], + qux: 'qux', + }, + jest: 'jest', + }), ].forEach(test => { jestExpect(test).toEqual(true); }); From c523b65b58fc31681c28fa3a7cc27fbcbfe3c89b Mon Sep 17 00:00:00 2001 From: Sebastian Good Date: Mon, 26 Oct 2020 20:04:45 +0000 Subject: [PATCH 2/2] Resolve stepping into non vanilla objects --- packages/expect/src/asymmetricMatchers.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/expect/src/asymmetricMatchers.ts b/packages/expect/src/asymmetricMatchers.ts index 280ebc9d5fe2..86db22aac4fe 100644 --- a/packages/expect/src/asymmetricMatchers.ts +++ b/packages/expect/src/asymmetricMatchers.ts @@ -178,11 +178,21 @@ class ObjectContaining extends AsymmetricMatcher> { return true; } else { for (const property in this.sample) { - const expected = - typeof this.sample[property] === 'object' && - !(this.sample[property] instanceof AsymmetricMatcher) - ? objectContaining(this.sample[property] as Record) - : this.sample[property]; + let expected = this.sample[property]; + + if (typeof this.sample[property] === 'object') { + const samplePropertyPrototype = Object.getPrototypeOf( + this.sample[property], + ); + if ( + samplePropertyPrototype === Object.prototype || + samplePropertyPrototype === Array.prototype + ) { + expected = objectContaining( + this.sample[property] as Record, + ); + } + } if ( !hasProperty(other, property) ||