From 0502a24da59e61d9788d84b4855f64eaf401dae2 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Thu, 3 Oct 2019 17:45:37 -0400 Subject: [PATCH 1/4] expect: Avoid incorrect difference for subset when toMatchObject fails --- packages/expect/src/__tests__/utils.test.js | 39 ++++++++++++++++++++- packages/expect/src/utils.ts | 4 +++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/expect/src/__tests__/utils.test.js b/packages/expect/src/__tests__/utils.test.js index b20b3db68700..ef244aadd558 100644 --- a/packages/expect/src/__tests__/utils.test.js +++ b/packages/expect/src/__tests__/utils.test.js @@ -147,7 +147,7 @@ describe('hasOwnProperty', () => { }); }); -describe('getObjectSubset()', () => { +describe('getObjectSubset', () => { [ [{a: 'b', c: 'd'}, {a: 'd'}, {a: 'b'}], [{a: [1, 2], b: 'b'}, {a: [3, 4]}, {a: [1, 2]}], @@ -165,6 +165,43 @@ describe('getObjectSubset()', () => { ); }); + describe('returns object instance', () => { + test('Date', () => { + const object = new Date('2015-11-30'); + const subset = new Date('2016-12-30'); + + expect(getObjectSubset(object, subset)).toBe(object); + }); + }); + + describe('returns subset instance', () => { + test('Object', () => { + const object = {key0: 'zero', key1: 'one', key2: 'two'}; + const subset = {key0: 'zero', key2: 'two'}; + + expect(getObjectSubset(object, subset)).toBe(subset); + }); + + describe('Uint8Array', () => { + const equalObject = {'0': 0, '1': 0, '2': 0}; + const typedArray = new Uint8Array(3); + + test('expected', () => { + const object = equalObject; + const subset = typedArray; + + expect(getObjectSubset(object, subset)).toBe(subset); + }); + + test('received', () => { + const object = typedArray; + const subset = equalObject; + + expect(getObjectSubset(object, subset)).toBe(subset); + }); + }); + }); + describe('calculating subsets of objects with circular references', () => { test('simple circular references', () => { const nonCircularObj = {a: 'world', b: 'something'}; diff --git a/packages/expect/src/utils.ts b/packages/expect/src/utils.ts index 6e77197bf219..da2fe090f0ee 100644 --- a/packages/expect/src/utils.ts +++ b/packages/expect/src/utils.ts @@ -118,6 +118,10 @@ export const getObjectSubset = ( } else if (object instanceof Date) { return object; } else if (isObject(object) && isObject(subset)) { + if (equals(object, subset, [iterableEquality, subsetEquality])) { + return subset; + } + const trimmed: any = {}; seenReferences.set(object, trimmed); From 93ee43588c7217a7c34c7215075b0a44a7293c86 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Thu, 3 Oct 2019 17:52:58 -0400 Subject: [PATCH 2/4] Add brief comments about returning subclass of subset --- packages/expect/src/utils.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/expect/src/utils.ts b/packages/expect/src/utils.ts index da2fe090f0ee..a642e56b22ee 100644 --- a/packages/expect/src/utils.ts +++ b/packages/expect/src/utils.ts @@ -111,6 +111,7 @@ export const getObjectSubset = ( ): any => { if (Array.isArray(object)) { if (Array.isArray(subset) && subset.length === object.length) { + // The map method returns correct subclass of subset. return subset.map((sub: any, i: number) => getObjectSubset(object[i], sub), ); @@ -119,6 +120,7 @@ export const getObjectSubset = ( return object; } else if (isObject(object) && isObject(subset)) { if (equals(object, subset, [iterableEquality, subsetEquality])) { + // Avoid unnecessary copy which might return Object instead of subclass. return subset; } From 7414ae47c25195e05083564f4ce79f111bd39edc Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Thu, 3 Oct 2019 17:54:58 -0400 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1bf053cde4a..22c008b0a1fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,8 @@ ### Fixes -- `[expect]` Display expectedDiff more carefully in toBeCloseTo ([#8389](https://github.com/facebook/jest/pull/8389)) +- `[expect]` Display `expectedDiff` more carefully in `toBeCloseTo` ([#8389](https://github.com/facebook/jest/pull/8389)) +- `[expect]` Avoid incorrect difference for subset when `toMatchObject` fails ([#9005](https://github.com/facebook/jest/pull/9005)) - `[jest-core]` Don't include unref'd timers in --detectOpenHandles results ([#8941](https://github.com/facebook/jest/pull/8941)) - `[jest-diff]` Do not inverse format if line consists of one change ([#8903](https://github.com/facebook/jest/pull/8903)) - `[jest-fake-timers]` `getTimerCount` will not include cancelled immediates ([#8764](https://github.com/facebook/jest/pull/8764)) From c6cfa1cfe12b83fa5c41388ea277ba5dd9a70a10 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Wed, 16 Oct 2019 17:00:02 -0400 Subject: [PATCH 4/4] Apply suggestions from code review Co-Authored-By: Tim Seckinger --- packages/expect/src/__tests__/utils.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/expect/src/__tests__/utils.test.js b/packages/expect/src/__tests__/utils.test.js index ef244aadd558..e78c4e597313 100644 --- a/packages/expect/src/__tests__/utils.test.js +++ b/packages/expect/src/__tests__/utils.test.js @@ -165,7 +165,7 @@ describe('getObjectSubset', () => { ); }); - describe('returns object instance', () => { + describe('returns the object instance if the subset has no extra properties', () => { test('Date', () => { const object = new Date('2015-11-30'); const subset = new Date('2016-12-30'); @@ -174,7 +174,7 @@ describe('getObjectSubset', () => { }); }); - describe('returns subset instance', () => { + describe('returns the subset instance if its property values are equal', () => { test('Object', () => { const object = {key0: 'zero', key1: 'one', key2: 'two'}; const subset = {key0: 'zero', key2: 'two'};