From 9362bccaf7e895cd6e3b3b25bbd7d85588fd8560 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Thu, 7 Jun 2018 11:16:19 -0700 Subject: [PATCH 1/6] Allow test titles to include array index --- packages/jest-each/src/__tests__/array.test.js | 6 +++--- packages/jest-each/src/bind.js | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/jest-each/src/__tests__/array.test.js b/packages/jest-each/src/__tests__/array.test.js index 6b8fb1ef1adc..3cfc96fd5d05 100644 --- a/packages/jest-each/src/__tests__/array.test.js +++ b/packages/jest-each/src/__tests__/array.test.js @@ -102,20 +102,20 @@ describe('jest-each', () => { ], ]); const testFunction = get(eachObject, keyPath); - testFunction('expected string: %s %d %s %s %d %j %s %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', - })} () => {} [] Infinity NaN`, + })} () => {} [] Infinity NaN 0`, expectFunction, ); expect(globalMock).toHaveBeenCalledWith( `expected string: world 1 null undefined 1.2 ${JSON.stringify({ baz: 'qux', - })} () => {} [] Infinity NaN`, + })} () => {} [] Infinity NaN 0`, expectFunction, ); }); diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index f2abdcba0872..ae995e86d222 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -21,6 +21,7 @@ const EXPECTED_COLOR = chalk.green; const RECEIVED_COLOR = chalk.red; const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g; const PRETTY_PLACEHOLDER = '%p'; +const INDEX_PLACEHOLDER = '%_'; export default (cb: Function) => (...args: any) => function eachBind(title: string, test: Function): void { @@ -85,16 +86,15 @@ const arrayFormat = (title, ...args) => { if (prettyIndexes.indexOf(index) !== -1) { return { args: acc.args, - title: acc.title.replace( - PRETTY_PLACEHOLDER, - pretty(arg, {maxDepth: 1, min: true}), - ), + title: acc.title + .replace(PRETTY_PLACEHOLDER, pretty(arg, {maxDepth: 1, min: true})) + .replace(INDEX_PLACEHOLDER, `${index}`), }; } return { args: acc.args.concat([arg]), - title: acc.title, + title: acc.title.replace(INDEX_PLACEHOLDER, `${index}`), }; }, {args: [], title}, From 8fc434a18f7f22031485e1f504b1993c444bf70d Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Thu, 7 Jun 2018 20:59:50 -0700 Subject: [PATCH 2/6] Address comments, %_ -> %# --- packages/jest-each/src/__tests__/array.test.js | 4 ++-- packages/jest-each/src/bind.js | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/jest-each/src/__tests__/array.test.js b/packages/jest-each/src/__tests__/array.test.js index 3cfc96fd5d05..2be82eef691a 100644 --- a/packages/jest-each/src/__tests__/array.test.js +++ b/packages/jest-each/src/__tests__/array.test.js @@ -102,7 +102,7 @@ describe('jest-each', () => { ], ]); const testFunction = get(eachObject, keyPath); - testFunction('expected string: %s %d %s %s %d %j %s %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); @@ -115,7 +115,7 @@ describe('jest-each', () => { expect(globalMock).toHaveBeenCalledWith( `expected string: world 1 null undefined 1.2 ${JSON.stringify({ baz: 'qux', - })} () => {} [] Infinity NaN 0`, + })} () => {} [] Infinity NaN 1`, expectFunction, ); }); diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index ae995e86d222..edb6f290c1e5 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -21,7 +21,7 @@ const EXPECTED_COLOR = chalk.green; const RECEIVED_COLOR = chalk.red; const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g; const PRETTY_PLACEHOLDER = '%p'; -const INDEX_PLACEHOLDER = '%_'; +const INDEX_PLACEHOLDER = '%#'; export default (cb: Function) => (...args: any) => function eachBind(title: string, test: Function): void { @@ -29,8 +29,8 @@ export default (cb: Function) => (...args: any) => const table: Table = args[0].every(Array.isArray) ? args[0] : args[0].map(entry => [entry]); - return table.forEach(row => - cb(arrayFormat(title, ...row), applyRestParams(row, test)), + return table.forEach((row, i) => + cb(arrayFormat(title, i, ...row), applyRestParams(row, test)), ); } @@ -77,7 +77,7 @@ const getPrettyIndexes = placeholders => [], ); -const arrayFormat = (title, ...args) => { +const arrayFormat = (title, rowIndex, ...args) => { const placeholders = title.match(SUPPORTED_PLACEHOLDERS) || []; const prettyIndexes = getPrettyIndexes(placeholders); @@ -88,13 +88,13 @@ const arrayFormat = (title, ...args) => { args: acc.args, title: acc.title .replace(PRETTY_PLACEHOLDER, pretty(arg, {maxDepth: 1, min: true})) - .replace(INDEX_PLACEHOLDER, `${index}`), + .replace(INDEX_PLACEHOLDER, `${rowIndex}`), }; } return { args: acc.args.concat([arg]), - title: acc.title.replace(INDEX_PLACEHOLDER, `${index}`), + title: acc.title.replace(INDEX_PLACEHOLDER, `${rowIndex}`), }; }, {args: [], title}, From b8f662ed4476e67ce1ec73eeaa629615350b6a1a Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Fri, 8 Jun 2018 09:58:02 -0700 Subject: [PATCH 3/6] Fix docs linting --- website/versioned_docs/version-23.0/WatchPlugins.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/versioned_docs/version-23.0/WatchPlugins.md b/website/versioned_docs/version-23.0/WatchPlugins.md index 4f992a170bc1..c2bad1742624 100644 --- a/website/versioned_docs/version-23.0/WatchPlugins.md +++ b/website/versioned_docs/version-23.0/WatchPlugins.md @@ -24,7 +24,7 @@ class MyWatchPlugin { ## Hooking into Jest To connect your watch plugin to Jest, add its path under `watchPlugins` in your Jest configuration: - + ```javascript // jest.config.js module.exports = { From a6f96bd23c5e46e0e97a5b22aaece8a2f118d3bf Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Mon, 11 Jun 2018 09:28:37 -0700 Subject: [PATCH 4/6] Add %# to docs --- docs/GlobalAPI.md | 2 ++ packages/jest-each/README.md | 3 +++ 2 files changed, 5 insertions(+) diff --git a/docs/GlobalAPI.md b/docs/GlobalAPI.md index 049cf3ec692f..f0ec591a6011 100644 --- a/docs/GlobalAPI.md +++ b/docs/GlobalAPI.md @@ -240,6 +240,7 @@ Use `describe.each` if you keep duplicating the same test suites with different - `%f` - Floating point value. - `%j` - JSON. - `%o` - Object. + - `%#` - Index of the test case. - `%%` - single percent sign ('%'). This does not consume an argument. - `fn`: `Function` the suite of tests to be ran, this is the function that will receive the parameters in each row as function arguments. @@ -485,6 +486,7 @@ Use `test.each` if you keep duplicating the same test with different data. `test - `%f` - Floating point value. - `%j` - JSON. - `%o` - Object. + - `%#` - Index of the test case. - `%%` - single percent sign ('%'). This does not consume an argument. - `fn`: `Function` the test to be ran, this is the function that will receive the parameters in each row as function arguments. diff --git a/packages/jest-each/README.md b/packages/jest-each/README.md index 08b2994837f5..79dcb3be1707 100644 --- a/packages/jest-each/README.md +++ b/packages/jest-each/README.md @@ -33,6 +33,7 @@ jest-each allows you to provide multiple arguments to your `test`/`describe` whi - `%f` - Floating point value. - `%j` - JSON. - `%o` - Object. + - `%#` - Index of the test case. - `%%` - single percent sign ('%'). This does not consume an argument. - 🖖 Spock like data tables with [Tagged Template Literals](#tagged-template-literal-of-rows) @@ -108,6 +109,7 @@ const each = require('jest-each'); - `%f` - Floating point value. - `%j` - JSON. - `%o` - Object. + - `%#` - Index of the test case. - `%%` - single percent sign ('%'). This does not consume an argument. - testFn: `Function` the test logic, this is the function that will receive the parameters of each row as function arguments @@ -128,6 +130,7 @@ const each = require('jest-each'); - `%f` - Floating point value. - `%j` - JSON. - `%o` - Object. + - `%#` - Index of the test case. - `%%` - single percent sign ('%'). This does not consume an argument. - suiteFn: `Function` the suite of `test`/`it`s to be ran, this is the function that will receive the parameters in each row as function arguments From 5ea49fa3232e262973aeefeba464edf8304bf2d8 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Sat, 21 Jul 2018 15:58:16 -0700 Subject: [PATCH 5/6] Add entry to CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45cf04e641ca..b7044b4c4594 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## master +### Features + +- `[jest-each]` introduces `%#` option to add index of the test to its title + ### Performance - `[jest-changed-files]` limit git and hg commands to specified roots ([#6732](https://github.com/facebook/jest/pull/6732)) From 6b16eaa5f720603bd259358c73723f425866f69b Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Sun, 22 Jul 2018 14:40:34 -0700 Subject: [PATCH 6/6] Replace index in a single place --- packages/jest-each/src/bind.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index 141c7d1ef0ed..03b8b7b53087 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -86,22 +86,23 @@ const arrayFormat = (title, rowIndex, ...args) => { if (prettyIndexes.indexOf(index) !== -1) { return { args: acc.args, - title: acc.title - .replace(PRETTY_PLACEHOLDER, pretty(arg, {maxDepth: 1, min: true})) - .replace(INDEX_PLACEHOLDER, `${rowIndex}`), + title: acc.title.replace( + PRETTY_PLACEHOLDER, + pretty(arg, {maxDepth: 1, min: true}), + ), }; } return { args: acc.args.concat([arg]), - title: acc.title.replace(INDEX_PLACEHOLDER, `${rowIndex}`), + title: acc.title, }; }, {args: [], title}, ); return util.format( - prettyTitle, + prettyTitle.replace(INDEX_PLACEHOLDER, rowIndex.toString()), ...remainingArgs.slice(0, placeholders.length - prettyIndexes.length), ); };