From b6f9b4811ef155e321f60f7a4d61aca72c9778c9 Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Thu, 24 May 2018 16:32:52 +0300 Subject: [PATCH 1/2] fix(matchers): a more robust isDomNode --- .../__snapshots__/matchers.test.js.snap | 22 ++++++++++++++++ .../expect/src/__tests__/matchers.test.js | 14 ++++++++++ packages/expect/src/jasmine_utils.js | 26 ++++++++++++------- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 5dd69d14b74e..6667bd36aac4 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1916,6 +1916,28 @@ Received: {\\"a\\": 99}" `; +exports[`.toEqual() {pass: false} expect({"target": {"nodeType": 1, "value": "a"}}).toEqual({"target": {"nodeType": 1, "value": "b"}}) 1`] = ` +"expect(received).toEqual(expected) + +Expected value to equal: + {\\"target\\": {\\"nodeType\\": 1, \\"value\\": \\"b\\"}} +Received: + {\\"target\\": {\\"nodeType\\": 1, \\"value\\": \\"a\\"}} + +Difference: + +- Expected ++ Received + + Object { + \\"target\\": Object { + \\"nodeType\\": 1, +- \\"value\\": \\"b\\", ++ \\"value\\": \\"a\\", + }, + }" +`; + exports[`.toEqual() {pass: false} expect({}).not.toEqual({}) 1`] = ` "expect(received).not.toEqual(expected) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 2b2d142f85f5..69e04a4e79b8 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -294,6 +294,20 @@ describe('.toEqual()', () => { }, }, ], + [ + { + target: { + nodeType: 1, + value: 'a', + }, + }, + { + target: { + nodeType: 1, + value: 'b', + }, + }, + ], ].forEach(([a, b]) => { test(`{pass: false} expect(${stringify(a)}).toEqual(${stringify( b, diff --git a/packages/expect/src/jasmine_utils.js b/packages/expect/src/jasmine_utils.js index 404a45f64528..6fc2f6d2e89f 100644 --- a/packages/expect/src/jasmine_utils.js +++ b/packages/expect/src/jasmine_utils.js @@ -28,7 +28,12 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. type Tester = (a: any, b: any) => boolean | typeof undefined; // Extracted out of jasmine 2.5.2 -export function equals(a: any, b: any, customTesters?: Array, strictCheck?: boolean): boolean { +export function equals( + a: any, + b: any, + customTesters?: Array, + strictCheck?: boolean, +): boolean { customTesters = customTesters || []; return eq(a, b, [], [], customTesters, strictCheck ? hasKey : hasDefinedKey); } @@ -181,7 +186,9 @@ function eq(a, b, aStack, bStack, customTesters, hasKey): boolean { key = aKeys[size]; // Deep compare each member - result = hasKey(b, key) && eq(a[key], b[key], aStack, bStack, customTesters, hasKey); + result = + hasKey(b, key) && + eq(a[key], b[key], aStack, bStack, customTesters, hasKey); if (!result) { return false; @@ -224,15 +231,11 @@ function keys(obj, isArray, hasKey) { } function hasDefinedKey(obj, key) { - return ( - hasKey(obj, key) && obj[key] !== undefined - ); + return hasKey(obj, key) && obj[key] !== undefined; } function hasKey(obj, key) { - return ( - Object.prototype.hasOwnProperty.call(obj, key) - ); + return Object.prototype.hasOwnProperty.call(obj, key); } export function isA(typeName: string, value: any) { @@ -240,7 +243,12 @@ export function isA(typeName: string, value: any) { } function isDomNode(obj) { - return obj.nodeType > 0; + return ( + obj !== null && + typeof obj === 'object' && + typeof obj.nodeType === 'number' && + typeof obj.nodeName === 'string' + ); } export function fnNameFor(func: Function) { From 8d3d92d0e270ec4545f7e7529b0f88c742e642c7 Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Thu, 24 May 2018 16:41:44 +0300 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7ae05772c6e..d16fd143140f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -101,6 +101,8 @@ ### Fixes +* `[expect]` Better detection of DOM Nodes for equality + ([#6246](https://github.com/facebook/jest/pull/6246)) * `[jest-cli]` Fix misleading action description for F key when in "only failed tests" mode. ([#6167](https://github.com/facebook/jest/issues/6167)) * `[jest-worker]` Stick calls to workers before processing them