diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f0cd29a31e2..30584b8c5f9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `[jest-jasmine2]` Will now only execute at most 5 concurrent tests _within the same testsuite_ when using `test.concurrent` ([#7770](https://github.com/facebook/jest/pull/7770)) - `[jest-circus]` Same as `[jest-jasmine2]`, only 5 tests will run concurrently by default ([#7770](https://github.com/facebook/jest/pull/7770)) - `[jest-config]` A new `maxConcurrency` option allows to change the number of tests allowed to run concurrently ([#7770](https://github.com/facebook/jest/pull/7770)) +- `[expect]` Clarify errors with `.not` and equal Expected and Received ([#7795](https://github.com/facebook/jest/pull/7795)) ### Fixes diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 1debb403a675..d3ea1ba505fe 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -244,50 +244,50 @@ Difference: exports[`.toBe() fails for '"a"' with '.not' 1`] = ` "expect(received).not.toBe(expected) // Object.is equality -Expected: \\"a\\" -Received: \\"a\\"" +Received: \\"a\\" +Expected: any other value" `; exports[`.toBe() fails for '[]' with '.not' 1`] = ` "expect(received).not.toBe(expected) // Object.is equality -Expected: [] -Received: []" +Received: [] +Expected: any other value" `; exports[`.toBe() fails for '{}' with '.not' 1`] = ` "expect(received).not.toBe(expected) // Object.is equality -Expected: {} -Received: {}" +Received: {} +Expected: any other value" `; exports[`.toBe() fails for '1' with '.not' 1`] = ` "expect(received).not.toBe(expected) // Object.is equality -Expected: 1 -Received: 1" +Received: 1 +Expected: any other value" `; exports[`.toBe() fails for 'false' with '.not' 1`] = ` "expect(received).not.toBe(expected) // Object.is equality -Expected: false -Received: false" +Received: false +Expected: any other value" `; exports[`.toBe() fails for 'null' with '.not' 1`] = ` "expect(received).not.toBe(expected) // Object.is equality -Expected: null -Received: null" +Received: null +Expected: any other value" `; exports[`.toBe() fails for 'undefined' with '.not' 1`] = ` "expect(received).not.toBe(expected) // Object.is equality -Expected: undefined -Received: undefined" +Received: undefined +Expected: any other value" `; exports[`.toBe() fails for: "abc" and "cde" 1`] = ` @@ -1165,22 +1165,25 @@ Received value: undefined" exports[`.toBeInstanceOf() passing [] and [Function Array] 1`] = ` "expect(value).not.toBeInstanceOf(constructor) -Expected constructor: Array -Received value: []" +Received value: [] +Received constructor: Array +Expected: any other constructor" `; exports[`.toBeInstanceOf() passing {} and [Function A] 1`] = ` "expect(value).not.toBeInstanceOf(constructor) -Expected constructor: A -Received value: {}" +Received value: {} +Received constructor: A +Expected: any other constructor" `; exports[`.toBeInstanceOf() passing Map {} and [Function Map] 1`] = ` "expect(value).not.toBeInstanceOf(constructor) -Expected constructor: Map -Received value: Map {}" +Received value: Map {} +Received constructor: Map +Expected: any other constructor" `; exports[`.toBeInstanceOf() throws if constructor is not a function 1`] = ` @@ -1709,8 +1712,8 @@ Received has value: null" exports[`.toEqual() {pass: false} expect("Alice").not.toEqual({"asymmetricMatch": [Function asymmetricMatch]}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: {\\"asymmetricMatch\\": [Function asymmetricMatch]} -Received: \\"Alice\\"" +Received: \\"Alice\\" +Expected: any different value" `; exports[`.toEqual() {pass: false} expect("Eve").toEqual({"asymmetricMatch": [Function asymmetricMatch]}) 1`] = ` @@ -1723,22 +1726,22 @@ Received: \\"Eve\\"" exports[`.toEqual() {pass: false} expect("abc").not.toEqual("abc") 1`] = ` "expect(received).not.toEqual(expected) -Expected: \\"abc\\" -Received: \\"abc\\"" +Received: \\"abc\\" +Expected: any different value" `; exports[`.toEqual() {pass: false} expect("abcd").not.toEqual(StringContaining "bc") 1`] = ` "expect(received).not.toEqual(expected) -Expected: StringContaining \\"bc\\" -Received: \\"abcd\\"" +Received: \\"abcd\\" +Expected: any different value" `; exports[`.toEqual() {pass: false} expect("abcd").not.toEqual(StringMatching /bc/) 1`] = ` "expect(received).not.toEqual(expected) -Expected: StringMatching /bc/ -Received: \\"abcd\\"" +Received: \\"abcd\\" +Expected: any different value" `; exports[`.toEqual() {pass: false} expect("abd").toEqual(StringContaining "bc") 1`] = ` @@ -1770,15 +1773,15 @@ Difference: exports[`.toEqual() {pass: false} expect([1, 2, 3]).not.toEqual(ArrayContaining [2, 3]) 1`] = ` "expect(received).not.toEqual(expected) -Expected: ArrayContaining [2, 3] -Received: [1, 2, 3]" +Received: [1, 2, 3] +Expected: any different value" `; exports[`.toEqual() {pass: false} expect([1, 2]).not.toEqual([1, 2]) 1`] = ` "expect(received).not.toEqual(expected) -Expected: [1, 2] -Received: [1, 2]" +Received: [1, 2] +Expected: any different value" `; exports[`.toEqual() {pass: false} expect([1, 2]).toEqual([2, 1]) 1`] = ` @@ -1815,8 +1818,8 @@ Difference: exports[`.toEqual() {pass: false} expect([1]).not.toEqual([1]) 1`] = ` "expect(received).not.toEqual(expected) -Expected: [1] -Received: [1]" +Received: [1] +Expected: any different value" `; exports[`.toEqual() {pass: false} expect([1]).toEqual([2]) 1`] = ` @@ -1836,22 +1839,22 @@ Difference: exports[`.toEqual() {pass: false} expect([Function anonymous]).not.toEqual(Any) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Any -Received: [Function anonymous]" +Received: [Function anonymous] +Expected: any different value" `; exports[`.toEqual() {pass: false} expect({"a": 1, "b": [Function b], "c": true}).not.toEqual({"a": 1, "b": Any, "c": Anything}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: {\\"a\\": 1, \\"b\\": Any, \\"c\\": Anything} -Received: {\\"a\\": 1, \\"b\\": [Function b], \\"c\\": true}" +Received: {\\"a\\": 1, \\"b\\": [Function b], \\"c\\": true} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect({"a": 1, "b": 2}).not.toEqual(ObjectContaining {"a": 1}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: ObjectContaining {\\"a\\": 1} -Received: {\\"a\\": 1, \\"b\\": 2}" +Received: {\\"a\\": 1, \\"b\\": 2} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect({"a": 1, "b": 2}).toEqual(ObjectContaining {"a": 2}) 1`] = ` @@ -1887,8 +1890,8 @@ Difference: exports[`.toEqual() {pass: false} expect({"a": 99}).not.toEqual({"a": 99}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: {\\"a\\": 99} -Received: {\\"a\\": 99}" +Received: {\\"a\\": 99} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect({"target": {"nodeType": 1, "value": "a"}}).toEqual({"target": {"nodeType": 1, "value": "b"}}) 1`] = ` @@ -1911,8 +1914,8 @@ Difference: exports[`.toEqual() {pass: false} expect({}).not.toEqual({}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: {} -Received: {}" +Received: {} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(0).toEqual(-0) 1`] = ` @@ -1925,8 +1928,8 @@ Received: 0" exports[`.toEqual() {pass: false} expect(1).not.toEqual(1) 1`] = ` "expect(received).not.toEqual(expected) -Expected: 1 -Received: 1" +Received: 1 +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(1).toEqual(2) 1`] = ` @@ -1946,8 +1949,8 @@ Received: 1" exports[`.toEqual() {pass: false} expect(Immutable.List [1, 2]).not.toEqual(Immutable.List [1, 2]) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.List [1, 2] -Received: Immutable.List [1, 2]" +Received: Immutable.List [1, 2] +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.List [1, 2]).toEqual(Immutable.List [2, 1]) 1`] = ` @@ -1968,8 +1971,8 @@ Difference: exports[`.toEqual() {pass: false} expect(Immutable.List [1]).not.toEqual(Immutable.List [1]) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.List [1] -Received: Immutable.List [1]" +Received: Immutable.List [1] +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.List [1]).toEqual(Immutable.List [2]) 1`] = ` @@ -1989,8 +1992,8 @@ Difference: exports[`.toEqual() {pass: false} expect(Immutable.Map {"1": Immutable.Map {"2": {"a": 99}}}).not.toEqual(Immutable.Map {"1": Immutable.Map {"2": {"a": 99}}}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.Map {\\"1\\": Immutable.Map {\\"2\\": {\\"a\\": 99}}} -Received: Immutable.Map {\\"1\\": Immutable.Map {\\"2\\": {\\"a\\": 99}}}" +Received: Immutable.Map {\\"1\\": Immutable.Map {\\"2\\": {\\"a\\": 99}}} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.Map {"1": Immutable.Map {"2": {"a": 99}}}).toEqual(Immutable.Map {"1": Immutable.Map {"2": {"a": 11}}}) 1`] = ` @@ -2042,29 +2045,29 @@ Difference: exports[`.toEqual() {pass: false} expect(Immutable.Map {}).not.toEqual(Immutable.Map {}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.Map {} -Received: Immutable.Map {}" +Received: Immutable.Map {} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.Map {1: "one", 2: "two"}).not.toEqual(Immutable.Map {1: "one", 2: "two"}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.Map {1: \\"one\\", 2: \\"two\\"} -Received: Immutable.Map {1: \\"one\\", 2: \\"two\\"}" +Received: Immutable.Map {1: \\"one\\", 2: \\"two\\"} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.Map {1: "one", 2: "two"}).not.toEqual(Immutable.Map {2: "two", 1: "one"}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.Map {2: \\"two\\", 1: \\"one\\"} -Received: Immutable.Map {1: \\"one\\", 2: \\"two\\"}" +Received: Immutable.Map {1: \\"one\\", 2: \\"two\\"} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.OrderedMap {1: "one", 2: "two"}).not.toEqual(Immutable.OrderedMap {1: "one", 2: "two"}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.OrderedMap {1: \\"one\\", 2: \\"two\\"} -Received: Immutable.OrderedMap {1: \\"one\\", 2: \\"two\\"}" +Received: Immutable.OrderedMap {1: \\"one\\", 2: \\"two\\"} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.OrderedMap {1: "one", 2: "two"}).toEqual(Immutable.OrderedMap {2: "two", 1: "one"}) 1`] = ` @@ -2085,15 +2088,15 @@ Difference: exports[`.toEqual() {pass: false} expect(Immutable.OrderedSet []).not.toEqual(Immutable.OrderedSet []) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.OrderedSet [] -Received: Immutable.OrderedSet []" +Received: Immutable.OrderedSet [] +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.OrderedSet [1, 2]).not.toEqual(Immutable.OrderedSet [1, 2]) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.OrderedSet [1, 2] -Received: Immutable.OrderedSet [1, 2]" +Received: Immutable.OrderedSet [1, 2] +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.OrderedSet [1, 2]).toEqual(Immutable.OrderedSet [2, 1]) 1`] = ` @@ -2114,22 +2117,22 @@ Difference: exports[`.toEqual() {pass: false} expect(Immutable.Set []).not.toEqual(Immutable.Set []) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.Set [] -Received: Immutable.Set []" +Received: Immutable.Set [] +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.Set [1, 2]).not.toEqual(Immutable.Set [1, 2]) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.Set [1, 2] -Received: Immutable.Set [1, 2]" +Received: Immutable.Set [1, 2] +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.Set [1, 2]).not.toEqual(Immutable.Set [2, 1]) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Immutable.Set [2, 1] -Received: Immutable.Set [1, 2]" +Received: Immutable.Set [1, 2] +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Immutable.Set [1, 2]).toEqual(Immutable.Set []) 1`] = ` @@ -2209,22 +2212,22 @@ Difference: exports[`.toEqual() {pass: false} expect(Map {[1] => "one", [2] => "two", [3] => "three", [3] => "four"}).not.toEqual(Map {[3] => "three", [3] => "four", [2] => "two", [1] => "one"}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Map {[3] => \\"three\\", [3] => \\"four\\", [2] => \\"two\\", [1] => \\"one\\"} -Received: Map {[1] => \\"one\\", [2] => \\"two\\", [3] => \\"three\\", [3] => \\"four\\"}" +Received: Map {[1] => \\"one\\", [2] => \\"two\\", [3] => \\"three\\", [3] => \\"four\\"} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Map {[1] => "one", [2] => "two"}).not.toEqual(Map {[2] => "two", [1] => "one"}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Map {[2] => \\"two\\", [1] => \\"one\\"} -Received: Map {[1] => \\"one\\", [2] => \\"two\\"}" +Received: Map {[1] => \\"one\\", [2] => \\"two\\"} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Map {[1] => Map {[1] => "one"}, [2] => Map {[2] => "two"}}).not.toEqual(Map {[2] => Map {[2] => "two"}, [1] => Map {[1] => "one"}}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Map {[2] => Map {[2] => \\"two\\"}, [1] => Map {[1] => \\"one\\"}} -Received: Map {[1] => Map {[1] => \\"one\\"}, [2] => Map {[2] => \\"two\\"}}" +Received: Map {[1] => Map {[1] => \\"one\\"}, [2] => Map {[2] => \\"two\\"}} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Map {[1] => Map {[1] => "one"}}).toEqual(Map {[1] => Map {[1] => "two"}}) 1`] = ` @@ -2250,15 +2253,15 @@ Difference: exports[`.toEqual() {pass: false} expect(Map {{"a": 1} => "one", {"b": 2} => "two"}).not.toEqual(Map {{"b": 2} => "two", {"a": 1} => "one"}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Map {{\\"b\\": 2} => \\"two\\", {\\"a\\": 1} => \\"one\\"} -Received: Map {{\\"a\\": 1} => \\"one\\", {\\"b\\": 2} => \\"two\\"}" +Received: Map {{\\"a\\": 1} => \\"one\\", {\\"b\\": 2} => \\"two\\"} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Map {}).not.toEqual(Map {}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Map {} -Received: Map {}" +Received: Map {} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Map {}).toEqual(Set {}) 1`] = ` @@ -2271,15 +2274,15 @@ Received: Map {}" exports[`.toEqual() {pass: false} expect(Map {1 => "one", 2 => "two"}).not.toEqual(Map {1 => "one", 2 => "two"}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Map {1 => \\"one\\", 2 => \\"two\\"} -Received: Map {1 => \\"one\\", 2 => \\"two\\"}" +Received: Map {1 => \\"one\\", 2 => \\"two\\"} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Map {1 => "one", 2 => "two"}).not.toEqual(Map {2 => "two", 1 => "one"}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Map {2 => \\"two\\", 1 => \\"one\\"} -Received: Map {1 => \\"one\\", 2 => \\"two\\"}" +Received: Map {1 => \\"one\\", 2 => \\"two\\"} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Map {1 => "one", 2 => "two"}).toEqual(Map {1 => "one"}) 1`] = ` @@ -2299,22 +2302,22 @@ Difference: exports[`.toEqual() {pass: false} expect(Map {1 => ["one"], 2 => ["two"]}).not.toEqual(Map {2 => ["two"], 1 => ["one"]}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Map {2 => [\\"two\\"], 1 => [\\"one\\"]} -Received: Map {1 => [\\"one\\"], 2 => [\\"two\\"]}" +Received: Map {1 => [\\"one\\"], 2 => [\\"two\\"]} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Set {[1], [2], [3], [3]}).not.toEqual(Set {[3], [3], [2], [1]}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Set {[3], [3], [2], [1]} -Received: Set {[1], [2], [3], [3]}" +Received: Set {[1], [2], [3], [3]} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Set {[1], [2]}).not.toEqual(Set {[2], [1]}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Set {[2], [1]} -Received: Set {[1], [2]}" +Received: Set {[1], [2]} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Set {[1], [2]}).toEqual(Set {[1], [2], [2]}) 1`] = ` @@ -2360,29 +2363,29 @@ Difference: exports[`.toEqual() {pass: false} expect(Set {{"a": 1}, {"b": 2}}).not.toEqual(Set {{"b": 2}, {"a": 1}}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Set {{\\"b\\": 2}, {\\"a\\": 1}} -Received: Set {{\\"a\\": 1}, {\\"b\\": 2}}" +Received: Set {{\\"a\\": 1}, {\\"b\\": 2}} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Set {}).not.toEqual(Set {}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Set {} -Received: Set {}" +Received: Set {} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Set {1, 2}).not.toEqual(Set {1, 2}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Set {1, 2} -Received: Set {1, 2}" +Received: Set {1, 2} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Set {1, 2}).not.toEqual(Set {2, 1}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Set {2, 1} -Received: Set {1, 2}" +Received: Set {1, 2} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Set {1, 2}).toEqual(Set {}) 1`] = ` @@ -2418,8 +2421,8 @@ Difference: exports[`.toEqual() {pass: false} expect(Set {Set {[1]}, Set {[2]}}).not.toEqual(Set {Set {[2]}, Set {[1]}}) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Set {Set {[2]}, Set {[1]}} -Received: Set {Set {[1]}, Set {[2]}}" +Received: Set {Set {[1]}, Set {[2]}} +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(Set {Set {1}, Set {2}}).toEqual(Set {Set {1}, Set {3}}) 1`] = ` @@ -2458,15 +2461,15 @@ Received: null" exports[`.toEqual() {pass: false} expect(true).not.toEqual(Anything) 1`] = ` "expect(received).not.toEqual(expected) -Expected: Anything -Received: true" +Received: true +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(true).not.toEqual(true) 1`] = ` "expect(received).not.toEqual(expected) -Expected: true -Received: true" +Received: true +Expected: any different value" `; exports[`.toEqual() {pass: false} expect(true).toEqual(false) 1`] = ` @@ -2507,8 +2510,8 @@ Difference: exports[`.toEqual() failure message matches the expected snapshot 2`] = ` "expect(received).not.toEqual(expected) -Expected: {\\"a\\": 1} -Received: {\\"a\\": 1}" +Received: {\\"a\\": 1} +Expected: any different value" `; exports[`.toHaveLength {pass: false} expect("").toHaveLength(1) 1`] = ` @@ -2554,41 +2557,41 @@ Received array: [1, 2]" exports[`.toHaveLength {pass: true} expect("").toHaveLength(0) 1`] = ` "expect(received).not.toHaveLength(length) -Expected length: 0 +Received string: \\"\\" Received length: 0 -Received string: \\"\\"" +Expected: length !== 0" `; exports[`.toHaveLength {pass: true} expect("abc").toHaveLength(3) 1`] = ` "expect(received).not.toHaveLength(length) -Expected length: 3 +Received string: \\"abc\\" Received length: 3 -Received string: \\"abc\\"" +Expected: length !== 3" `; exports[`.toHaveLength {pass: true} expect(["a", "b"]).toHaveLength(2) 1`] = ` "expect(received).not.toHaveLength(length) -Expected length: 2 +Received array: [\\"a\\", \\"b\\"] Received length: 2 -Received array: [\\"a\\", \\"b\\"]" +Expected: length !== 2" `; exports[`.toHaveLength {pass: true} expect([]).toHaveLength(0) 1`] = ` "expect(received).not.toHaveLength(length) -Expected length: 0 +Received array: [] Received length: 0 -Received array: []" +Expected: length !== 0" `; exports[`.toHaveLength {pass: true} expect([1, 2]).toHaveLength(2) 1`] = ` "expect(received).not.toHaveLength(length) -Expected length: 2 +Received array: [1, 2] Received length: 2 -Received array: [1, 2]" +Expected: length !== 2" `; exports[`.toHaveLength error cases 1`] = ` @@ -3270,8 +3273,8 @@ Difference: exports[`.toStrictEqual() matches the expected snapshot when it fails 2`] = ` "expect(received).not.toStrictEqual(expected) -Expected: {\\"test\\": {\\"a\\": 1, \\"b\\": 2}} -Received: {\\"test\\": {\\"a\\": 1, \\"b\\": 2}}" +Received: {\\"test\\": {\\"a\\": 1, \\"b\\": 2}} +Expected: any different value" `; exports[`toMatchObject() {pass: false} expect([0]).toMatchObject([-0]) 1`] = ` diff --git a/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.js.snap index c9779d1f78b8..c960ec2ea4ef 100644 --- a/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.js.snap @@ -117,8 +117,8 @@ Received value: undefined exports[`.toThrow() error class threw, but class should not match (error) 1`] = ` "expect(received).not.toThrow(expected) -Expected name: \\"Err\\" Received name: \\"Error\\" +Expected: any other name Received message: \\"apple\\" @@ -136,8 +136,8 @@ Received message: \\"banana\\" exports[`.toThrow() error-message fail isNot true 1`] = ` "expect(received).not.toThrow(expected) -Expected message: \\"apple\\" Received message: \\"apple\\" +Expected: any other message " `; @@ -262,8 +262,8 @@ Received value: \\"\\" exports[`.toThrow() strings threw, but message should not match (error) 1`] = ` "expect(received).not.toThrow(expected) -Expected substring: \\"apple\\" -Received message: \\"apple\\" +Expected no substring: \\"apple\\" +Received message: \\"apple\\" at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -271,8 +271,8 @@ Received message: \\"apple\\" exports[`.toThrow() strings threw, but message should not match (non-error truthy) 1`] = ` "expect(received).not.toThrow(expected) -Expected substring: \\"Server Error\\" -Received value: \\"Internal Server Error\\" +Expected no substring: \\"Server Error\\" +Received value: \\"Internal Server Error\\" " `; @@ -393,8 +393,8 @@ Received value: undefined exports[`.toThrowError() error class threw, but class should not match (error) 1`] = ` "expect(received).not.toThrowError(expected) -Expected name: \\"Err\\" Received name: \\"Error\\" +Expected: any other name Received message: \\"apple\\" @@ -412,8 +412,8 @@ Received message: \\"banana\\" exports[`.toThrowError() error-message fail isNot true 1`] = ` "expect(received).not.toThrowError(expected) -Expected message: \\"apple\\" Received message: \\"apple\\" +Expected: any other message " `; @@ -538,8 +538,8 @@ Received value: \\"\\" exports[`.toThrowError() strings threw, but message should not match (error) 1`] = ` "expect(received).not.toThrowError(expected) -Expected substring: \\"apple\\" -Received message: \\"apple\\" +Expected no substring: \\"apple\\" +Received message: \\"apple\\" at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74)" `; @@ -547,7 +547,7 @@ Received message: \\"apple\\" exports[`.toThrowError() strings threw, but message should not match (non-error truthy) 1`] = ` "expect(received).not.toThrowError(expected) -Expected substring: \\"Server Error\\" -Received value: \\"Internal Server Error\\" +Expected no substring: \\"Server Error\\" +Received value: \\"Internal Server Error\\" " `; diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 298f1d1cb681..e87ef0e02e4f 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -56,8 +56,8 @@ const matchers: MatchersObject = { isNot: true, }) + '\n\n' + - `Expected: ${printExpected(expected)}\n` + - `Received: ${printReceived(received)}` + `Received: ${printReceived(received)}\n` + + `Expected: ${EXPECTED_COLOR('any other value')}` : () => { const receivedType = getType(received); const expectedType = getType(expected); @@ -189,16 +189,19 @@ const matchers: MatchersObject = { const message = pass ? () => matcherHint('.toBeInstanceOf', 'value', 'constructor', { - isNot: this.isNot, + isNot: true, }) + '\n\n' + - `Expected constructor: ${EXPECTED_COLOR( - constructor.name || String(constructor), + `Received value: ${printReceived(received)}\n` + + `Received constructor: ${RECEIVED_COLOR( + received != null + ? received.constructor && received.constructor.name + : '', )}\n` + - `Received value: ${printReceived(received)}` + `Expected: ${EXPECTED_COLOR('any other constructor')}` : () => matcherHint('.toBeInstanceOf', 'value', 'constructor', { - isNot: this.isNot, + isNot: false, }) + '\n\n' + `Expected constructor: ${EXPECTED_COLOR( @@ -414,17 +417,17 @@ const matchers: MatchersObject = { const message = pass ? () => matcherHint('.toEqual', undefined, undefined, { - isNot: this.isNot, + isNot: true, }) + '\n\n' + - `Expected: ${printExpected(expected)}\n` + - `Received: ${printReceived(received)}` + `Received: ${printReceived(received)}\n` + + `Expected: ${EXPECTED_COLOR('any different value')}` : () => { const diffString = diff(expected, received, {expand: this.expand}); return ( matcherHint('.toEqual', undefined, undefined, { - isNot: this.isNot, + isNot: false, }) + '\n\n' + (diffString && diffString.includes('- Expect') @@ -470,28 +473,41 @@ const matchers: MatchersObject = { ); } - const pass = received.length === length; + const receivedLength = received.length; + const pass = receivedLength === length; const message = () => { - const stringExpected = 'Expected length'; + const stringExpected = 'Expected'; + const stringExpectedLength = 'Expected length'; const stringReceivedLength = 'Received length'; const stringReceivedValue = `Received ${getType(received)}`; const printLabel = getLabelPrinter( stringExpected, + stringExpectedLength, stringReceivedLength, stringReceivedValue, ); - return ( - matcherHint('.toHaveLength', 'received', 'length', { - isNot: this.isNot, - }) + - '\n\n' + - `${printLabel(stringExpected)}${printExpected(length)}\n` + - `${printLabel(stringReceivedLength)}${printReceived( - received.length, - )}\n` + - `${printLabel(stringReceivedValue)}${printReceived(received)}` - ); + return pass + ? matcherHint('.toHaveLength', 'received', 'length', { + isNot: true, + }) + + '\n\n' + + `${printLabel(stringReceivedValue)}${printReceived(received)}\n` + + `${printLabel(stringReceivedLength)}${printReceived( + receivedLength, + )}\n` + + `${printLabel(stringExpected)}${EXPECTED_COLOR( + 'length !== ' + receivedLength, + )}` + : matcherHint('.toHaveLength', 'received', 'length', { + isNot: false, + }) + + '\n\n' + + `${printLabel(stringExpectedLength)}${printExpected(length)}\n` + + `${printLabel(stringReceivedLength)}${printReceived( + receivedLength, + )}\n` + + `${printLabel(stringReceivedValue)}${printReceived(received)}`; }; return {message, pass}; @@ -707,8 +723,8 @@ const matchers: MatchersObject = { ? () => hint + '\n\n' + - `Expected: ${printExpected(expected)}\n` + - `Received: ${printReceived(received)}` + `Received: ${printReceived(received)}\n` + + `Expected: ${EXPECTED_COLOR('any different value')}` : () => { const diffString = diff(expected, received, { expand: this.expand, diff --git a/packages/expect/src/toThrowMatchers.js b/packages/expect/src/toThrowMatchers.js index a53e6979e461..53636d1d16f6 100644 --- a/packages/expect/src/toThrowMatchers.js +++ b/packages/expect/src/toThrowMatchers.js @@ -205,11 +205,12 @@ const toThrowExpectedObject = ( ? () => matcherHint(matcherName, undefined, undefined, options) + '\n\n' + - formatExpected('Expected message: ', expected.message) + (thrown !== null && thrown.hasMessage ? formatReceived('Received message: ', thrown, 'message') + + `Expected: ${EXPECTED_COLOR('any other message')}\n` + formatStack(thrown) - : formatReceived('Received value: ', thrown, 'value')) + : formatExpected('Expected message: ', expected.message) + + formatReceived('Received value: ', thrown, 'value')) : () => matcherHint(matcherName, undefined, undefined, options) + '\n\n' + @@ -236,8 +237,8 @@ const toThrowExpectedClass = ( ? () => matcherHint(matcherName, undefined, undefined, options) + '\n\n' + - formatExpected('Expected name: ', expected.name) + formatReceived('Received name: ', thrown, 'name') + + `Expected: ${EXPECTED_COLOR('any other name')}\n` + '\n' + (thrown !== null && thrown.hasMessage ? formatReceived('Received message: ', thrown, 'message') + @@ -271,11 +272,11 @@ const toThrowExpectedString = ( ? () => matcherHint(matcherName, undefined, undefined, options) + '\n\n' + - formatExpected('Expected substring: ', expected) + + formatExpected('Expected no substring: ', expected) + (thrown !== null && thrown.hasMessage - ? formatReceived('Received message: ', thrown, 'message') + + ? formatReceived('Received message: ', thrown, 'message') + formatStack(thrown) - : formatReceived('Received value: ', thrown, 'value')) + : formatReceived('Received value: ', thrown, 'value')) : () => matcherHint(matcherName, undefined, undefined, options) + '\n\n' +