From 8ae206816aa768e01e0264ceded5d367b6e4e69b Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Sun, 16 Aug 2020 18:20:52 +0800 Subject: [PATCH 01/15] refactor deepCyclicCopyObject should also overwrite symbol key descriptors --- .../deepCyclicCopyReplaceable.test.ts | 19 +++++++ .../src/deepCyclicCopyReplaceable.ts | 52 ++++++++++++------- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceable.test.ts b/packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceable.test.ts index 63af3a7dce3d..254835b01291 100644 --- a/packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceable.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceable.test.ts @@ -113,3 +113,22 @@ test('return same value for built-in object type except array, map and object', expect(deepCyclicCopyReplaceable(regexp)).toBe(regexp); expect(deepCyclicCopyReplaceable(set)).toBe(set); }); + +test('should copy object symbol key property', () => { + const symbolKey = Symbol.for('key'); + expect(deepCyclicCopyReplaceable({[symbolKey]: 1})).toEqual({[symbolKey]: 1}); +}); + +test('should set writable, configurable to true', () => { + const a = {}; + Object.defineProperty(a, 'key', { + configurable: false, + enumerable: true, + value: 1, + writable: false, + }); + const copied = deepCyclicCopyReplaceable(a); + expect(Object.getOwnPropertyDescriptors(copied)).toEqual({ + key: {configurable: true, enumerable: true, value: 1, writable: true}, + }); +}); diff --git a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts index 6849735a7f2c..531102c2cd8f 100644 --- a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts +++ b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts @@ -55,25 +55,41 @@ function deepCyclicCopyObject(object: T, cycles: WeakMap): T { cycles.set(object, newObject); - Object.keys(descriptors).forEach(key => { - if (descriptors[key].enumerable) { - descriptors[key] = { - configurable: true, - enumerable: true, - value: deepCyclicCopyReplaceable( - // this accesses the value or getter, depending. We just care about the value anyways, and this allows us to not mess with accessors - // it has the side effect of invoking the getter here though, rather than copying it over - (object as Record)[key], - cycles, - ), - writable: true, - }; - } else { - delete descriptors[key]; - } + const filterEnumerable = (descriptors: {[x: string]: PropertyDescriptor}) => ( + key: string, + ) => descriptors[key].enumerable; + const setDescriptor = (object: {[x: string]: any}) => ( + newDescriptors: {[x: string]: PropertyDescriptor}, + key: string, + ) => { + newDescriptors[key] = { + configurable: true, + enumerable: true, + value: deepCyclicCopyReplaceable( + // this accesses the value or getter, depending. We just care about the value anyways, and this allows us to not mess with accessors + // it has the side effect of invoking the getter here though, rather than copying it over + (object as Record)[key], + cycles, + ), + writable: true, + }; + return newDescriptors; + }; + + const newStringKeyDescriptors = Object.keys(descriptors) + .filter(filterEnumerable(descriptors)) + .reduce(setDescriptor(object), {}); + const newSymbolKeyDescriptors = Object.getOwnPropertySymbols(descriptors) + //@ts-expect-error because typescript do not support symbol key in object + .filter(filterEnumerable(descriptors)) + //@ts-expect-error because typescript do not support symbol key in object + .reduce(setDescriptor(object), {}); + + return Object.defineProperties(newObject, { + ...newStringKeyDescriptors, + //@ts-expect-error because typescript do not support symbol key in object + ...newSymbolKeyDescriptors, }); - - return Object.defineProperties(newObject, descriptors); } function deepCyclicCopyArray(array: Array, cycles: WeakMap): T { From 58af88edcf5e40248028b73f02c71fdcb304d186 Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Sun, 16 Aug 2020 18:25:22 +0800 Subject: [PATCH 02/15] add object contain readonly symbol key object test --- .../printDiffOrStringify.test.ts.snap | 13 +++++++++++ .../__tests__/printDiffOrStringify.test.ts | 23 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap b/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap index 7e0674650dc6..03023f17dc7d 100644 --- a/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap +++ b/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap @@ -142,6 +142,19 @@ exports[`printDiffOrStringify asymmetricMatcher object in array 1`] = ` ] `; +exports[`printDiffOrStringify asymmetricMatcher object contain readonly symbol key object 1`] = ` +- Expected - 1 ++ Received + 1 + + Object { +- "b": 2, ++ "b": 1, + Symbol(key): Object { + "a": 1, + }, + } +`; + exports[`printDiffOrStringify asymmetricMatcher transitive circular 1`] = ` - Expected - 1 + Received + 1 diff --git a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts index 41b61c4b9fdc..2d1ee15f67e4 100644 --- a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts @@ -245,5 +245,28 @@ describe('printDiffOrStringify', () => { received.set('circular', received); expect(testDiffOrStringify(expected, received)).toMatchSnapshot(); }); + + test('object contain readonly symbol key object', () => { + const expected = {b: 2}; + const received = {b: 1}; + const symbolKey = Symbol.for('key'); + Object.defineProperty(expected, symbolKey, { + configurable: true, + enumerable: true, + value: { + a: 1, + }, + writable: false, + }); + Object.defineProperty(received, symbolKey, { + configurable: true, + enumerable: true, + value: { + a: 1, + }, + writable: false, + }); + expect(testDiffOrStringify(expected, received)).toMatchSnapshot(); + }); }); }); From 95eb84c99f6ccd836a835c452bc19af8c5934a75 Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Sun, 16 Aug 2020 18:53:19 +0800 Subject: [PATCH 03/15] update wrong test position --- .../printDiffOrStringify.test.ts.snap | 26 +++++------ .../__tests__/printDiffOrStringify.test.ts | 46 +++++++++---------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap b/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap index 03023f17dc7d..a35b44743030 100644 --- a/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap +++ b/packages/jest-matcher-utils/src/__tests__/__snapshots__/printDiffOrStringify.test.ts.snap @@ -142,19 +142,6 @@ exports[`printDiffOrStringify asymmetricMatcher object in array 1`] = ` ] `; -exports[`printDiffOrStringify asymmetricMatcher object contain readonly symbol key object 1`] = ` -- Expected - 1 -+ Received + 1 - - Object { -- "b": 2, -+ "b": 1, - Symbol(key): Object { - "a": 1, - }, - } -`; - exports[`printDiffOrStringify asymmetricMatcher transitive circular 1`] = ` - Expected - 1 + Received + 1 @@ -211,3 +198,16 @@ exports[`printDiffOrStringify has no common after clean up chaff one-line 1`] = Expected: "delete" Received: "insert" `; + +exports[`printDiffOrStringify object contain readonly symbol key object 1`] = ` +- Expected - 1 ++ Received + 1 + + Object { +- "b": 2, ++ "b": 1, + Symbol(key): Object { + "a": 1, + }, + } +`; diff --git a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts index 2d1ee15f67e4..874d38ec6068 100644 --- a/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/printDiffOrStringify.test.ts @@ -50,6 +50,29 @@ describe('printDiffOrStringify', () => { expect(testDiffOrStringify(expected, received)).toMatchSnapshot(); }); + test('object contain readonly symbol key object', () => { + const expected = {b: 2}; + const received = {b: 1}; + const symbolKey = Symbol.for('key'); + Object.defineProperty(expected, symbolKey, { + configurable: true, + enumerable: true, + value: { + a: 1, + }, + writable: false, + }); + Object.defineProperty(received, symbolKey, { + configurable: true, + enumerable: true, + value: { + a: 1, + }, + writable: false, + }); + expect(testDiffOrStringify(expected, received)).toMatchSnapshot(); + }); + describe('MAX_DIFF_STRING_LENGTH', () => { const lessChange = INVERTED_COLOR('single '); const less = 'single line'; @@ -245,28 +268,5 @@ describe('printDiffOrStringify', () => { received.set('circular', received); expect(testDiffOrStringify(expected, received)).toMatchSnapshot(); }); - - test('object contain readonly symbol key object', () => { - const expected = {b: 2}; - const received = {b: 1}; - const symbolKey = Symbol.for('key'); - Object.defineProperty(expected, symbolKey, { - configurable: true, - enumerable: true, - value: { - a: 1, - }, - writable: false, - }); - Object.defineProperty(received, symbolKey, { - configurable: true, - enumerable: true, - value: { - a: 1, - }, - writable: false, - }); - expect(testDiffOrStringify(expected, received)).toMatchSnapshot(); - }); }); }); From b9fcdf69d3b0023a46018651dfd8bf0c210d11b8 Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Sun, 16 Aug 2020 18:55:12 +0800 Subject: [PATCH 04/15] update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd0ebbcf853a..750bcf940973 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - `[jest-reporters]` Fixes notify reporter on Linux (using notify-send) ([#10393](https://github.com/facebook/jest/pull/10400)) - `[jest-snapshot]` Correctly handles arrays and property matchers in snapshots ([#10404](https://github.com/facebook/jest/pull/10404)) +- `[jest-matcher-utils]` Fix diffing object contain readonly symbol key object ([#10414](https://github.com/facebook/jest/pull/10414)) ### Chore & Maintenance From 04e47addd9bcdaca03d7ff6b688ca977e018a609 Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Mon, 17 Aug 2020 09:20:31 +0800 Subject: [PATCH 05/15] add a link to the typescript issue --- packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts index 531102c2cd8f..859edaa3d4a7 100644 --- a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts +++ b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts @@ -81,13 +81,16 @@ function deepCyclicCopyObject(object: T, cycles: WeakMap): T { .reduce(setDescriptor(object), {}); const newSymbolKeyDescriptors = Object.getOwnPropertySymbols(descriptors) //@ts-expect-error because typescript do not support symbol key in object + //https://github.com/microsoft/TypeScript/issues/1863 .filter(filterEnumerable(descriptors)) //@ts-expect-error because typescript do not support symbol key in object + //https://github.com/microsoft/TypeScript/issues/1863 .reduce(setDescriptor(object), {}); return Object.defineProperties(newObject, { ...newStringKeyDescriptors, //@ts-expect-error because typescript do not support symbol key in object + //https://github.com/microsoft/TypeScript/issues/1863 ...newSymbolKeyDescriptors, }); } From 930e1983175465c0ec4a78d73cda2c8dceef974d Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Mon, 17 Aug 2020 09:20:55 +0800 Subject: [PATCH 06/15] sort changelog alphabetical --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 750bcf940973..d9fc181407cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,9 @@ ### Fixes +- `[jest-matcher-utils]` Fix diffing object contain readonly symbol key object ([#10414](https://github.com/facebook/jest/pull/10414)) - `[jest-reporters]` Fixes notify reporter on Linux (using notify-send) ([#10393](https://github.com/facebook/jest/pull/10400)) - `[jest-snapshot]` Correctly handles arrays and property matchers in snapshots ([#10404](https://github.com/facebook/jest/pull/10404)) -- `[jest-matcher-utils]` Fix diffing object contain readonly symbol key object ([#10414](https://github.com/facebook/jest/pull/10414)) ### Chore & Maintenance From b9436a18e162cb34f9d63481aca4becfa92da2f3 Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Tue, 18 Aug 2020 01:30:12 +0800 Subject: [PATCH 07/15] shuold not skips non-enumerables property --- .../deepCyclicCopyReplaceable.test.ts | 11 +++- .../src/deepCyclicCopyReplaceable.ts | 61 ++++++++----------- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceable.test.ts b/packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceable.test.ts index 254835b01291..3e647b4de61d 100644 --- a/packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceable.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceable.test.ts @@ -43,13 +43,20 @@ test('convert accessor descriptor into value descriptor', () => { }); }); -test('skips non-enumerables', () => { +test('shuold not skips non-enumerables', () => { const obj = {}; Object.defineProperty(obj, 'foo', {enumerable: false, value: 'bar'}); const copy = deepCyclicCopyReplaceable(obj); - expect(Object.getOwnPropertyDescriptors(copy)).toEqual({}); + expect(Object.getOwnPropertyDescriptors(copy)).toEqual({ + foo: { + configurable: true, + enumerable: false, + value: 'bar', + writable: true, + }, + }); }); test('copies symbols', () => { diff --git a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts index 859edaa3d4a7..58d3d031be45 100644 --- a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts +++ b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts @@ -55,44 +55,33 @@ function deepCyclicCopyObject(object: T, cycles: WeakMap): T { cycles.set(object, newObject); - const filterEnumerable = (descriptors: {[x: string]: PropertyDescriptor}) => ( - key: string, - ) => descriptors[key].enumerable; - const setDescriptor = (object: {[x: string]: any}) => ( - newDescriptors: {[x: string]: PropertyDescriptor}, - key: string, - ) => { - newDescriptors[key] = { - configurable: true, - enumerable: true, - value: deepCyclicCopyReplaceable( - // this accesses the value or getter, depending. We just care about the value anyways, and this allows us to not mess with accessors - // it has the side effect of invoking the getter here though, rather than copying it over - (object as Record)[key], - cycles, - ), - writable: true, - }; - return newDescriptors; - }; - - const newStringKeyDescriptors = Object.keys(descriptors) - .filter(filterEnumerable(descriptors)) - .reduce(setDescriptor(object), {}); - const newSymbolKeyDescriptors = Object.getOwnPropertySymbols(descriptors) + const newDescriptors = [ + ...Object.keys(descriptors), + ...Object.getOwnPropertySymbols(descriptors), + ].reduce( //@ts-expect-error because typescript do not support symbol key in object //https://github.com/microsoft/TypeScript/issues/1863 - .filter(filterEnumerable(descriptors)) - //@ts-expect-error because typescript do not support symbol key in object - //https://github.com/microsoft/TypeScript/issues/1863 - .reduce(setDescriptor(object), {}); - - return Object.defineProperties(newObject, { - ...newStringKeyDescriptors, - //@ts-expect-error because typescript do not support symbol key in object - //https://github.com/microsoft/TypeScript/issues/1863 - ...newSymbolKeyDescriptors, - }); + (newDescriptors: {[x: string]: PropertyDescriptor}, key: string) => { + const enumerable = descriptors[key].enumerable; + + newDescriptors[key] = { + configurable: true, + enumerable, + value: deepCyclicCopyReplaceable( + // this accesses the value or getter, depending. We just care about the value anyways, and this allows us to not mess with accessors + // it has the side effect of invoking the getter here though, rather than copying it over + (object as Record)[key], + cycles, + ), + writable: true, + }; + return newDescriptors; + }, + {}, + ); + //@ts-expect-error because typescript do not support symbol key in object + //https://github.com/microsoft/TypeScript/issues/1863 + return Object.defineProperties(newObject, newDescriptors); } function deepCyclicCopyArray(array: Array, cycles: WeakMap): T { From f8e48851dba2464fc9bb6f004371e1d888a90898 Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Tue, 18 Aug 2020 01:30:42 +0800 Subject: [PATCH 08/15] use node.cloneNode(true) to copy DOM node --- packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts index 58d3d031be45..2761945603ef 100644 --- a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts +++ b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +import {plugins} from 'pretty-format'; + const builtInObject = [ Array, Buffer, @@ -42,6 +44,9 @@ export default function deepCyclicCopyReplaceable( return deepCyclicCopyMap(value, cycles); } else if (isBuiltInObject(value)) { return value; + } else if (plugins.DOMElement.test(value)) { + //@ts-expect-error skip casting to Node + return value.cloneNode(true); } else { return deepCyclicCopyObject(value, cycles); } From 8656b705832bf64e2af9663589378cac764dbc90 Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Tue, 18 Aug 2020 01:31:25 +0800 Subject: [PATCH 09/15] skip nonenumerable property when calling forEach --- .../jest-matcher-utils/src/Replaceable.ts | 17 ++++++++----- .../src/__tests__/Replaceable.test.ts | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/packages/jest-matcher-utils/src/Replaceable.ts b/packages/jest-matcher-utils/src/Replaceable.ts index 8f518fef1494..07f26d6cd89a 100644 --- a/packages/jest-matcher-utils/src/Replaceable.ts +++ b/packages/jest-matcher-utils/src/Replaceable.ts @@ -32,12 +32,17 @@ export default class Replaceable { forEach(cb: ReplaceableForEachCallBack): void { if (this.type === 'object') { - Object.entries(this.object).forEach(([key, value]) => { - cb(value, key, this.object); - }); - Object.getOwnPropertySymbols(this.object).forEach(key => { - cb(this.object[key], key, this.object); - }); + const descriptors = Object.getOwnPropertyDescriptors(this.object); + [ + ...Object.keys(descriptors), + ...Object.getOwnPropertySymbols(descriptors), + ] + //@ts-expect-error because typescript do not support symbol key in object + //https://github.com/microsoft/TypeScript/issues/1863 + .filter(key => descriptors[key].enumerable) + .forEach(key => { + cb(this.object[key], key, this.object); + }); } else { this.object.forEach(cb); } diff --git a/packages/jest-matcher-utils/src/__tests__/Replaceable.test.ts b/packages/jest-matcher-utils/src/__tests__/Replaceable.test.ts index 890f1ba38b96..ef82ba3772ea 100644 --- a/packages/jest-matcher-utils/src/__tests__/Replaceable.test.ts +++ b/packages/jest-matcher-utils/src/__tests__/Replaceable.test.ts @@ -132,6 +132,30 @@ describe('Replaceable', () => { expect(cb.mock.calls[0]).toEqual([1, 'a', map]); expect(cb.mock.calls[1]).toEqual([2, 'b', map]); }); + + test('forEach should ignore nonenumerable property', () => { + const symbolKey = Symbol('jest'); + const symbolKey2 = Symbol('awesome'); + const object = {a: 1, [symbolKey]: 3}; + Object.defineProperty(object, 'b', { + configurable: true, + enumerable: false, + value: 2, + writable: true, + }); + Object.defineProperty(object, symbolKey2, { + configurable: true, + enumerable: false, + value: 4, + writable: true, + }); + const replaceable = new Replaceable(object); + const cb = jest.fn(); + replaceable.forEach(cb); + expect(cb).toHaveBeenCalledTimes(2); + expect(cb.mock.calls[0]).toEqual([1, 'a', object]); + expect(cb.mock.calls[1]).toEqual([3, symbolKey, object]); + }); }); describe('isReplaceable', () => { From c524e0244736dd4f814ae97828aa92549e813c6d Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Thu, 20 Aug 2020 00:33:24 +0800 Subject: [PATCH 10/15] remove comment --- packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts index 2761945603ef..2a22bcfddaf4 100644 --- a/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts +++ b/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts @@ -45,8 +45,7 @@ export default function deepCyclicCopyReplaceable( } else if (isBuiltInObject(value)) { return value; } else if (plugins.DOMElement.test(value)) { - //@ts-expect-error skip casting to Node - return value.cloneNode(true); + return (((value as unknown) as Element).cloneNode(true) as unknown) as T; } else { return deepCyclicCopyObject(value, cycles); } From df800751466e27f5c60879ac359e1d372109c11d Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Thu, 20 Aug 2020 00:33:41 +0800 Subject: [PATCH 11/15] add domDiffing e2e test --- .../__snapshots__/domDiffing.test.ts.snap | 51 +++++++++++++++++++ e2e/__tests__/domDiffing.test.ts | 9 ++++ e2e/dom-diffing/__tests__/dom.test.js | 9 ++++ e2e/dom-diffing/package.json | 3 ++ 4 files changed, 72 insertions(+) create mode 100644 e2e/__tests__/__snapshots__/domDiffing.test.ts.snap create mode 100644 e2e/__tests__/domDiffing.test.ts create mode 100644 e2e/dom-diffing/__tests__/dom.test.js create mode 100644 e2e/dom-diffing/package.json diff --git a/e2e/__tests__/__snapshots__/domDiffing.test.ts.snap b/e2e/__tests__/__snapshots__/domDiffing.test.ts.snap new file mode 100644 index 000000000000..4ecb091ab441 --- /dev/null +++ b/e2e/__tests__/__snapshots__/domDiffing.test.ts.snap @@ -0,0 +1,51 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should work without error 1`] = ` +"FAIL __tests__/dom.test.js + ✕ use toBe compare two div (5 ms) + ✕ compare span and div (2 ms) + + ● use toBe compare two div + + expect(received).toBe(expected) // Object.is equality + + If it should pass with deep equality, replace \\"toBe\\" with \\"toStrictEqual\\" + + Expected:
+ Received: serializes to the same string + + 2 | const div1 = document.createElement('div'); + 3 | const div2 = document.createElement('div'); + > 4 | expect(div1).toBe(div2); + | ^ + 5 | }); + 6 | + 7 | test('compare span and div', () => { + + at Object.toBe (__tests__/dom.test.js:4:16) + + ● compare span and div + + expect(received).toBe(expected) // Object.is equality + + - Expected - 1 + + Received + 1 + + - + +
+ + 6 | + 7 | test('compare span and div', () => { + > 8 | expect(document.createElement('div')).toBe(document.createElement('span')); + | ^ + 9 | }); + 10 | + + at Object.toBe (__tests__/dom.test.js:8:41) + +Test Suites: 1 failed, 1 total +Tests: 2 failed, 2 total +Snapshots: 0 total +Time: 0.569 s, estimated 1 s +Ran all test suites." +`; diff --git a/e2e/__tests__/domDiffing.test.ts b/e2e/__tests__/domDiffing.test.ts new file mode 100644 index 000000000000..4ade0d9de361 --- /dev/null +++ b/e2e/__tests__/domDiffing.test.ts @@ -0,0 +1,9 @@ +import runJest from '../runJest'; +import {extractSortedSummary} from '../Utils'; + +test('should work without error', () => { + const output = runJest('dom-diffing'); + console.log(extractSortedSummary(output.stderr)); + expect(output.failed).toBe(true); + expect(output.stderr).toMatchSnapshot(); +}); diff --git a/e2e/dom-diffing/__tests__/dom.test.js b/e2e/dom-diffing/__tests__/dom.test.js new file mode 100644 index 000000000000..6fd85a785029 --- /dev/null +++ b/e2e/dom-diffing/__tests__/dom.test.js @@ -0,0 +1,9 @@ +test('use toBe compare two div', () => { + const div1 = document.createElement('div'); + const div2 = document.createElement('div'); + expect(div1).toBe(div2); +}); + +test('compare span and div', () => { + expect(document.createElement('div')).toBe(document.createElement('span')); +}); diff --git a/e2e/dom-diffing/package.json b/e2e/dom-diffing/package.json new file mode 100644 index 000000000000..586d4ca6b75c --- /dev/null +++ b/e2e/dom-diffing/package.json @@ -0,0 +1,3 @@ +{ + "jest": {} +} From 48c5dcae776b0f25e3471810de84ebbe9a1decc2 Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Thu, 20 Aug 2020 00:36:53 +0800 Subject: [PATCH 12/15] replace output time --- e2e/__tests__/__snapshots__/domDiffing.test.ts.snap | 6 +++--- e2e/__tests__/domDiffing.test.ts | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/e2e/__tests__/__snapshots__/domDiffing.test.ts.snap b/e2e/__tests__/__snapshots__/domDiffing.test.ts.snap index 4ecb091ab441..69e81da89ecd 100644 --- a/e2e/__tests__/__snapshots__/domDiffing.test.ts.snap +++ b/e2e/__tests__/__snapshots__/domDiffing.test.ts.snap @@ -2,8 +2,8 @@ exports[`should work without error 1`] = ` "FAIL __tests__/dom.test.js - ✕ use toBe compare two div (5 ms) - ✕ compare span and div (2 ms) + ✕ use toBe compare two div (<>) + ✕ compare span and div (<>) ● use toBe compare two div @@ -46,6 +46,6 @@ exports[`should work without error 1`] = ` Test Suites: 1 failed, 1 total Tests: 2 failed, 2 total Snapshots: 0 total -Time: 0.569 s, estimated 1 s +Time: <> Ran all test suites." `; diff --git a/e2e/__tests__/domDiffing.test.ts b/e2e/__tests__/domDiffing.test.ts index 4ade0d9de361..1e0295eb5d79 100644 --- a/e2e/__tests__/domDiffing.test.ts +++ b/e2e/__tests__/domDiffing.test.ts @@ -1,9 +1,8 @@ import runJest from '../runJest'; -import {extractSortedSummary} from '../Utils'; +import {replaceTime} from '../Utils'; test('should work without error', () => { const output = runJest('dom-diffing'); - console.log(extractSortedSummary(output.stderr)); expect(output.failed).toBe(true); - expect(output.stderr).toMatchSnapshot(); + expect(replaceTime(output.stderr)).toMatchSnapshot(); }); From b90b04f5e6478eda931e4c2cf5f3c358d9147bbd Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Thu, 20 Aug 2020 01:17:23 +0800 Subject: [PATCH 13/15] add eslint-env and copyright header --- e2e/__tests__/domDiffing.test.ts | 8 ++++++++ e2e/dom-diffing/__tests__/dom.test.js | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/e2e/__tests__/domDiffing.test.ts b/e2e/__tests__/domDiffing.test.ts index 1e0295eb5d79..64d4622ac156 100644 --- a/e2e/__tests__/domDiffing.test.ts +++ b/e2e/__tests__/domDiffing.test.ts @@ -1,3 +1,11 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + import runJest from '../runJest'; import {replaceTime} from '../Utils'; diff --git a/e2e/dom-diffing/__tests__/dom.test.js b/e2e/dom-diffing/__tests__/dom.test.js index 6fd85a785029..9d87fc11b60b 100644 --- a/e2e/dom-diffing/__tests__/dom.test.js +++ b/e2e/dom-diffing/__tests__/dom.test.js @@ -1,3 +1,13 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +/* eslint-env browser */ + test('use toBe compare two div', () => { const div1 = document.createElement('div'); const div2 = document.createElement('div'); From a4208a5e49b32d0f223424180b5c412b3bbb7e70 Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Thu, 20 Aug 2020 01:24:07 +0800 Subject: [PATCH 14/15] add deepCyclicCopyReplaceableDom test --- .../deepCyclicCopyReplaceableDom.test.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceableDom.test.ts diff --git a/packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceableDom.test.ts b/packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceableDom.test.ts new file mode 100644 index 000000000000..31536d9c4372 --- /dev/null +++ b/packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceableDom.test.ts @@ -0,0 +1,29 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @jest-environment jsdom + */ +/* eslint-env browser*/ +import deepCyclicCopyReplaceable from '../deepCyclicCopyReplaceable'; + +test('should copy dom element', () => { + const div = document.createElement('div'); + const copied = deepCyclicCopyReplaceable(div); + expect(copied).toEqual(div); + expect(div === copied).toBe(false); //assert reference is not the same +}); + +test('should copy complex element', () => { + const div = document.createElement('div'); + const span = document.createElement('span'); + div.setAttribute('id', 'div'); + div.innerText = 'this is div'; + div.appendChild(span); + const copied = deepCyclicCopyReplaceable(div); + expect(copied).toEqual(div); + expect(div === copied).toBe(false); //assert reference is not the same + expect(div.children[0] === copied.children[0]).toBe(false); //assert reference is not the same +}); From 6ccc83beac3844f8f57295c7175275b31d3ab5b5 Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Thu, 20 Aug 2020 01:49:23 +0800 Subject: [PATCH 15/15] update snapshot --- .../__snapshots__/domDiffing.test.ts.snap | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/e2e/__tests__/__snapshots__/domDiffing.test.ts.snap b/e2e/__tests__/__snapshots__/domDiffing.test.ts.snap index 69e81da89ecd..a99d8acde447 100644 --- a/e2e/__tests__/__snapshots__/domDiffing.test.ts.snap +++ b/e2e/__tests__/__snapshots__/domDiffing.test.ts.snap @@ -14,15 +14,15 @@ exports[`should work without error 1`] = ` Expected:
Received: serializes to the same string - 2 | const div1 = document.createElement('div'); - 3 | const div2 = document.createElement('div'); - > 4 | expect(div1).toBe(div2); - | ^ - 5 | }); - 6 | - 7 | test('compare span and div', () => { + 12 | const div1 = document.createElement('div'); + 13 | const div2 = document.createElement('div'); + > 14 | expect(div1).toBe(div2); + | ^ + 15 | }); + 16 | + 17 | test('compare span and div', () => { - at Object.toBe (__tests__/dom.test.js:4:16) + at Object.toBe (__tests__/dom.test.js:14:16) ● compare span and div @@ -34,14 +34,14 @@ exports[`should work without error 1`] = ` - +
- 6 | - 7 | test('compare span and div', () => { - > 8 | expect(document.createElement('div')).toBe(document.createElement('span')); + 16 | + 17 | test('compare span and div', () => { + > 18 | expect(document.createElement('div')).toBe(document.createElement('span')); | ^ - 9 | }); - 10 | + 19 | }); + 20 | - at Object.toBe (__tests__/dom.test.js:8:41) + at Object.toBe (__tests__/dom.test.js:18:41) Test Suites: 1 failed, 1 total Tests: 2 failed, 2 total