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); }); 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) ||