From 8e663ef988f100bfa26dbb165986909681ec969a Mon Sep 17 00:00:00 2001 From: Joachim Seminck Date: Thu, 24 Aug 2017 08:01:17 +0300 Subject: [PATCH 1/4] Add support for Errors in toMatchObject() --- .../__snapshots__/basic-support.test.js.snap | 7 ++++++ .../__tests__/basic-support.test.js | 1 + .../__snapshots__/matchers.test.js.snap | 24 +++++++++++++++++++ .../src/__tests__/matchers.test.js | 2 ++ packages/jest-matchers/src/matchers.js | 3 +++ 5 files changed, 37 insertions(+) create mode 100644 integration_tests/toMatchSnapshot/__tests__/__snapshots__/basic-support.test.js.snap create mode 100644 integration_tests/toMatchSnapshot/__tests__/basic-support.test.js diff --git a/integration_tests/toMatchSnapshot/__tests__/__snapshots__/basic-support.test.js.snap b/integration_tests/toMatchSnapshot/__tests__/__snapshots__/basic-support.test.js.snap new file mode 100644 index 000000000000..6bf3a2470ffc --- /dev/null +++ b/integration_tests/toMatchSnapshot/__tests__/__snapshots__/basic-support.test.js.snap @@ -0,0 +1,7 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`snapshots 1`] = ` +Object { + "apple": "original value", +} +`; diff --git a/integration_tests/toMatchSnapshot/__tests__/basic-support.test.js b/integration_tests/toMatchSnapshot/__tests__/basic-support.test.js new file mode 100644 index 000000000000..37c89c00d03f --- /dev/null +++ b/integration_tests/toMatchSnapshot/__tests__/basic-support.test.js @@ -0,0 +1 @@ +test('snapshots', () => expect({apple: "updated value"}).toMatchSnapshot()); \ No newline at end of file diff --git a/packages/jest-matchers/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/jest-matchers/src/__tests__/__snapshots__/matchers.test.js.snap index f9ddb5314793..69632c8a590c 100644 --- a/packages/jest-matchers/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/jest-matchers/src/__tests__/__snapshots__/matchers.test.js.snap @@ -2914,6 +2914,21 @@ Difference: ]" `; +exports[`toMatchObject() {pass: false} expect([Error: foo]).toMatchObject([Error: bar]) 1`] = ` +"expect(received).toMatchObject(expected) + +Expected value to match object: + [Error: bar] +Received: + [Error: foo] +Difference: +- Expected ++ Received + +-[Error: bar] ++[Error: foo]" +`; + exports[`toMatchObject() {pass: false} expect({"a": "a", "c": "d"}).toMatchObject({"a": Any}) 1`] = ` "expect(received).toMatchObject(expected) @@ -3295,6 +3310,15 @@ Received: [1, 2]" `; +exports[`toMatchObject() {pass: true} expect([Error: foo]).toMatchObject([Error: foo]) 1`] = ` +"expect(received).not.toMatchObject(expected) + +Expected value not to match object: + [Error: foo] +Received: + [Error: foo]" +`; + exports[`toMatchObject() {pass: true} expect({"a": "b", "c": "d"}).toMatchObject({"a": "b", "c": "d"}) 1`] = ` "expect(received).not.toMatchObject(expected) diff --git a/packages/jest-matchers/src/__tests__/matchers.test.js b/packages/jest-matchers/src/__tests__/matchers.test.js index e5616c312b06..22ad6db509d0 100644 --- a/packages/jest-matchers/src/__tests__/matchers.test.js +++ b/packages/jest-matchers/src/__tests__/matchers.test.js @@ -848,6 +848,7 @@ describe('toMatchObject()', () => { [[1, 2], [1, 2]], [{a: undefined}, {a: undefined}], [[], []], + [new Error('foo'), new Error('foo')], ].forEach(([n1, n2]) => { it(`{pass: true} expect(${stringify(n1)}).toMatchObject(${stringify( n2, @@ -883,6 +884,7 @@ describe('toMatchObject()', () => { [{}, {a: undefined}], [[1, 2, 3], [2, 3, 1]], [[1, 2, 3], [1, 2, 2]], + [new Error('foo'), new Error('bar')], ].forEach(([n1, n2]) => { it(`{pass: false} expect(${stringify(n1)}).toMatchObject(${stringify( n2, diff --git a/packages/jest-matchers/src/matchers.js b/packages/jest-matchers/src/matchers.js index 6ea550dfad63..6de8c8c46128 100644 --- a/packages/jest-matchers/src/matchers.js +++ b/packages/jest-matchers/src/matchers.js @@ -42,12 +42,15 @@ type ContainIterable = const isObjectWithKeys = a => a !== null && typeof a === 'object' && + !(a instanceof Error) && !(a instanceof Array) && !(a instanceof Date); + const subsetEquality = (object, subset) => { if (!isObjectWithKeys(object) || !isObjectWithKeys(subset)) { return undefined; } + return Object.keys(subset).every( key => hasOwnProperty(object, key) && From 6170ced67afebd91a2f4b673f132b9a0a128a313 Mon Sep 17 00:00:00 2001 From: Joachim Seminck Date: Thu, 24 Aug 2017 08:11:58 +0300 Subject: [PATCH 2/4] Move subsetEqualityMatcher and isObjectWithKeys to the utils directory --- packages/jest-matchers/src/matchers.js | 20 +------------------- packages/jest-matchers/src/utils.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/packages/jest-matchers/src/matchers.js b/packages/jest-matchers/src/matchers.js index 6de8c8c46128..c2bd907d67be 100644 --- a/packages/jest-matchers/src/matchers.js +++ b/packages/jest-matchers/src/matchers.js @@ -29,6 +29,7 @@ import { getPath, hasOwnProperty, iterableEquality, + subsetEquality, } from './utils'; import {equals} from './jasmine_utils'; @@ -39,25 +40,6 @@ type ContainIterable = | DOMTokenList | HTMLCollection; -const isObjectWithKeys = a => - a !== null && - typeof a === 'object' && - !(a instanceof Error) && - !(a instanceof Array) && - !(a instanceof Date); - -const subsetEquality = (object, subset) => { - if (!isObjectWithKeys(object) || !isObjectWithKeys(subset)) { - return undefined; - } - - return Object.keys(subset).every( - key => - hasOwnProperty(object, key) && - equals(object[key], subset[key], [iterableEquality, subsetEquality]), - ); -}; - const matchers: MatchersObject = { toBe(received: any, expected: number) { const pass = received === expected; diff --git a/packages/jest-matchers/src/utils.js b/packages/jest-matchers/src/utils.js index 53ff7e59dd58..69135bedf1cc 100644 --- a/packages/jest-matchers/src/utils.js +++ b/packages/jest-matchers/src/utils.js @@ -143,6 +143,25 @@ export const iterableEquality = (a: any, b: any) => { return true; }; +const isObjectWithKeys = a => + a !== null && + typeof a === 'object' && + !(a instanceof Error) && + !(a instanceof Array) && + !(a instanceof Date); + +export const subsetEquality = (object, subset) => { + if (!isObjectWithKeys(object) || !isObjectWithKeys(subset)) { + return undefined; + } + + return Object.keys(subset).every( + key => + hasOwnProperty(object, key) && + equals(object[key], subset[key], [iterableEquality, subsetEquality]), + ); +}; + export const partition = ( items: Array, predicate: T => boolean, From b2b437ae3169497acd6e33d7a78d658b47cff01d Mon Sep 17 00:00:00 2001 From: Joachim Seminck Date: Thu, 24 Aug 2017 12:38:04 +0300 Subject: [PATCH 3/4] Remove files that were created... somehow... --- .../__tests__/__snapshots__/basic-support.test.js.snap | 7 ------- .../toMatchSnapshot/__tests__/basic-support.test.js | 1 - 2 files changed, 8 deletions(-) delete mode 100644 integration_tests/toMatchSnapshot/__tests__/__snapshots__/basic-support.test.js.snap delete mode 100644 integration_tests/toMatchSnapshot/__tests__/basic-support.test.js diff --git a/integration_tests/toMatchSnapshot/__tests__/__snapshots__/basic-support.test.js.snap b/integration_tests/toMatchSnapshot/__tests__/__snapshots__/basic-support.test.js.snap deleted file mode 100644 index 6bf3a2470ffc..000000000000 --- a/integration_tests/toMatchSnapshot/__tests__/__snapshots__/basic-support.test.js.snap +++ /dev/null @@ -1,7 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`snapshots 1`] = ` -Object { - "apple": "original value", -} -`; diff --git a/integration_tests/toMatchSnapshot/__tests__/basic-support.test.js b/integration_tests/toMatchSnapshot/__tests__/basic-support.test.js deleted file mode 100644 index 37c89c00d03f..000000000000 --- a/integration_tests/toMatchSnapshot/__tests__/basic-support.test.js +++ /dev/null @@ -1 +0,0 @@ -test('snapshots', () => expect({apple: "updated value"}).toMatchSnapshot()); \ No newline at end of file From 220d69b8eb8e2eaa38fa43d29a23fd6ed0b41684 Mon Sep 17 00:00:00 2001 From: Joachim Seminck Date: Thu, 24 Aug 2017 12:53:46 +0300 Subject: [PATCH 4/4] Add types for subsetEquality parameters --- packages/jest-matchers/src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-matchers/src/utils.js b/packages/jest-matchers/src/utils.js index 69135bedf1cc..750d26861cfc 100644 --- a/packages/jest-matchers/src/utils.js +++ b/packages/jest-matchers/src/utils.js @@ -150,7 +150,7 @@ const isObjectWithKeys = a => !(a instanceof Array) && !(a instanceof Date); -export const subsetEquality = (object, subset) => { +export const subsetEquality = (object: Object, subset: Object) => { if (!isObjectWithKeys(object) || !isObjectWithKeys(subset)) { return undefined; }