From 6938483a229c469e84ee6f37a59c552cc059d11f Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Fri, 12 Oct 2018 13:21:57 -0400 Subject: [PATCH 1/8] expect: Improve report when assertion fails, part 3 --- .../__snapshots__/matchers.test.js.snap | 295 ++++++------------ packages/expect/src/matchers.js | 76 ++--- 2 files changed, 133 insertions(+), 238 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 400e8b6e424d..81c7da90c35c 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1469,226 +1469,165 @@ Got: null" `; exports[`.toContain(), .toContainEqual() '"11112111"' contains '"2"' 1`] = ` -"expect(string).not.toContain(value) +"expect(string).not.toContain(value) // indexOf -Expected string: - \\"11112111\\" -Not to contain value: - \\"2\\" -" +Expected value: \\"2\\" +Received string: \\"11112111\\"" `; exports[`.toContain(), .toContainEqual() '"abcdef"' contains '"abc"' 1`] = ` -"expect(string).not.toContain(value) +"expect(string).not.toContain(value) // indexOf -Expected string: - \\"abcdef\\" -Not to contain value: - \\"abc\\" -" +Expected value: \\"abc\\" +Received string: \\"abcdef\\"" `; exports[`.toContain(), .toContainEqual() '["a", "b", "c", "d"]' contains '"a"' 1`] = ` -"expect(array).not.toContain(value) +"expect(array).not.toContain(value) // indexOf -Expected array: - [\\"a\\", \\"b\\", \\"c\\", \\"d\\"] -Not to contain value: - \\"a\\" -" +Expected value: \\"a\\" +Received array: [\\"a\\", \\"b\\", \\"c\\", \\"d\\"]" `; exports[`.toContain(), .toContainEqual() '["a", "b", "c", "d"]' contains a value equal to '"a"' 1`] = ` -"expect(array).not.toContainEqual(value) +"expect(array).not.toContainEqual(value) // deep equality -Expected array: - [\\"a\\", \\"b\\", \\"c\\", \\"d\\"] -Not to contain a value equal to: - \\"a\\" -" +Expected value: \\"a\\" +Received array: [\\"a\\", \\"b\\", \\"c\\", \\"d\\"]" `; exports[`.toContain(), .toContainEqual() '[{"a": "b"}, {"a": "c"}]' contains a value equal to '{"a": "b"}' 1`] = ` -"expect(array).not.toContainEqual(value) +"expect(array).not.toContainEqual(value) // deep equality -Expected array: - [{\\"a\\": \\"b\\"}, {\\"a\\": \\"c\\"}] -Not to contain a value equal to: - {\\"a\\": \\"b\\"} -" +Expected value: {\\"a\\": \\"b\\"} +Received array: [{\\"a\\": \\"b\\"}, {\\"a\\": \\"c\\"}]" `; exports[`.toContain(), .toContainEqual() '[{"a": "b"}, {"a": "c"}]' does not contain a value equal to'{"a": "d"}' 1`] = ` -"expect(array).toContainEqual(value) +"expect(array).toContainEqual(value) // deep equality -Expected array: - [{\\"a\\": \\"b\\"}, {\\"a\\": \\"c\\"}] -To contain a value equal to: - {\\"a\\": \\"d\\"}" +Expected value: {\\"a\\": \\"d\\"} +Received array: [{\\"a\\": \\"b\\"}, {\\"a\\": \\"c\\"}]" `; exports[`.toContain(), .toContainEqual() '[{}, []]' does not contain '[]' 1`] = ` -"expect(array).toContain(value) +"expect(array).toContain(value) // indexOf -Expected array: - [{}, []] -To contain value: - [] +Expected value: [] +Received array: [{}, []] Looks like you wanted to test for object/array equality with the stricter \`toContain\` matcher. You probably need to use \`toContainEqual\` instead." `; exports[`.toContain(), .toContainEqual() '[{}, []]' does not contain '{}' 1`] = ` -"expect(array).toContain(value) +"expect(array).toContain(value) // indexOf -Expected array: - [{}, []] -To contain value: - {} +Expected value: {} +Received array: [{}, []] Looks like you wanted to test for object/array equality with the stricter \`toContain\` matcher. You probably need to use \`toContainEqual\` instead." `; exports[`.toContain(), .toContainEqual() '[0, 1]' contains '1' 1`] = ` -"expect(object).not.toContain(value) +"expect(object).not.toContain(value) // indexOf -Expected object: - [0, 1] -Not to contain value: - 1 -" +Expected value: 1 +Received object: [0, 1]" `; exports[`.toContain(), .toContainEqual() '[0, 1]' contains a value equal to '1' 1`] = ` -"expect(object).not.toContainEqual(value) +"expect(object).not.toContainEqual(value) // deep equality -Expected object: - [0, 1] -Not to contain a value equal to: - 1 -" +Expected value: 1 +Received object: [0, 1]" `; exports[`.toContain(), .toContainEqual() '[1, 2, 3, 4]' contains '1' 1`] = ` -"expect(array).not.toContain(value) +"expect(array).not.toContain(value) // indexOf -Expected array: - [1, 2, 3, 4] -Not to contain value: - 1 -" +Expected value: 1 +Received array: [1, 2, 3, 4]" `; exports[`.toContain(), .toContainEqual() '[1, 2, 3, 4]' contains a value equal to '1' 1`] = ` -"expect(array).not.toContainEqual(value) +"expect(array).not.toContainEqual(value) // deep equality -Expected array: - [1, 2, 3, 4] -Not to contain a value equal to: - 1 -" +Expected value: 1 +Received array: [1, 2, 3, 4]" `; exports[`.toContain(), .toContainEqual() '[1, 2, 3]' does not contain '4' 1`] = ` -"expect(array).toContain(value) +"expect(array).toContain(value) // indexOf -Expected array: - [1, 2, 3] -To contain value: - 4" +Expected value: 4 +Received array: [1, 2, 3]" `; exports[`.toContain(), .toContainEqual() '[Symbol(a)]' contains 'Symbol(a)' 1`] = ` -"expect(array).not.toContain(value) +"expect(array).not.toContain(value) // indexOf -Expected array: - [Symbol(a)] -Not to contain value: - Symbol(a) -" +Expected value: Symbol(a) +Received array: [Symbol(a)]" `; exports[`.toContain(), .toContainEqual() '[Symbol(a)]' contains a value equal to 'Symbol(a)' 1`] = ` -"expect(array).not.toContainEqual(value) +"expect(array).not.toContainEqual(value) // deep equality -Expected array: - [Symbol(a)] -Not to contain a value equal to: - Symbol(a) -" +Expected value: Symbol(a) +Received array: [Symbol(a)]" `; exports[`.toContain(), .toContainEqual() '[null, undefined]' does not contain '1' 1`] = ` -"expect(array).toContain(value) +"expect(array).toContain(value) // indexOf -Expected array: - [null, undefined] -To contain value: - 1" +Expected value: 1 +Received array: [null, undefined]" `; exports[`.toContain(), .toContainEqual() '[undefined, null]' contains 'null' 1`] = ` -"expect(array).not.toContain(value) +"expect(array).not.toContain(value) // indexOf -Expected array: - [undefined, null] -Not to contain value: - null -" +Expected value: null +Received array: [undefined, null]" `; exports[`.toContain(), .toContainEqual() '[undefined, null]' contains 'undefined' 1`] = ` -"expect(array).not.toContain(value) +"expect(array).not.toContain(value) // indexOf -Expected array: - [undefined, null] -Not to contain value: - undefined -" +Expected value: undefined +Received array: [undefined, null]" `; exports[`.toContain(), .toContainEqual() '[undefined, null]' contains a value equal to 'null' 1`] = ` -"expect(array).not.toContainEqual(value) +"expect(array).not.toContainEqual(value) // deep equality -Expected array: - [undefined, null] -Not to contain a value equal to: - null -" +Expected value: null +Received array: [undefined, null]" `; exports[`.toContain(), .toContainEqual() '[undefined, null]' contains a value equal to 'undefined' 1`] = ` -"expect(array).not.toContainEqual(value) +"expect(array).not.toContainEqual(value) // deep equality -Expected array: - [undefined, null] -Not to contain a value equal to: - undefined -" +Expected value: undefined +Received array: [undefined, null]" `; exports[`.toContain(), .toContainEqual() 'Set {"abc", "def"}' contains '"abc"' 1`] = ` -"expect(set).not.toContain(value) +"expect(set).not.toContain(value) // indexOf -Expected set: - Set {\\"abc\\", \\"def\\"} -Not to contain value: - \\"abc\\" -" +Expected value: \\"abc\\" +Received set: Set {\\"abc\\", \\"def\\"}" `; exports[`.toContain(), .toContainEqual() 'Set {1, 2, 3, 4}' contains a value equal to '1' 1`] = ` -"expect(set).not.toContainEqual(value) +"expect(set).not.toContainEqual(value) // deep equality -Expected set: - Set {1, 2, 3, 4} -Not to contain a value equal to: - 1 -" +Expected value: 1 +Received set: Set {1, 2, 3, 4}" `; exports[`.toContain(), .toContainEqual() error cases 1`] = ` -"expect(collection)[.not].toContainEqual(value) +"expect(collection)[.not].toContain(value) Expected collection to be an array-like structure. Received: null" @@ -2742,111 +2681,81 @@ Received: exports[`.toHaveLength {pass: false} expect("").toHaveLength(1) 1`] = ` "expect(received).toHaveLength(length) -Expected value to have length: - 1 -Received: - \\"\\" -received.length: - 0" +Expected length: 1 +Received length: 0 +Received value: \\"\\"" `; exports[`.toHaveLength {pass: false} expect("abc").toHaveLength(66) 1`] = ` "expect(received).toHaveLength(length) -Expected value to have length: - 66 -Received: - \\"abc\\" -received.length: - 3" +Expected length: 66 +Received length: 3 +Received value: \\"abc\\"" `; exports[`.toHaveLength {pass: false} expect(["a", "b"]).toHaveLength(99) 1`] = ` "expect(received).toHaveLength(length) -Expected value to have length: - 99 -Received: - [\\"a\\", \\"b\\"] -received.length: - 2" +Expected length: 99 +Received length: 2 +Received value: [\\"a\\", \\"b\\"]" `; exports[`.toHaveLength {pass: false} expect([]).toHaveLength(1) 1`] = ` "expect(received).toHaveLength(length) -Expected value to have length: - 1 -Received: - [] -received.length: - 0" +Expected length: 1 +Received length: 0 +Received value: []" `; exports[`.toHaveLength {pass: false} expect([1, 2]).toHaveLength(3) 1`] = ` "expect(received).toHaveLength(length) -Expected value to have length: - 3 -Received: - [1, 2] -received.length: - 2" +Expected length: 3 +Received length: 2 +Received value: [1, 2]" `; exports[`.toHaveLength {pass: true} expect("").toHaveLength(0) 1`] = ` -"expect(received).not.toHaveLength(length) +"expect(received).not.toHaveLength(length) -Expected value to not have length: - 0 -Received: - \\"\\" -received.length: - 0" +Expected length: 0 +Received length: 0 +Received value: \\"\\"" `; exports[`.toHaveLength {pass: true} expect("abc").toHaveLength(3) 1`] = ` -"expect(received).not.toHaveLength(length) +"expect(received).not.toHaveLength(length) -Expected value to not have length: - 3 -Received: - \\"abc\\" -received.length: - 3" +Expected length: 3 +Received length: 3 +Received value: \\"abc\\"" `; exports[`.toHaveLength {pass: true} expect(["a", "b"]).toHaveLength(2) 1`] = ` -"expect(received).not.toHaveLength(length) +"expect(received).not.toHaveLength(length) -Expected value to not have length: - 2 -Received: - [\\"a\\", \\"b\\"] -received.length: - 2" +Expected length: 2 +Received length: 2 +Received value: [\\"a\\", \\"b\\"]" `; exports[`.toHaveLength {pass: true} expect([]).toHaveLength(0) 1`] = ` -"expect(received).not.toHaveLength(length) +"expect(received).not.toHaveLength(length) -Expected value to not have length: - 0 -Received: - [] -received.length: - 0" +Expected length: 0 +Received length: 0 +Received value: []" `; exports[`.toHaveLength {pass: true} expect([1, 2]).toHaveLength(2) 1`] = ` -"expect(received).not.toHaveLength(length) +"expect(received).not.toHaveLength(length) -Expected value to not have length: - 2 -Received: - [1, 2] -received.length: - 2" +Expected length: 2 +Received length: 2 +Received value: [1, 2]" `; exports[`.toHaveLength error cases 1`] = ` diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 0d8d235ab1b4..208eceaef639 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -280,7 +280,7 @@ const matchers: MatchersObject = { converted = Array.from(collection); } catch (e) { throw new Error( - matcherHint('[.not].toContainEqual', 'collection', 'value') + + matcherHint('[.not].toContain', 'collection', 'value') + '\n\n' + `Expected ${RECEIVED_COLOR( 'collection', @@ -289,17 +289,19 @@ const matchers: MatchersObject = { ); } } + const comment = 'indexOf'; // At this point, we're either a string or an Array, // which was converted from an array-like structure. const pass = converted.indexOf(value) != -1; const message = pass ? () => - matcherHint('.not.toContain', collectionType, 'value') + + matcherHint('.toContain', collectionType, 'value', { + comment, + isNot: this.isNot, + }) + '\n\n' + - `Expected ${collectionType}:\n` + - ` ${printReceived(collection)}\n` + - `Not to contain value:\n` + - ` ${printExpected(value)}\n` + `Expected value: ${printExpected(value)}\n` + + `Received ${collectionType}: ${printReceived(collection)}` : () => { const suggestToContainEqual = converted !== null && @@ -309,12 +311,13 @@ const matchers: MatchersObject = { equals(item, value, [iterableEquality]), ) !== -1; return ( - matcherHint('.toContain', collectionType, 'value') + + matcherHint('.toContain', collectionType, 'value', { + comment, + isNot: this.isNot, + }) + '\n\n' + - `Expected ${collectionType}:\n` + - ` ${printReceived(collection)}\n` + - `To contain value:\n` + - ` ${printExpected(value)}` + + `Expected value: ${printExpected(value)}\n` + + `Received ${collectionType}: ${printReceived(collection)}` + (suggestToContainEqual ? `\n\n${SUGGEST_TO_CONTAIN_EQUAL}` : '') ); }; @@ -342,24 +345,18 @@ const matchers: MatchersObject = { } } + const comment = 'deep equality'; const pass = converted.findIndex(item => equals(item, value, [iterableEquality])) !== -1; - const message = pass - ? () => - matcherHint('.not.toContainEqual', collectionType, 'value') + - '\n\n' + - `Expected ${collectionType}:\n` + - ` ${printReceived(collection)}\n` + - `Not to contain a value equal to:\n` + - ` ${printExpected(value)}\n` - : () => - matcherHint('.toContainEqual', collectionType, 'value') + - '\n\n' + - `Expected ${collectionType}:\n` + - ` ${printReceived(collection)}\n` + - `To contain a value equal to:\n` + - ` ${printExpected(value)}`; + const message = () => + matcherHint('.toContainEqual', collectionType, 'value', { + comment, + isNot: this.isNot, + }) + + '\n\n' + + `Expected value: ${printExpected(value)}\n` + + `Received ${collectionType}: ${printReceived(collection)}`; return {message, pass}; }, @@ -414,25 +411,14 @@ const matchers: MatchersObject = { } const pass = received.length === length; - const message = pass - ? () => - matcherHint('.not.toHaveLength', 'received', 'length') + - '\n\n' + - `Expected value to not have length:\n` + - ` ${printExpected(length)}\n` + - `Received:\n` + - ` ${printReceived(received)}\n` + - `received.length:\n` + - ` ${printReceived(received.length)}` - : () => - matcherHint('.toHaveLength', 'received', 'length') + - '\n\n' + - `Expected value to have length:\n` + - ` ${printExpected(length)}\n` + - `Received:\n` + - ` ${printReceived(received)}\n` + - `received.length:\n` + - ` ${printReceived(received.length)}`; + const message = () => + matcherHint('.toHaveLength', 'received', 'length', { + isNot: this.isNot, + }) + + '\n\n' + + `Expected length: ${printExpected(length)}\n` + + `Received length: ${printReceived(received.length)}\n` + + `Received value: ${printReceived(received)}`; return {message, pass}; }, From fca74b6b3921c1bb1f19ceefc3f198c09fbae79e Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 15 Oct 2018 12:48:32 -0400 Subject: [PATCH 2/8] Display lines of labeled values as two columns with monospace alignment --- .../__snapshots__/matchers.test.js.snap | 32 ++++++------ packages/expect/src/matchers.js | 33 ++++++++---- .../src/__tests__/index.test.js | 50 ++++++++++++++++++- packages/jest-matcher-utils/src/index.js | 15 ++++++ 4 files changed, 104 insertions(+), 26 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 81c7da90c35c..5515f7697733 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1471,14 +1471,14 @@ Got: null" exports[`.toContain(), .toContainEqual() '"11112111"' contains '"2"' 1`] = ` "expect(string).not.toContain(value) // indexOf -Expected value: \\"2\\" +Expected value: \\"2\\" Received string: \\"11112111\\"" `; exports[`.toContain(), .toContainEqual() '"abcdef"' contains '"abc"' 1`] = ` "expect(string).not.toContain(value) // indexOf -Expected value: \\"abc\\" +Expected value: \\"abc\\" Received string: \\"abcdef\\"" `; @@ -1531,14 +1531,14 @@ Received array: [{}, []] exports[`.toContain(), .toContainEqual() '[0, 1]' contains '1' 1`] = ` "expect(object).not.toContain(value) // indexOf -Expected value: 1 +Expected value: 1 Received object: [0, 1]" `; exports[`.toContain(), .toContainEqual() '[0, 1]' contains a value equal to '1' 1`] = ` "expect(object).not.toContainEqual(value) // deep equality -Expected value: 1 +Expected value: 1 Received object: [0, 1]" `; @@ -1616,14 +1616,14 @@ exports[`.toContain(), .toContainEqual() 'Set {"abc", "def"}' contains '"abc"' 1 "expect(set).not.toContain(value) // indexOf Expected value: \\"abc\\" -Received set: Set {\\"abc\\", \\"def\\"}" +Received set: Set {\\"abc\\", \\"def\\"}" `; exports[`.toContain(), .toContainEqual() 'Set {1, 2, 3, 4}' contains a value equal to '1' 1`] = ` "expect(set).not.toContainEqual(value) // deep equality Expected value: 1 -Received set: Set {1, 2, 3, 4}" +Received set: Set {1, 2, 3, 4}" `; exports[`.toContain(), .toContainEqual() error cases 1`] = ` @@ -2683,7 +2683,7 @@ exports[`.toHaveLength {pass: false} expect("").toHaveLength(1) 1`] = ` Expected length: 1 Received length: 0 -Received value: \\"\\"" +Received value: \\"\\"" `; exports[`.toHaveLength {pass: false} expect("abc").toHaveLength(66) 1`] = ` @@ -2691,7 +2691,7 @@ exports[`.toHaveLength {pass: false} expect("abc").toHaveLength(66) 1`] = ` Expected length: 66 Received length: 3 -Received value: \\"abc\\"" +Received value: \\"abc\\"" `; exports[`.toHaveLength {pass: false} expect(["a", "b"]).toHaveLength(99) 1`] = ` @@ -2699,7 +2699,7 @@ exports[`.toHaveLength {pass: false} expect(["a", "b"]).toHaveLength(99) 1`] = ` Expected length: 99 Received length: 2 -Received value: [\\"a\\", \\"b\\"]" +Received value: [\\"a\\", \\"b\\"]" `; exports[`.toHaveLength {pass: false} expect([]).toHaveLength(1) 1`] = ` @@ -2707,7 +2707,7 @@ exports[`.toHaveLength {pass: false} expect([]).toHaveLength(1) 1`] = ` Expected length: 1 Received length: 0 -Received value: []" +Received value: []" `; exports[`.toHaveLength {pass: false} expect([1, 2]).toHaveLength(3) 1`] = ` @@ -2715,7 +2715,7 @@ exports[`.toHaveLength {pass: false} expect([1, 2]).toHaveLength(3) 1`] = ` Expected length: 3 Received length: 2 -Received value: [1, 2]" +Received value: [1, 2]" `; exports[`.toHaveLength {pass: true} expect("").toHaveLength(0) 1`] = ` @@ -2723,7 +2723,7 @@ exports[`.toHaveLength {pass: true} expect("").toHaveLength(0) 1`] = ` Expected length: 0 Received length: 0 -Received value: \\"\\"" +Received value: \\"\\"" `; exports[`.toHaveLength {pass: true} expect("abc").toHaveLength(3) 1`] = ` @@ -2731,7 +2731,7 @@ exports[`.toHaveLength {pass: true} expect("abc").toHaveLength(3) 1`] = ` Expected length: 3 Received length: 3 -Received value: \\"abc\\"" +Received value: \\"abc\\"" `; exports[`.toHaveLength {pass: true} expect(["a", "b"]).toHaveLength(2) 1`] = ` @@ -2739,7 +2739,7 @@ exports[`.toHaveLength {pass: true} expect(["a", "b"]).toHaveLength(2) 1`] = ` Expected length: 2 Received length: 2 -Received value: [\\"a\\", \\"b\\"]" +Received value: [\\"a\\", \\"b\\"]" `; exports[`.toHaveLength {pass: true} expect([]).toHaveLength(0) 1`] = ` @@ -2747,7 +2747,7 @@ exports[`.toHaveLength {pass: true} expect([]).toHaveLength(0) 1`] = ` Expected length: 0 Received length: 0 -Received value: []" +Received value: []" `; exports[`.toHaveLength {pass: true} expect([1, 2]).toHaveLength(2) 1`] = ` @@ -2755,7 +2755,7 @@ exports[`.toHaveLength {pass: true} expect([1, 2]).toHaveLength(2) 1`] = ` Expected length: 2 Received length: 2 -Received value: [1, 2]" +Received value: [1, 2]" `; exports[`.toHaveLength error cases 1`] = ` diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 208eceaef639..097078cdc4cb 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -19,6 +19,7 @@ import { SUGGEST_TO_CONTAIN_EQUAL, ensureNoExpected, ensureNumbers, + getLabelPrinter, matcherHint, printReceived, printExpected, @@ -290,6 +291,9 @@ const matchers: MatchersObject = { } } const comment = 'indexOf'; + const stringExpected = 'Expected value'; + const stringReceived = `Received ${collectionType}`; + const printLabel = getLabelPrinter(stringExpected, stringReceived); // At this point, we're either a string or an Array, // which was converted from an array-like structure. const pass = converted.indexOf(value) != -1; @@ -300,8 +304,8 @@ const matchers: MatchersObject = { isNot: this.isNot, }) + '\n\n' + - `Expected value: ${printExpected(value)}\n` + - `Received ${collectionType}: ${printReceived(collection)}` + `${printLabel(stringExpected)}${printExpected(value)}\n` + + `${printLabel(stringReceived)}${printReceived(collection)}` : () => { const suggestToContainEqual = converted !== null && @@ -316,8 +320,8 @@ const matchers: MatchersObject = { isNot: this.isNot, }) + '\n\n' + - `Expected value: ${printExpected(value)}\n` + - `Received ${collectionType}: ${printReceived(collection)}` + + `${printLabel(stringExpected)}${printExpected(value)}\n` + + `${printLabel(stringReceived)}${printReceived(collection)}` + (suggestToContainEqual ? `\n\n${SUGGEST_TO_CONTAIN_EQUAL}` : '') ); }; @@ -346,6 +350,9 @@ const matchers: MatchersObject = { } const comment = 'deep equality'; + const stringExpected = 'Expected value'; + const stringReceived = `Received ${collectionType}`; + const printLabel = getLabelPrinter(stringExpected, stringReceived); const pass = converted.findIndex(item => equals(item, value, [iterableEquality])) !== -1; @@ -355,8 +362,8 @@ const matchers: MatchersObject = { isNot: this.isNot, }) + '\n\n' + - `Expected value: ${printExpected(value)}\n` + - `Received ${collectionType}: ${printReceived(collection)}`; + `${printLabel(stringExpected)}${printExpected(value)}\n` + + `${printLabel(stringReceived)}${printReceived(collection)}`; return {message, pass}; }, @@ -411,14 +418,22 @@ const matchers: MatchersObject = { } const pass = received.length === length; + const stringExpected = 'Expected length'; + const stringReceivedLength = 'Received length'; + const stringReceivedValue = 'Received value'; + const printLabel = getLabelPrinter( + stringExpected, + stringReceivedLength, + stringReceivedValue, + ); const message = () => matcherHint('.toHaveLength', 'received', 'length', { isNot: this.isNot, }) + '\n\n' + - `Expected length: ${printExpected(length)}\n` + - `Received length: ${printReceived(received.length)}\n` + - `Received value: ${printReceived(received)}`; + `${printLabel(stringExpected)}${printExpected(length)}\n` + + `${printLabel(stringReceivedLength)}${printReceived(received.length)}\n` + + `${printLabel(stringReceivedValue)}${printReceived(received)}`; return {message, pass}; }, diff --git a/packages/jest-matcher-utils/src/__tests__/index.test.js b/packages/jest-matcher-utils/src/__tests__/index.test.js index 95cb4682ff09..c42b15546740 100644 --- a/packages/jest-matcher-utils/src/__tests__/index.test.js +++ b/packages/jest-matcher-utils/src/__tests__/index.test.js @@ -8,7 +8,13 @@ 'use strict'; -const {stringify, ensureNumbers, pluralize, ensureNoExpected} = require('../'); +const { + ensureNumbers, + ensureNoExpected, + getLabelPrinter, + pluralize, + stringify, +} = require('../'); describe('.stringify()', () => { [ @@ -130,3 +136,45 @@ describe('.pluralize()', () => { test('two', () => expect(pluralize('apple', 2)).toEqual('two apples')); test('20', () => expect(pluralize('apple', 20)).toEqual('20 apples')); }); + +describe('getLabelPrinter', () => { + test('0 args', () => { + const printLabel = getLabelPrinter(); + expect(printLabel('')).toBe(': '); + }); + test('1 empty string', () => { + const printLabel = getLabelPrinter(); + expect(printLabel('')).toBe(': '); + }); + test('1 non-empty string', () => { + const string = 'Expected'; + const printLabel = getLabelPrinter(string); + expect(printLabel(string)).toBe('Expected: '); + }); + test('2 equal lengths', () => { + const stringExpected = 'Expected value'; + const collectionType = 'array'; + const stringReceived = `Received ${collectionType}`; + const printLabel = getLabelPrinter(stringExpected, stringReceived); + expect(printLabel(stringExpected)).toBe('Expected value: '); + expect(printLabel(stringReceived)).toBe('Received array: '); + }); + test('2 unequal lengths', () => { + const stringExpected = 'Expected value'; + const collectionType = 'set'; + const stringReceived = `Received ${collectionType}`; + const printLabel = getLabelPrinter(stringExpected, stringReceived); + expect(printLabel(stringExpected)).toBe('Expected value: '); + expect(printLabel(stringReceived)).toBe('Received set: '); + }); + test('throws if inconsistent arg is longer', () => { + const stringConsistent = 'Expected'; + const stringInconsistent = 'Received value'; + const stringInconsistentLonger = 'Received string'; + const printLabel = getLabelPrinter(stringConsistent, stringInconsistent); + expect(printLabel(stringConsistent)).toBe('Expected: '); + expect(() => { + printLabel(stringInconsistentLonger); + }).toThrow(); + }); +}); diff --git a/packages/jest-matcher-utils/src/index.js b/packages/jest-matcher-utils/src/index.js index 232b0e33cba9..82fdb55a02cb 100644 --- a/packages/jest-matcher-utils/src/index.js +++ b/packages/jest-matcher-utils/src/index.js @@ -150,6 +150,21 @@ export const ensureNumbers = ( export const pluralize = (word: string, count: number) => (NUMBERS[count] || count) + ' ' + word + (count === 1 ? '' : 's'); +// To display lines of labeled values as two columns with monospace alignment: +// given the strings which will describe the values, +// return function which given each string, returns the label: +// string, colon, space, and enough padding spaces to align the value. + +type PrintLabel = string => string; + +export const getLabelPrinter = (...strings: Array): PrintLabel => { + const maxLength = strings.reduce( + (max, string) => (string.length > max ? string.length : max), + 0, + ); + return string => `${string}: ${' '.repeat(maxLength - string.length)}`; +}; + export const matcherHint = ( matcherName: string, received: string = 'received', From 2bcac8bcf6bb051a2a3ce9c34479cd0b9ad58fbc Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 15 Oct 2018 12:52:33 -0400 Subject: [PATCH 3/8] Merge and then update CHANGELOG.md --- CHANGELOG.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a14e84672b85..19445e33712a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,13 @@ - `[jest-haste-map]` Add `getFileIterator` to `HasteFS` for faster file iteration ([#7010](https://github.com/facebook/jest/pull/7010)). - `[jest-worker]` [**BREAKING**] Add functionality to call a `setup` method in the worker before the first call and a `teardown` method when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014)). - `[jest-config]` [**BREAKING**] Set default `notifyMode` to `failure-change` ([#7024](https://github.com/facebook/jest/pull/7024)) +- `[jest-config]` Add `readConfigs` function, previously in `jest-cli` ([#7096](https://github.com/facebook/jest/pull/7096)) - `[jest-snapshot]` Enable configurable snapshot paths ([#6143](https://github.com/facebook/jest/pull/6143)) +- `[jest-haste-map]` [**BREAKING**] Remove support for `@providesModule` ([#6104](https://github.com/facebook/jest/pull/6104)) +- `[pretty-format]` Support HTMLCollection and NodeList in DOMCollection plugin ([#7125](https://github.com/facebook/jest/pull/7125)) +- `[jest-runtime]` Pass the normalized configuration to script transformers ([#7148](https://github.com/facebook/jest/pull/7148)) +- `[expect]` Improve report when assertion fails, part 3 ([#7152](https://github.com/facebook/jest/pull/7152)) +- `[jest-runtime]` If `require` fails without a file extension, print all files that match with one ([#7160](https://github.com/facebook/jest/pull/7160)) ### Fixes @@ -31,9 +37,13 @@ - `[jest-jasmine2]` Pending calls inside async tests are reported as pending not failed ([#6782](https://github.com/facebook/jest/pull/6782)) - `[jest-circus]` Better error message when a describe block is empty ([#6372](https://github.com/facebook/jest/pull/6372)) - `[jest-cli]` Fix unhandled error when a bad revision is provided to `changedSince` ([#7115](https://github.com/facebook/jest/pull/7115)) +- `[jest-config]` Moved dynamically assigned `cwd` from `jest-cli` to default configuration in `jest-config` ([#7146](https://github.com/facebook/jest/pull/7146)) +- `[jest-config]` Fix `getMaxWorkers` on termux ([#7154](https://github.com/facebook/jest/pull/7154)) +- `[jest-runtime]` Throw an explicit error if `js` is missing from `moduleFileExtensions` ([#7160](https://github.com/facebook/jest/pull/7160)) ### Chore & Maintenance +- `[docs]` Add synchronous test.each setup ([#7150](https://github.com/facebook/jest/pull/7150)) - `[docs]` Add `this.extend` to the Custom Matchers API reference ([#7130](https://github.com/facebook/jest/pull/7130)) - `[docs]` Fix default value for `coverageReporters` value in configuration docs ([#7126](https://github.com/facebook/jest/pull/7126)) - `[docs]` Add link for jest-extended in expect docs ([#7078](https://github.com/facebook/jest/pull/7078)) @@ -47,6 +57,12 @@ - `[*]` Bump dated dependencies ([#6978](https://github.com/facebook/jest/pull/6978)) - `[scripts]` Don’t make empty subfolders for ignored files in build folder ([#7001](https://github.com/facebook/jest/pull/7001)) - `[docs]` Add missing export statement in `puppeteer_environment.js` under `docs/Puppeteer.md` ([#7127](https://github.com/facebook/jest/pull/7127)) +- `[docs]` Removed useless expect.assertions in `TestingAsyncCode.md` ([#7131](https://github.com/facebook/jest/pull/7131)) +- `[docs]` Remove references to `@providesModule` which isn't supported anymore ([#7147](https://github.com/facebook/jest/pull/7147)) + +### Performance + +- `[jest-mock]` Improve `getType` function performance. ([#7159](https://github.com/facebook/jest/pull/7159)) ## 23.6.0 From 8e11f7ff8465efffa52becf8d07cb7c45c0da9d7 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 15 Oct 2018 14:04:32 -0400 Subject: [PATCH 4/8] Add test for getLabelPrinter --- packages/jest-matcher-utils/src/__tests__/index.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/jest-matcher-utils/src/__tests__/index.test.js b/packages/jest-matcher-utils/src/__tests__/index.test.js index c42b15546740..35b9ca264bac 100644 --- a/packages/jest-matcher-utils/src/__tests__/index.test.js +++ b/packages/jest-matcher-utils/src/__tests__/index.test.js @@ -167,6 +167,14 @@ describe('getLabelPrinter', () => { expect(printLabel(stringExpected)).toBe('Expected value: '); expect(printLabel(stringReceived)).toBe('Received set: '); }); + test('returns incorrect padding if inconsistent arg is shorter', () => { + const stringConsistent = 'Expected'; + const stringInconsistent = 'Received value'; + const stringInconsistentShorter = 'Received set'; + const printLabel = getLabelPrinter(stringConsistent, stringInconsistent); + expect(printLabel(stringConsistent)).toBe('Expected: '); + expect(printLabel(stringInconsistentShorter)).toBe('Received set: '); + }); test('throws if inconsistent arg is longer', () => { const stringConsistent = 'Expected'; const stringInconsistent = 'Received value'; From 1cf474587ffa02f1bdcf4dca31f3af59df86faab Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 15 Oct 2018 14:25:37 -0400 Subject: [PATCH 5/8] Include received type in label for toHaveLength matcher --- .../__snapshots__/matchers.test.js.snap | 20 ++++++++--------- packages/expect/src/matchers.js | 22 ++++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 5515f7697733..1c2b3248a853 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -2683,7 +2683,7 @@ exports[`.toHaveLength {pass: false} expect("").toHaveLength(1) 1`] = ` Expected length: 1 Received length: 0 -Received value: \\"\\"" +Received string: \\"\\"" `; exports[`.toHaveLength {pass: false} expect("abc").toHaveLength(66) 1`] = ` @@ -2691,7 +2691,7 @@ exports[`.toHaveLength {pass: false} expect("abc").toHaveLength(66) 1`] = ` Expected length: 66 Received length: 3 -Received value: \\"abc\\"" +Received string: \\"abc\\"" `; exports[`.toHaveLength {pass: false} expect(["a", "b"]).toHaveLength(99) 1`] = ` @@ -2699,7 +2699,7 @@ exports[`.toHaveLength {pass: false} expect(["a", "b"]).toHaveLength(99) 1`] = ` Expected length: 99 Received length: 2 -Received value: [\\"a\\", \\"b\\"]" +Received array: [\\"a\\", \\"b\\"]" `; exports[`.toHaveLength {pass: false} expect([]).toHaveLength(1) 1`] = ` @@ -2707,7 +2707,7 @@ exports[`.toHaveLength {pass: false} expect([]).toHaveLength(1) 1`] = ` Expected length: 1 Received length: 0 -Received value: []" +Received array: []" `; exports[`.toHaveLength {pass: false} expect([1, 2]).toHaveLength(3) 1`] = ` @@ -2715,7 +2715,7 @@ exports[`.toHaveLength {pass: false} expect([1, 2]).toHaveLength(3) 1`] = ` Expected length: 3 Received length: 2 -Received value: [1, 2]" +Received array: [1, 2]" `; exports[`.toHaveLength {pass: true} expect("").toHaveLength(0) 1`] = ` @@ -2723,7 +2723,7 @@ exports[`.toHaveLength {pass: true} expect("").toHaveLength(0) 1`] = ` Expected length: 0 Received length: 0 -Received value: \\"\\"" +Received string: \\"\\"" `; exports[`.toHaveLength {pass: true} expect("abc").toHaveLength(3) 1`] = ` @@ -2731,7 +2731,7 @@ exports[`.toHaveLength {pass: true} expect("abc").toHaveLength(3) 1`] = ` Expected length: 3 Received length: 3 -Received value: \\"abc\\"" +Received string: \\"abc\\"" `; exports[`.toHaveLength {pass: true} expect(["a", "b"]).toHaveLength(2) 1`] = ` @@ -2739,7 +2739,7 @@ exports[`.toHaveLength {pass: true} expect(["a", "b"]).toHaveLength(2) 1`] = ` Expected length: 2 Received length: 2 -Received value: [\\"a\\", \\"b\\"]" +Received array: [\\"a\\", \\"b\\"]" `; exports[`.toHaveLength {pass: true} expect([]).toHaveLength(0) 1`] = ` @@ -2747,7 +2747,7 @@ exports[`.toHaveLength {pass: true} expect([]).toHaveLength(0) 1`] = ` Expected length: 0 Received length: 0 -Received value: []" +Received array: []" `; exports[`.toHaveLength {pass: true} expect([1, 2]).toHaveLength(2) 1`] = ` @@ -2755,7 +2755,7 @@ exports[`.toHaveLength {pass: true} expect([1, 2]).toHaveLength(2) 1`] = ` Expected length: 2 Received length: 2 -Received value: [1, 2]" +Received array: [1, 2]" `; exports[`.toHaveLength error cases 1`] = ` diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 097078cdc4cb..e54053489d63 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -418,22 +418,24 @@ const matchers: MatchersObject = { } const pass = received.length === length; - const stringExpected = 'Expected length'; - const stringReceivedLength = 'Received length'; - const stringReceivedValue = 'Received value'; - const printLabel = getLabelPrinter( - stringExpected, - stringReceivedLength, - stringReceivedValue, - ); - const message = () => - matcherHint('.toHaveLength', 'received', 'length', { + const message = () => { + const stringExpected = 'Expected length'; + const stringReceivedLength = 'Received length'; + const stringReceivedValue = `Received ${getType(received)}`; + const printLabel = getLabelPrinter( + stringExpected, + 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 {message, pass}; }, From aeb0c5d3b5900eb996eaae53d60d957a5e012711 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 15 Oct 2018 14:39:00 -0400 Subject: [PATCH 6/8] Fix pretty lint error --- packages/expect/src/matchers.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index e54053489d63..4a0b9a8c242c 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -428,13 +428,17 @@ const matchers: MatchersObject = { 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 ( + 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 {message, pass}; From 22f02634452dec22cf46d9773cf3d39b1866aff1 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 15 Oct 2018 15:14:19 -0400 Subject: [PATCH 7/8] Refactor message function in toContain and toContainEqual --- packages/expect/src/matchers.js | 82 +++++++++++++++------------------ 1 file changed, 38 insertions(+), 44 deletions(-) diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index 4a0b9a8c242c..c63529cfda84 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -290,41 +290,32 @@ const matchers: MatchersObject = { ); } } - const comment = 'indexOf'; - const stringExpected = 'Expected value'; - const stringReceived = `Received ${collectionType}`; - const printLabel = getLabelPrinter(stringExpected, stringReceived); // At this point, we're either a string or an Array, // which was converted from an array-like structure. const pass = converted.indexOf(value) != -1; - const message = pass - ? () => - matcherHint('.toContain', collectionType, 'value', { - comment, - isNot: this.isNot, - }) + - '\n\n' + - `${printLabel(stringExpected)}${printExpected(value)}\n` + - `${printLabel(stringReceived)}${printReceived(collection)}` - : () => { - const suggestToContainEqual = - converted !== null && - typeof converted !== 'string' && - converted instanceof Array && - converted.findIndex(item => - equals(item, value, [iterableEquality]), - ) !== -1; - return ( - matcherHint('.toContain', collectionType, 'value', { - comment, - isNot: this.isNot, - }) + - '\n\n' + - `${printLabel(stringExpected)}${printExpected(value)}\n` + - `${printLabel(stringReceived)}${printReceived(collection)}` + - (suggestToContainEqual ? `\n\n${SUGGEST_TO_CONTAIN_EQUAL}` : '') - ); - }; + const message = () => { + const stringExpected = 'Expected value'; + const stringReceived = `Received ${collectionType}`; + const printLabel = getLabelPrinter(stringExpected, stringReceived); + const suggestToContainEqual = + !pass && + converted !== null && + typeof converted !== 'string' && + converted instanceof Array && + converted.findIndex(item => equals(item, value, [iterableEquality])) !== + -1; + + return ( + matcherHint('.toContain', collectionType, 'value', { + comment: 'indexOf', + isNot: this.isNot, + }) + + '\n\n' + + `${printLabel(stringExpected)}${printExpected(value)}\n` + + `${printLabel(stringReceived)}${printReceived(collection)}` + + (suggestToContainEqual ? `\n\n${SUGGEST_TO_CONTAIN_EQUAL}` : '') + ); + }; return {message, pass}; }, @@ -349,21 +340,24 @@ const matchers: MatchersObject = { } } - const comment = 'deep equality'; - const stringExpected = 'Expected value'; - const stringReceived = `Received ${collectionType}`; - const printLabel = getLabelPrinter(stringExpected, stringReceived); const pass = converted.findIndex(item => equals(item, value, [iterableEquality])) !== -1; - const message = () => - matcherHint('.toContainEqual', collectionType, 'value', { - comment, - isNot: this.isNot, - }) + - '\n\n' + - `${printLabel(stringExpected)}${printExpected(value)}\n` + - `${printLabel(stringReceived)}${printReceived(collection)}`; + const message = () => { + const stringExpected = 'Expected value'; + const stringReceived = `Received ${collectionType}`; + const printLabel = getLabelPrinter(stringExpected, stringReceived); + + return ( + matcherHint('.toContainEqual', collectionType, 'value', { + comment: 'deep equality', + isNot: this.isNot, + }) + + '\n\n' + + `${printLabel(stringExpected)}${printExpected(value)}\n` + + `${printLabel(stringReceived)}${printReceived(collection)}` + ); + }; return {message, pass}; }, From 29737548cdaf02ae198be1ab5725117456f13f3b Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 15 Oct 2018 17:23:40 -0400 Subject: [PATCH 8/8] Align values for toBeCloseTo --- .../__snapshots__/matchers.test.js.snap | 48 +++++++++---------- packages/expect/src/matchers.js | 4 +- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 1c2b3248a853..7741bb1f6dd1 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -372,96 +372,96 @@ exports[`.toBeCloseTo() {pass: true} expect(0)toBeCloseTo( 0) 1`] = ` "expect(received).not.toBeCloseTo(expected) Precision: 2-digit -Expected: 0 -Received: 0" +Expected: 0 +Received: 0" `; exports[`.toBeCloseTo() {pass: true} expect(0)toBeCloseTo( 0.001) 1`] = ` "expect(received).not.toBeCloseTo(expected) Precision: 2-digit -Expected: 0.001 -Received: 0" +Expected: 0.001 +Received: 0" `; exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.225) 1`] = ` "expect(received).not.toBeCloseTo(expected) Precision: 2-digit -Expected: 1.225 -Received: 1.23" +Expected: 1.225 +Received: 1.23" `; exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.226) 1`] = ` "expect(received).not.toBeCloseTo(expected) Precision: 2-digit -Expected: 1.226 -Received: 1.23" +Expected: 1.226 +Received: 1.23" `; exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.229) 1`] = ` "expect(received).not.toBeCloseTo(expected) Precision: 2-digit -Expected: 1.229 -Received: 1.23" +Expected: 1.229 +Received: 1.23" `; exports[`.toBeCloseTo() {pass: true} expect(1.23)toBeCloseTo( 1.234) 1`] = ` "expect(received).not.toBeCloseTo(expected) Precision: 2-digit -Expected: 1.234 -Received: 1.23" +Expected: 1.234 +Received: 1.23" `; exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.000004, 5] 1`] = ` "expect(received).not.toBeCloseTo(expected, precision) Precision: 5-digit -Expected: 0.000004 -Received: 0" +Expected: 0.000004 +Received: 0" `; exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.0001, 3] 1`] = ` "expect(received).not.toBeCloseTo(expected, precision) Precision: 3-digit -Expected: 0.0001 -Received: 0" +Expected: 0.0001 +Received: 0" `; exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.1, 0] 1`] = ` "expect(received).not.toBeCloseTo(expected, precision) Precision: 0-digit -Expected: 0.1 -Received: 0" +Expected: 0.1 +Received: 0" `; exports[`.toBeCloseTo() throws: [0, 0.01] 1`] = ` "expect(received).toBeCloseTo(expected) Precision: 2-digit -Expected: 0.01 -Received: 0" +Expected: 0.01 +Received: 0" `; exports[`.toBeCloseTo() throws: [1, 1.23] 1`] = ` "expect(received).toBeCloseTo(expected) Precision: 2-digit -Expected: 1.23 -Received: 1" +Expected: 1.23 +Received: 1" `; exports[`.toBeCloseTo() throws: [1.23, 1.2249999] 1`] = ` "expect(received).toBeCloseTo(expected) Precision: 2-digit -Expected: 1.2249999 -Received: 1.23" +Expected: 1.2249999 +Received: 1.23" `; exports[`.toBeDefined(), .toBeUndefined() '"a"' is defined 1`] = ` diff --git a/packages/expect/src/matchers.js b/packages/expect/src/matchers.js index c63529cfda84..18910ee06eaf 100644 --- a/packages/expect/src/matchers.js +++ b/packages/expect/src/matchers.js @@ -94,8 +94,8 @@ const matchers: MatchersObject = { }) + '\n\n' + `Precision: ${printExpected(precision)}-digit\n` + - `Expected: ${printExpected(expected)}\n` + - `Received: ${printReceived(actual)}`; + `Expected: ${printExpected(expected)}\n` + + `Received: ${printReceived(actual)}`; return {message, pass}; },