From b17f430de08bc7516f834d5f3f738032afba43f2 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 21 Apr 2019 15:30:15 -0400 Subject: [PATCH 1/2] expect: Improve report when matcher fails, part 18 --- .../__snapshots__/matchers.test.js.snap | 377 +++++------------- packages/expect/src/matchers.ts | 57 +-- 2 files changed, 119 insertions(+), 315 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 0db6e14b79d4..b3f8a191a0a4 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -3489,13 +3489,8 @@ Expected: not {\\"test\\": {\\"a\\": 1, \\"b\\": 2}} `; exports[`toMatchObject() {pass: false} expect([0]).toMatchObject([-0]) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - [-0] -Received: - [0] -Difference: - Expected + Received @@ -3506,13 +3501,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect([1, 2, 3]).toMatchObject([1, 2, 2]) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - [1, 2, 2] -Received: - [1, 2, 3] -Difference: - Expected + Received @@ -3525,13 +3515,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect([1, 2, 3]).toMatchObject([2, 3, 1]) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - [2, 3, 1] -Received: - [1, 2, 3] -Difference: - Expected + Received @@ -3544,13 +3529,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect([1, 2]).toMatchObject([1, 3]) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - [1, 3] -Received: - [1, 2] -Difference: - Expected + Received @@ -3562,28 +3542,15 @@ Difference: `; exports[`toMatchObject() {pass: false} expect([Error: foo]).toMatchObject([Error: bar]) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - [Error: bar] -Received: - [Error: foo] -Difference: -- Expected -+ Received - -- [Error: bar] -+ [Error: foo]" +Expected: [Error: bar] +Received: [Error: foo]" `; exports[`toMatchObject() {pass: false} expect({"a": "a", "c": "d"}).toMatchObject({"a": Any}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": Any} -Received: - {\\"a\\": \\"a\\", \\"c\\": \\"d\\"} -Difference: - Expected + Received @@ -3594,13 +3561,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": "b", "c": "d"}).toMatchObject({"a": "b!", "c": "d"}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": \\"b!\\", \\"c\\": \\"d\\"} -Received: - {\\"a\\": \\"b\\", \\"c\\": \\"d\\"} -Difference: - Expected + Received @@ -3612,13 +3574,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": "b", "c": "d"}).toMatchObject({"e": "b"}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"e\\": \\"b\\"} -Received: - {\\"a\\": \\"b\\", \\"c\\": \\"d\\"} -Difference: - Expected + Received @@ -3630,13 +3587,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": "b", "t": {"x": {"r": "r"}, "z": "z"}}).toMatchObject({"a": "b", "t": {"z": [3]}}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": \\"b\\", \\"t\\": {\\"z\\": [3]}} -Received: - {\\"a\\": \\"b\\", \\"t\\": {\\"x\\": {\\"r\\": \\"r\\"}, \\"z\\": \\"z\\"}} -Difference: - Expected + Received @@ -3652,13 +3604,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": "b", "t": {"x": {"r": "r"}, "z": "z"}}).toMatchObject({"t": {"l": {"r": "r"}}}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"t\\": {\\"l\\": {\\"r\\": \\"r\\"}}} -Received: - {\\"a\\": \\"b\\", \\"t\\": {\\"x\\": {\\"r\\": \\"r\\"}, \\"z\\": \\"z\\"}} -Difference: - Expected + Received @@ -3674,13 +3621,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": "b"}).toMatchObject({"c": "d"}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"c\\": \\"d\\"} -Received: - {\\"a\\": \\"b\\"} -Difference: - Expected + Received @@ -3691,13 +3633,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": [{"a": "a", "b": "b"}]}).toMatchObject({"a": [{"a": "c"}]}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": [{\\"a\\": \\"c\\"}]} -Received: - {\\"a\\": [{\\"a\\": \\"a\\", \\"b\\": \\"b\\"}]} -Difference: - Expected + Received @@ -3712,13 +3649,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": [3, 4, "v"], "b": "b"}).toMatchObject({"a": ["v"]}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": [\\"v\\"]} -Received: - {\\"a\\": [3, 4, \\"v\\"], \\"b\\": \\"b\\"} -Difference: - Expected + Received @@ -3732,13 +3664,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": [3, 4, 5], "b": "b"}).toMatchObject({"a": [3, 4, 5, 6]}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": [3, 4, 5, 6]} -Received: - {\\"a\\": [3, 4, 5], \\"b\\": \\"b\\"} -Difference: - Expected + Received @@ -3753,13 +3680,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": [3, 4, 5], "b": "b"}).toMatchObject({"a": [3, 4]}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": [3, 4]} -Received: - {\\"a\\": [3, 4, 5], \\"b\\": \\"b\\"} -Difference: - Expected + Received @@ -3773,13 +3695,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": [3, 4, 5], "b": "b"}).toMatchObject({"a": {"b": 4}}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": {\\"b\\": 4}} -Received: - {\\"a\\": [3, 4, 5], \\"b\\": \\"b\\"} -Difference: - Expected + Received @@ -3796,13 +3713,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": [3, 4, 5], "b": "b"}).toMatchObject({"a": {"b": Any}}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": {\\"b\\": Any}} -Received: - {\\"a\\": [3, 4, 5], \\"b\\": \\"b\\"} -Difference: - Expected + Received @@ -3819,13 +3731,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": 1, "b": 1, "c": 1, "d": {"e": {"f": 555}}}).toMatchObject({"d": {"e": {"f": 222}}}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"d\\": {\\"e\\": {\\"f\\": 222}}} -Received: - {\\"a\\": 1, \\"b\\": 1, \\"c\\": 1, \\"d\\": {\\"e\\": {\\"f\\": 555}}} -Difference: - Expected + Received @@ -3840,13 +3747,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": 2015-11-30T00:00:00.000Z, "b": "b"}).toMatchObject({"a": 2015-10-10T00:00:00.000Z}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": 2015-10-10T00:00:00.000Z} -Received: - {\\"a\\": 2015-11-30T00:00:00.000Z, \\"b\\": \\"b\\"} -Difference: - Expected + Received @@ -3857,13 +3759,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": null, "b": "b"}).toMatchObject({"a": "4"}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": \\"4\\"} -Received: - {\\"a\\": null, \\"b\\": \\"b\\"} -Difference: - Expected + Received @@ -3874,13 +3771,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": null, "b": "b"}).toMatchObject({"a": undefined}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": undefined} -Received: - {\\"a\\": null, \\"b\\": \\"b\\"} -Difference: - Expected + Received @@ -3891,13 +3783,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({"a": undefined}).toMatchObject({"a": null}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": null} -Received: - {\\"a\\": undefined} -Difference: - Expected + Received @@ -3908,13 +3795,8 @@ Difference: `; exports[`toMatchObject() {pass: false} expect({}).toMatchObject({"a": undefined}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - {\\"a\\": undefined} -Received: - {} -Difference: - Expected + Received @@ -3925,28 +3807,15 @@ Difference: `; exports[`toMatchObject() {pass: false} expect(2015-11-30T00:00:00.000Z).toMatchObject(2015-10-10T00:00:00.000Z) 1`] = ` -"expect(received).toMatchObject(expected) - -Expected value to match object: - 2015-10-10T00:00:00.000Z -Received: - 2015-11-30T00:00:00.000Z -Difference: -- Expected -+ Received +"expect(received).toMatchObject(expected) -- 2015-10-10T00:00:00.000Z -+ 2015-11-30T00:00:00.000Z" +Expected: 2015-10-10T00:00:00.000Z +Received: 2015-11-30T00:00:00.000Z" `; exports[`toMatchObject() {pass: false} expect(Set {1, 2}).toMatchObject(Set {2}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) -Expected value to match object: - Set {2} -Received: - Set {1, 2} -Difference: - Expected + Received @@ -3957,205 +3826,153 @@ Difference: `; exports[`toMatchObject() {pass: true} expect([]).toMatchObject([]) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - [] -Received: - []" +Expected: not []" `; exports[`toMatchObject() {pass: true} expect([1, 2]).toMatchObject([1, 2]) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - [1, 2] -Received: - [1, 2]" +Expected: not [1, 2]" `; exports[`toMatchObject() {pass: true} expect([Error: bar]).toMatchObject({"message": "bar"}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"message\\": \\"bar\\"} -Received: - [Error: bar]" +Expected: not {\\"message\\": \\"bar\\"} +Received: [Error: bar]" `; exports[`toMatchObject() {pass: true} expect([Error: foo]).toMatchObject([Error: foo]) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - [Error: foo] -Received: - [Error: foo]" +Expected: not [Error: foo]" `; exports[`toMatchObject() {pass: true} expect({"a": "b", "c": "d"}).toMatchObject({"a": "b", "c": "d"}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": \\"b\\", \\"c\\": \\"d\\"} -Received: - {\\"a\\": \\"b\\", \\"c\\": \\"d\\"}" +Expected: not {\\"a\\": \\"b\\", \\"c\\": \\"d\\"}" `; exports[`toMatchObject() {pass: true} expect({"a": "b", "c": "d"}).toMatchObject({"a": "b"}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": \\"b\\"} -Received: - {\\"a\\": \\"b\\", \\"c\\": \\"d\\"}" +Expected: not {\\"a\\": \\"b\\"} +Received: {\\"a\\": \\"b\\", \\"c\\": \\"d\\"}" `; exports[`toMatchObject() {pass: true} expect({"a": "b", "t": {"x": {"r": "r"}, "z": "z"}}).toMatchObject({"a": "b", "t": {"z": "z"}}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": \\"b\\", \\"t\\": {\\"z\\": \\"z\\"}} -Received: - {\\"a\\": \\"b\\", \\"t\\": {\\"x\\": {\\"r\\": \\"r\\"}, \\"z\\": \\"z\\"}}" +Expected: not {\\"a\\": \\"b\\", \\"t\\": {\\"z\\": \\"z\\"}} +Received: {\\"a\\": \\"b\\", \\"t\\": {\\"x\\": {\\"r\\": \\"r\\"}, \\"z\\": \\"z\\"}}" `; exports[`toMatchObject() {pass: true} expect({"a": "b", "t": {"x": {"r": "r"}, "z": "z"}}).toMatchObject({"t": {"x": {"r": "r"}}}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"t\\": {\\"x\\": {\\"r\\": \\"r\\"}}} -Received: - {\\"a\\": \\"b\\", \\"t\\": {\\"x\\": {\\"r\\": \\"r\\"}, \\"z\\": \\"z\\"}}" +Expected: not {\\"t\\": {\\"x\\": {\\"r\\": \\"r\\"}}} +Received: {\\"a\\": \\"b\\", \\"t\\": {\\"x\\": {\\"r\\": \\"r\\"}, \\"z\\": \\"z\\"}}" `; exports[`toMatchObject() {pass: true} expect({"a": "b"}).toMatchObject({"a": "b"}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": \\"b\\"} -Received: - {\\"a\\": \\"b\\"}" +Expected: not {\\"a\\": \\"b\\"}" `; exports[`toMatchObject() {pass: true} expect({"a": [{"a": "a", "b": "b"}]}).toMatchObject({"a": [{"a": "a"}]}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": [{\\"a\\": \\"a\\"}]} -Received: - {\\"a\\": [{\\"a\\": \\"a\\", \\"b\\": \\"b\\"}]}" +Expected: not {\\"a\\": [{\\"a\\": \\"a\\"}]} +Received: {\\"a\\": [{\\"a\\": \\"a\\", \\"b\\": \\"b\\"}]}" `; exports[`toMatchObject() {pass: true} expect({"a": [3, 4, 5, "v"], "b": "b"}).toMatchObject({"a": [3, 4, 5, "v"]}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": [3, 4, 5, \\"v\\"]} -Received: - {\\"a\\": [3, 4, 5, \\"v\\"], \\"b\\": \\"b\\"}" +Expected: not {\\"a\\": [3, 4, 5, \\"v\\"]} +Received: {\\"a\\": [3, 4, 5, \\"v\\"], \\"b\\": \\"b\\"}" `; exports[`toMatchObject() {pass: true} expect({"a": [3, 4, 5], "b": "b"}).toMatchObject({"a": [3, 4, 5]}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": [3, 4, 5]} -Received: - {\\"a\\": [3, 4, 5], \\"b\\": \\"b\\"}" +Expected: not {\\"a\\": [3, 4, 5]} +Received: {\\"a\\": [3, 4, 5], \\"b\\": \\"b\\"}" `; exports[`toMatchObject() {pass: true} expect({"a": {"x": "x", "y": "y"}}).toMatchObject({"a": {"x": Any}}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": {\\"x\\": Any}} -Received: - {\\"a\\": {\\"x\\": \\"x\\", \\"y\\": \\"y\\"}}" +Expected: not {\\"a\\": {\\"x\\": Any}} +Received: {\\"a\\": {\\"x\\": \\"x\\", \\"y\\": \\"y\\"}}" `; exports[`toMatchObject() {pass: true} expect({"a": 1, "c": 2}).toMatchObject({"a": Any}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": Any} -Received: - {\\"a\\": 1, \\"c\\": 2}" +Expected: not {\\"a\\": Any} +Received: {\\"a\\": 1, \\"c\\": 2}" `; exports[`toMatchObject() {pass: true} expect({"a": 2015-11-30T00:00:00.000Z, "b": "b"}).toMatchObject({"a": 2015-11-30T00:00:00.000Z}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": 2015-11-30T00:00:00.000Z} -Received: - {\\"a\\": 2015-11-30T00:00:00.000Z, \\"b\\": \\"b\\"}" +Expected: not {\\"a\\": 2015-11-30T00:00:00.000Z} +Received: {\\"a\\": 2015-11-30T00:00:00.000Z, \\"b\\": \\"b\\"}" `; exports[`toMatchObject() {pass: true} expect({"a": null, "b": "b"}).toMatchObject({"a": null}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": null} -Received: - {\\"a\\": null, \\"b\\": \\"b\\"}" +Expected: not {\\"a\\": null} +Received: {\\"a\\": null, \\"b\\": \\"b\\"}" `; exports[`toMatchObject() {pass: true} expect({"a": undefined, "b": "b"}).toMatchObject({"a": undefined}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": undefined} -Received: - {\\"a\\": undefined, \\"b\\": \\"b\\"}" +Expected: not {\\"a\\": undefined} +Received: {\\"a\\": undefined, \\"b\\": \\"b\\"}" `; exports[`toMatchObject() {pass: true} expect({"a": undefined}).toMatchObject({"a": undefined}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": undefined} -Received: - {\\"a\\": undefined}" +Expected: not {\\"a\\": undefined}" `; exports[`toMatchObject() {pass: true} expect({}).toMatchObject({"a": undefined, "b": "b"}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - {\\"a\\": undefined, \\"b\\": \\"b\\"} -Received: - {}" +Expected: not {\\"a\\": undefined, \\"b\\": \\"b\\"} +Received: {}" `; exports[`toMatchObject() {pass: true} expect(2015-11-30T00:00:00.000Z).toMatchObject(2015-11-30T00:00:00.000Z) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - 2015-11-30T00:00:00.000Z -Received: - 2015-11-30T00:00:00.000Z" +Expected: not 2015-11-30T00:00:00.000Z" `; exports[`toMatchObject() {pass: true} expect(Set {1, 2}).toMatchObject(Set {1, 2}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - Set {1, 2} -Received: - Set {1, 2}" +Expected: not Set {1, 2}" `; exports[`toMatchObject() {pass: true} expect(Set {1, 2}).toMatchObject(Set {2, 1}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) -Expected value not to match object: - Set {2, 1} -Received: - Set {1, 2}" +Expected: not Set {2, 1} +Received: Set {1, 2}" `; exports[`toMatchObject() throws expect("44").toMatchObject({}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) Matcher error: received value must be a non-null object @@ -4164,7 +3981,7 @@ Received has value: \\"44\\"" `; exports[`toMatchObject() throws expect({}).toMatchObject("some string") 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) Matcher error: expected value must be a non-null object @@ -4173,7 +3990,7 @@ Expected has value: \\"some string\\"" `; exports[`toMatchObject() throws expect({}).toMatchObject(4) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) Matcher error: expected value must be a non-null object @@ -4182,7 +3999,7 @@ Expected has value: 4" `; exports[`toMatchObject() throws expect({}).toMatchObject(null) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) Matcher error: expected value must be a non-null object @@ -4190,7 +4007,7 @@ Expected has value: null" `; exports[`toMatchObject() throws expect({}).toMatchObject(true) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) Matcher error: expected value must be a non-null object @@ -4199,7 +4016,7 @@ Expected has value: true" `; exports[`toMatchObject() throws expect({}).toMatchObject(undefined) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) Matcher error: expected value must be a non-null object @@ -4207,7 +4024,7 @@ Expected has value: undefined" `; exports[`toMatchObject() throws expect(4).toMatchObject({}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) Matcher error: received value must be a non-null object @@ -4216,7 +4033,7 @@ Received has value: 4" `; exports[`toMatchObject() throws expect(null).toMatchObject({}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) Matcher error: received value must be a non-null object @@ -4224,7 +4041,7 @@ Received has value: null" `; exports[`toMatchObject() throws expect(true).toMatchObject({}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) Matcher error: received value must be a non-null object @@ -4233,7 +4050,7 @@ Received has value: true" `; exports[`toMatchObject() throws expect(undefined).toMatchObject({}) 1`] = ` -"expect(received).toMatchObject(expected) +"expect(received).toMatchObject(expected) Matcher error: received value must be a non-null object diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index 6f31c11f8528..2309facee19a 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -12,7 +12,6 @@ import { EXPECTED_COLOR, RECEIVED_COLOR, SUGGEST_TO_CONTAIN_EQUAL, - diff, ensureExpectedIsNonNegativeInteger, ensureNoExpected, ensureNumbers, @@ -850,65 +849,53 @@ const matchers: MatchersObject = { return {message, pass}; }, - toMatchObject( - this: MatcherState, - receivedObject: object, - expectedObject: object, - ) { - const matcherName = '.toMatchObject'; + toMatchObject(this: MatcherState, received: object, expected: object) { + const matcherName = 'toMatchObject'; const options: MatcherHintOptions = { isNot: this.isNot, + promise: this.promise, }; - if (typeof receivedObject !== 'object' || receivedObject === null) { + if (typeof received !== 'object' || received === null) { throw new Error( matcherErrorMessage( matcherHint(matcherName, undefined, undefined, options), `${RECEIVED_COLOR('received')} value must be a non-null object`, - printWithType('Received', receivedObject, printReceived), + printWithType('Received', received, printReceived), ), ); } - if (typeof expectedObject !== 'object' || expectedObject === null) { + if (typeof expected !== 'object' || expected === null) { throw new Error( matcherErrorMessage( matcherHint(matcherName, undefined, undefined, options), `${EXPECTED_COLOR('expected')} value must be a non-null object`, - printWithType('Expected', expectedObject, printExpected), + printWithType('Expected', expected, printExpected), ), ); } - const pass = equals(receivedObject, expectedObject, [ - iterableEquality, - subsetEquality, - ]); + const pass = equals(received, expected, [iterableEquality, subsetEquality]); const message = pass ? () => matcherHint(matcherName, undefined, undefined, options) + - `\n\nExpected value not to match object:\n` + - ` ${printExpected(expectedObject)}` + - `\nReceived:\n` + - ` ${printReceived(receivedObject)}` - : () => { - const difference = diff( - expectedObject, - getObjectSubset(receivedObject, expectedObject), - { - expand: this.expand, - }, - ); - return ( - matcherHint(matcherName, undefined, undefined, options) + - `\n\nExpected value to match object:\n` + - ` ${printExpected(expectedObject)}` + - `\nReceived:\n` + - ` ${printReceived(receivedObject)}` + - (difference ? `\nDifference:\n${difference}` : '') + '\n\n' + + `Expected: not ${printExpected(expected)}` + + (stringify(expected) !== stringify(received) + ? `\nReceived: ${printReceived(received)}` + : '') + : () => + matcherHint(matcherName, undefined, undefined, options) + + '\n\n' + + printDiffOrStringify( + expected, + getObjectSubset(received, expected), + EXPECTED_LABEL, + RECEIVED_LABEL, + this.expand, ); - }; return {message, pass}; }, From 8b3e0956b6835587ccb7c5e1ef7b36b111723183 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sun, 21 Apr 2019 15:43:46 -0400 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 173773078bf6..d4d5fe700a5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - `[jest-runner]` Pass docblock pragmas to TestEnvironment constructor ([#8320](https://github.com/facebook/jest/pull/8320)) - `[docs]` Add DynamoDB guide ([#8319](https://github.com/facebook/jest/pull/8319)) - `[expect]` Improve report when matcher fails, part 17 ([#8349](https://github.com/facebook/jest/pull/8349)) +- `[expect]` Improve report when matcher fails, part 18 ([#8356](https://github.com/facebook/jest/pull/8356)) ### Fixes