From 22a520dc05f07e0b37c1415b0c2e4041f054192d Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 24 Jan 2019 11:28:24 +0000 Subject: [PATCH 1/8] Add jest-get-type dependency --- packages/jest-each/package.json | 1 + yarn.lock | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/packages/jest-each/package.json b/packages/jest-each/package.json index dfae0c58209a..9a430c533582 100644 --- a/packages/jest-each/package.json +++ b/packages/jest-each/package.json @@ -18,6 +18,7 @@ "license": "MIT", "dependencies": { "chalk": "^2.0.1", + "jest-get-type": "^22.4.3", "jest-util": "^23.4.0", "pretty-format": "^23.6.0" }, diff --git a/yarn.lock b/yarn.lock index ba42a8185c44..b6f64b6c9dcb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7177,6 +7177,11 @@ jest-docblock@^21.0.0: resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-21.2.0.tgz#51529c3b30d5fd159da60c27ceedc195faf8d414" integrity sha512-5IZ7sY9dBAYSV+YjQ0Ovb540Ku7AO9Z5o2Cg789xj167iQuZ2cG+z0f3Uct6WeYLbU6aQiM2pCs7sZ+4dotydw== +jest-get-type@^22.4.3: + version "22.4.3" + resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-22.4.3.tgz#e3a8504d8479342dd4420236b322869f18900ce4" + integrity sha512-/jsz0Y+V29w1chdXVygEKSz2nBoHoYqNShPe+QgxSNjAuP1i8+k4LbQNrfoliKej0P45sivkSCh7yiD6ubHS3w== + jest-haste-map@23.5.0: version "23.5.0" resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-23.5.0.tgz#d4ca618188bd38caa6cb20349ce6610e194a8065" From 6dd783cee04e3d1e5dcdca90b13e52248be4c2bd Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 24 Jan 2019 11:28:46 +0000 Subject: [PATCH 2/8] Add primitive check to pretty printing --- .../jest-each/src/__tests__/template.test.js | 28 ++++++++++++++++++- packages/jest-each/src/bind.js | 7 +++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/jest-each/src/__tests__/template.test.js b/packages/jest-each/src/__tests__/template.test.js index 7dfe4fbd274a..8d0ee6042735 100644 --- a/packages/jest-each/src/__tests__/template.test.js +++ b/packages/jest-each/src/__tests__/template.test.js @@ -220,7 +220,7 @@ describe('jest-each', () => { const globalMock = get(globalTestMocks, keyPath); expect(globalMock).toHaveBeenCalledTimes(1); expect(globalMock).toHaveBeenCalledWith( - 'interpolates object keyPath to value: "baz"', + 'interpolates object keyPath to value: baz', expectFunction, undefined, ); @@ -282,6 +282,32 @@ describe('jest-each', () => { 10000, ); }); + + test('formats primitive values using .toString()', () => { + const globalTestMocks = getGlobalTestMocks(); + const number = 1; + const string = 'hello'; + const boolean = true; + const symbol = Symbol('world'); + const nullValue = null; + const undefinedValue = undefined; + const eachObject = each.withGlobal(globalTestMocks)` + number | string | boolean | symbol | nullValue | undefinedValue + ${number} | ${string} | ${boolean} | ${symbol} | ${nullValue} | ${undefinedValue} + `; + + const testFunction = get(eachObject, keyPath); + testFunction( + 'number: $number | string: $string | boolean: $boolean | symbol: $symbol | null: $nullValue | undefined: $undefinedValue', + noop, + ); + const globalMock = get(globalTestMocks, keyPath); + expect(globalMock).toHaveBeenCalledWith( + 'number: 1 | string: hello | boolean: true | symbol: Symbol(world) | null: null | undefined: undefined', + expect.any(Function), + undefined, + ); + }); }); }); diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index 76e646a0cad3..68f06d7fc5cb 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -10,6 +10,7 @@ import util from 'util'; import chalk from 'chalk'; import pretty from 'pretty-format'; +import getType from 'jest-get-type'; import {ErrorWithStack} from 'jest-util'; type Table = Array>; @@ -23,6 +24,7 @@ const RECEIVED_COLOR = chalk.red; const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g; const PRETTY_PLACEHOLDER = '%p'; const INDEX_PLACEHOLDER = '%#'; +const PRIMITIVES = ['string', 'number', 'boolean', 'null', 'undefined']; export default (cb: Function, supportsDone: boolean = true) => (...args: any) => function eachBind(title: string, test: Function, timeout: number): void { @@ -195,6 +197,11 @@ const getMatchingKeyPaths = title => (matches, key) => const replaceKeyPathWithValue = data => (title, match) => { const keyPath = match.replace('$', '').split('.'); const value = getPath(data, keyPath); + const valueType = getType(value); + + if (PRIMITIVES.includes(valueType)) { + return title.replace(match, value); + } return title.replace(match, pretty(value, {maxDepth: 1, min: true})); }; From e7a5186de8f8e115996882ef0621954e81890c23 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 24 Jan 2019 11:32:58 +0000 Subject: [PATCH 3/8] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cd60cd8b786..a65d8df207bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[jest-each]` [**BREAKING**] Add primitive pretty printing for interpolated titles ([#7694](https://github.com/facebook/jest/pull/7694)) - `[jest-runtime]` Add `jest.isolateModules` for scoped module initialization ([#6701](https://github.com/facebook/jest/pull/6701)) - `[jest-diff]` [**BREAKING**] Support diffing numbers and booleans instead of returning null for different ones ([#7605](https://github.com/facebook/jest/pull/7605)) - `[jest-diff]` [**BREAKING**] Replace `diff` with `diff-sequences` package ([#6961](https://github.com/facebook/jest/pull/6961)) From c102961052ebe0a771f0d55c09f1816871756b45 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 24 Jan 2019 11:43:54 +0000 Subject: [PATCH 4/8] Update missing snapshot --- e2e/__tests__/__snapshots__/each.test.js.snap | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/e2e/__tests__/__snapshots__/each.test.js.snap b/e2e/__tests__/__snapshots__/each.test.js.snap index 75220531c419..671ce85b22c5 100644 --- a/e2e/__tests__/__snapshots__/each.test.js.snap +++ b/e2e/__tests__/__snapshots__/each.test.js.snap @@ -17,7 +17,7 @@ PASS __tests__/pretty.test.js ✓ -Infinity == -Infinity ✓ NaN == NaN template - ✓ "hello" == "hello" + ✓ hello == hello ✓ 1 == 1 ✓ null == null ✓ undefined == undefined @@ -127,9 +127,9 @@ FAIL __tests__/failure.test.js ✕ The word red contains the letter 'z' ✕ The word green contains the letter 'z' ✕ The word bean contains the letter 'z' - template table describe fails on all rows expected "a" == "b" + template table describe fails on all rows expected a == b ✕ fails - template table describe fails on all rows expected "c" == "d" + template table describe fails on all rows expected c == d ✕ fails array table describe fails on all rows expected a == b ✕ fails @@ -289,7 +289,7 @@ FAIL __tests__/failure.test.js at toBe (__tests__/failure.test.js:47:28) - ● template table describe fails on all rows expected "a" == "b" › fails + ● template table describe fails on all rows expected a == b › fails expect(received).toBe(expected) // Object.is equality @@ -306,7 +306,7 @@ FAIL __tests__/failure.test.js at Object.toBe (__tests__/failure.test.js:59:20) - ● template table describe fails on all rows expected "c" == "d" › fails + ● template table describe fails on all rows expected c == d › fails expect(received).toBe(expected) // Object.is equality From e1d074660b73ee2e22398c03a110c6b89b9f7b4b Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 24 Jan 2019 12:05:12 +0000 Subject: [PATCH 5/8] Update primitives constant to a Set --- packages/jest-each/src/bind.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index 68f06d7fc5cb..b09a0dff20f3 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -24,7 +24,13 @@ const RECEIVED_COLOR = chalk.red; const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g; const PRETTY_PLACEHOLDER = '%p'; const INDEX_PLACEHOLDER = '%#'; -const PRIMITIVES = ['string', 'number', 'boolean', 'null', 'undefined']; +const PRIMITIVES = new Set([ + 'string', + 'number', + 'boolean', + 'null', + 'undefined', +]); export default (cb: Function, supportsDone: boolean = true) => (...args: any) => function eachBind(title: string, test: Function, timeout: number): void { @@ -199,7 +205,7 @@ const replaceKeyPathWithValue = data => (title, match) => { const value = getPath(data, keyPath); const valueType = getType(value); - if (PRIMITIVES.includes(valueType)) { + if (PRIMITIVES.has(valueType)) { return title.replace(match, value); } return title.replace(match, pretty(value, {maxDepth: 1, min: true})); From 5b05da855633b418ab01bbf44ab55190c2a280ac Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 24 Jan 2019 12:05:36 +0000 Subject: [PATCH 6/8] Add quotes in to snapshot to make failure clearer --- e2e/__tests__/__snapshots__/each.test.js.snap | 16 ++++++++-------- e2e/each/__tests__/failure.test.js | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/e2e/__tests__/__snapshots__/each.test.js.snap b/e2e/__tests__/__snapshots__/each.test.js.snap index 671ce85b22c5..9e6dc51f03aa 100644 --- a/e2e/__tests__/__snapshots__/each.test.js.snap +++ b/e2e/__tests__/__snapshots__/each.test.js.snap @@ -127,9 +127,9 @@ FAIL __tests__/failure.test.js ✕ The word red contains the letter 'z' ✕ The word green contains the letter 'z' ✕ The word bean contains the letter 'z' - template table describe fails on all rows expected a == b + template table describe fails on all rows expected "a" == "b" ✕ fails - template table describe fails on all rows expected c == d + template table describe fails on all rows expected "c" == "d" ✕ fails array table describe fails on all rows expected a == b ✕ fails @@ -289,12 +289,12 @@ FAIL __tests__/failure.test.js at toBe (__tests__/failure.test.js:47:28) - ● template table describe fails on all rows expected a == b › fails + ● template table describe fails on all rows expected "a" == "b" › fails expect(received).toBe(expected) // Object.is equality - Expected: "b" - Received: "a" + Expected: "\\"b\\"" + Received: "\\"a\\"" 57 | ({left, right}) => { 58 | it('fails ', () => { @@ -306,12 +306,12 @@ FAIL __tests__/failure.test.js at Object.toBe (__tests__/failure.test.js:59:20) - ● template table describe fails on all rows expected c == d › fails + ● template table describe fails on all rows expected "c" == "d" › fails expect(received).toBe(expected) // Object.is equality - Expected: "d" - Received: "c" + Expected: "\\"d\\"" + Received: "\\"c\\"" 57 | ({left, right}) => { 58 | it('fails ', () => { diff --git a/e2e/each/__tests__/failure.test.js b/e2e/each/__tests__/failure.test.js index 23ef09020f6b..9c84f112e50d 100644 --- a/e2e/each/__tests__/failure.test.js +++ b/e2e/each/__tests__/failure.test.js @@ -49,9 +49,9 @@ test.each(['red', 'green', 'bean'])( ); describe.each` - left | right - ${'a'} | ${'b'} - ${'c'} | ${'d'} + left | right + ${'"a"'} | ${'"b"'} + ${'"c"'} | ${'"d"'} `( 'template table describe fails on all rows expected $left == $right', ({left, right}) => { From 4ad8e3533d6895d4181e35fab97d455f5f354121 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 24 Jan 2019 14:08:29 +0000 Subject: [PATCH 7/8] Update jest-get-type version --- packages/jest-each/package.json | 2 +- yarn.lock | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/jest-each/package.json b/packages/jest-each/package.json index 9a430c533582..556924cc28b0 100644 --- a/packages/jest-each/package.json +++ b/packages/jest-each/package.json @@ -18,7 +18,7 @@ "license": "MIT", "dependencies": { "chalk": "^2.0.1", - "jest-get-type": "^22.4.3", + "jest-get-type": "^22.1.0", "jest-util": "^23.4.0", "pretty-format": "^23.6.0" }, diff --git a/yarn.lock b/yarn.lock index b6f64b6c9dcb..ba42a8185c44 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7177,11 +7177,6 @@ jest-docblock@^21.0.0: resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-21.2.0.tgz#51529c3b30d5fd159da60c27ceedc195faf8d414" integrity sha512-5IZ7sY9dBAYSV+YjQ0Ovb540Ku7AO9Z5o2Cg789xj167iQuZ2cG+z0f3Uct6WeYLbU6aQiM2pCs7sZ+4dotydw== -jest-get-type@^22.4.3: - version "22.4.3" - resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-22.4.3.tgz#e3a8504d8479342dd4420236b322869f18900ce4" - integrity sha512-/jsz0Y+V29w1chdXVygEKSz2nBoHoYqNShPe+QgxSNjAuP1i8+k4LbQNrfoliKej0P45sivkSCh7yiD6ubHS3w== - jest-haste-map@23.5.0: version "23.5.0" resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-23.5.0.tgz#d4ca618188bd38caa6cb20349ce6610e194a8065" From 9b140dccb71287ec4ea9d5bf2f962c7ffdf574f4 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Fri, 25 Jan 2019 12:44:46 +0000 Subject: [PATCH 8/8] Move string quotes to test title --- e2e/__tests__/__snapshots__/each.test.js.snap | 8 ++++---- e2e/each/__tests__/failure.test.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/e2e/__tests__/__snapshots__/each.test.js.snap b/e2e/__tests__/__snapshots__/each.test.js.snap index 9e6dc51f03aa..20e373ef77d5 100644 --- a/e2e/__tests__/__snapshots__/each.test.js.snap +++ b/e2e/__tests__/__snapshots__/each.test.js.snap @@ -293,8 +293,8 @@ FAIL __tests__/failure.test.js expect(received).toBe(expected) // Object.is equality - Expected: "\\"b\\"" - Received: "\\"a\\"" + Expected: "b" + Received: "a" 57 | ({left, right}) => { 58 | it('fails ', () => { @@ -310,8 +310,8 @@ FAIL __tests__/failure.test.js expect(received).toBe(expected) // Object.is equality - Expected: "\\"d\\"" - Received: "\\"c\\"" + Expected: "d" + Received: "c" 57 | ({left, right}) => { 58 | it('fails ', () => { diff --git a/e2e/each/__tests__/failure.test.js b/e2e/each/__tests__/failure.test.js index 9c84f112e50d..abc9acfda1dd 100644 --- a/e2e/each/__tests__/failure.test.js +++ b/e2e/each/__tests__/failure.test.js @@ -49,11 +49,11 @@ test.each(['red', 'green', 'bean'])( ); describe.each` - left | right - ${'"a"'} | ${'"b"'} - ${'"c"'} | ${'"d"'} + left | right + ${'a'} | ${'b'} + ${'c'} | ${'d'} `( - 'template table describe fails on all rows expected $left == $right', + 'template table describe fails on all rows expected "$left" == "$right"', ({left, right}) => { it('fails ', () => { expect(left).toBe(right);