From 00d03ce4e0c0e5c7098fa98c0a78f4486c097f19 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 29 May 2018 22:03:26 +0100 Subject: [PATCH 1/3] Fix each array title concatenation --- e2e/__tests__/__snapshots__/each.test.js.snap | 2 +- .../jest-each/src/__tests__/array.test.js | 59 +++++++++++++++++-- packages/jest-each/src/bind.js | 8 ++- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/e2e/__tests__/__snapshots__/each.test.js.snap b/e2e/__tests__/__snapshots__/each.test.js.snap index 6a074807acbe..342597f90523 100644 --- a/e2e/__tests__/__snapshots__/each.test.js.snap +++ b/e2e/__tests__/__snapshots__/each.test.js.snap @@ -29,7 +29,7 @@ exports[`shows error message when not enough arguments are supplied to tests 1`] Missing 1 arguments - at packages/jest-each/build/bind.js:81:17 + at packages/jest-each/build/bind.js:82:17 " `; diff --git a/packages/jest-each/src/__tests__/array.test.js b/packages/jest-each/src/__tests__/array.test.js index a4a04cd0e4d7..3ecea027e5db 100644 --- a/packages/jest-each/src/__tests__/array.test.js +++ b/packages/jest-each/src/__tests__/array.test.js @@ -72,23 +72,70 @@ describe('jest-each', () => { ); }); - test('calls global with title containing param values when using sprintf format', () => { + test('calls global with title containing param values when using printf format', () => { const globalTestMocks = getGlobalTestMocks(); const eachObject = each.withGlobal(globalTestMocks)([ - ['hello', 1], - ['world', 2], + [ + 'hello', + 1, + null, + undefined, + 1.2, + {foo: 'bar'}, + () => {}, + [], + Infinity, + NaN, + ], + [ + 'world', + 1, + null, + undefined, + 1.2, + {baz: 'qux'}, + () => {}, + [], + Infinity, + NaN, + ], ]); const testFunction = get(eachObject, keyPath); - testFunction('expected string: %s %s', noop); + testFunction('expected string: %s %i %o %o %f %j %O %j %d %d', noop); const globalMock = get(globalTestMocks, keyPath); expect(globalMock).toHaveBeenCalledTimes(2); expect(globalMock).toHaveBeenCalledWith( - 'expected string: hello 1', + `expected string: hello 1 null undefined 1.2 ${JSON.stringify({ + foo: 'bar', + })} [Function] [] Infinity NaN`, expectFunction, ); expect(globalMock).toHaveBeenCalledWith( - 'expected string: world 2', + `expected string: world 1 null undefined 1.2 ${JSON.stringify({ + baz: 'qux', + })} [Function] [] Infinity NaN`, + expectFunction, + ); + }); + + test('does not call global test with title containing more param values than sprintf placeholders', () => { + const globalTestMocks = getGlobalTestMocks(); + const eachObject = each.withGlobal(globalTestMocks)([ + ['hello', 1, 2, 3, 4, 5], + ['world', 1, 2, 3, 4, 5], + ]); + const testFunction = get(eachObject, keyPath); + testFunction('expected string: %s', noop); + + const globalMock = get(globalTestMocks, keyPath); + expect(globalMock).toHaveBeenCalledTimes(2); + expect(globalMock).toHaveBeenCalledWith( + 'expected string: hello', + expectFunction, + ); + expect(globalMock).toHaveBeenCalledWith( + 'expected string: world', expectFunction, ); }); diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index 0df9276d660d..751c250a58a4 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -15,6 +15,7 @@ type Table = Array>; const EXPECTED_COLOR = chalk.green; const RECEIVED_COLOR = chalk.red; +const SUPPORTED_PLACEHOLDERS = /%[sdifjoO%]/g; export default (cb: Function) => (...args: any) => ( title: string, @@ -23,7 +24,7 @@ export default (cb: Function) => (...args: any) => ( if (args.length === 1) { const table: Table = args[0]; return table.forEach(row => - cb(util.format(title, ...row), applyRestParams(row, test)), + cb(arrayFormat(title, ...row), applyRestParams(row, test)), ); } @@ -52,6 +53,11 @@ export default (cb: Function) => (...args: any) => ( ); }; +const arrayFormat = (str, ...args) => { + const matches = (str.match(SUPPORTED_PLACEHOLDERS) || []).length; + return util.format(str, ...args.slice(0, matches)); +}; + const applyRestParams = (params: Array, test: Function) => { if (params.length < test.length) return done => test(...params, done); From 6836ad4a6276cd34cd06727bf6923d35d3036fcb Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 29 May 2018 22:07:14 +0100 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4ed1c2d8212..d79c945dab59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Fixes +* `[jest-each]` Stop test title concatenating extra args ([##6346](https://github.com/facebook/jest/pull/#6346)) * `[expect]` toMatchObject throws TypeError when a source property is null ([#6313](https://github.com/facebook/jest/pull/6313)) * `[jest-cli]` Normalize slashes in paths in CLI output on Windows ([#6310](https://github.com/facebook/jest/pull/6310)) From dd3f9fcc8fc5f7fd86067592905a3e463c95a477 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 29 May 2018 22:51:34 +0100 Subject: [PATCH 3/3] Fix node 6 placeholders --- packages/jest-each/src/__tests__/array.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-each/src/__tests__/array.test.js b/packages/jest-each/src/__tests__/array.test.js index 3ecea027e5db..e7adb255abe7 100644 --- a/packages/jest-each/src/__tests__/array.test.js +++ b/packages/jest-each/src/__tests__/array.test.js @@ -101,20 +101,20 @@ describe('jest-each', () => { ], ]); const testFunction = get(eachObject, keyPath); - testFunction('expected string: %s %i %o %o %f %j %O %j %d %d', noop); + testFunction('expected string: %s %d %s %s %d %j %s %j %d %d', noop); const globalMock = get(globalTestMocks, keyPath); expect(globalMock).toHaveBeenCalledTimes(2); expect(globalMock).toHaveBeenCalledWith( `expected string: hello 1 null undefined 1.2 ${JSON.stringify({ foo: 'bar', - })} [Function] [] Infinity NaN`, + })} () => {} [] Infinity NaN`, expectFunction, ); expect(globalMock).toHaveBeenCalledWith( `expected string: world 1 null undefined 1.2 ${JSON.stringify({ baz: 'qux', - })} [Function] [] Infinity NaN`, + })} () => {} [] Infinity NaN`, expectFunction, ); });