diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fec529ec774..aa2b96e0404e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - `[jest-runner]` print stack trace when `process.exit` is called from user code ([#6714](https://github.com/facebook/jest/pull/6714)) +- `[jest-each]` introduces `%#` option to add index of the test to its title ([#6414](https://github.com/facebook/jest/pull/6414)) ### Fixes diff --git a/docs/GlobalAPI.md b/docs/GlobalAPI.md index 98414cef4145..b87c9a7aa548 100644 --- a/docs/GlobalAPI.md +++ b/docs/GlobalAPI.md @@ -241,6 +241,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. @@ -488,6 +489,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 7df07bb751bb..b3d269d0b2a5 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) @@ -109,6 +110,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 @@ -130,6 +132,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 diff --git a/packages/jest-each/src/__tests__/array.test.js b/packages/jest-each/src/__tests__/array.test.js index 6b8fb1ef1adc..2be82eef691a 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 1`, expectFunction, ); }); diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index 1d72d097f394..03b8b7b53087 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 { @@ -28,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)), ); } @@ -76,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); @@ -101,7 +102,7 @@ const arrayFormat = (title, ...args) => { ); return util.format( - prettyTitle, + prettyTitle.replace(INDEX_PLACEHOLDER, rowIndex.toString()), ...remainingArgs.slice(0, placeholders.length - prettyIndexes.length), ); };