Skip to content

Commit b4b1eee

Browse files
Michael Fixthymikee
authored andcommitted
Allow test titles to include array index (#6414)
1 parent 9f07036 commit b4b1eee

5 files changed

Lines changed: 14 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44

55
- `[jest-runner]` print stack trace when `process.exit` is called from user code ([#6714](https://github.com/facebook/jest/pull/6714))
6+
- `[jest-each]` introduces `%#` option to add index of the test to its title ([#6414](https://github.com/facebook/jest/pull/6414))
67

78
### Fixes
89

docs/GlobalAPI.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ Use `describe.each` if you keep duplicating the same test suites with different
241241
- `%f` - Floating point value.
242242
- `%j` - JSON.
243243
- `%o` - Object.
244+
- `%#` - Index of the test case.
244245
- `%%` - single percent sign ('%'). This does not consume an argument.
245246
- `fn`: `Function` the suite of tests to be ran, this is the function that will receive the parameters in each row as function arguments.
246247

@@ -488,6 +489,7 @@ Use `test.each` if you keep duplicating the same test with different data. `test
488489
- `%f` - Floating point value.
489490
- `%j` - JSON.
490491
- `%o` - Object.
492+
- `%#` - Index of the test case.
491493
- `%%` - single percent sign ('%'). This does not consume an argument.
492494
- `fn`: `Function` the test to be ran, this is the function that will receive the parameters in each row as function arguments.
493495

packages/jest-each/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jest-each allows you to provide multiple arguments to your `test`/`describe` whi
3333
- `%f` - Floating point value.
3434
- `%j` - JSON.
3535
- `%o` - Object.
36+
- `%#` - Index of the test case.
3637
- `%%` - single percent sign ('%'). This does not consume an argument.
3738
- 🖖 Spock like data tables with [Tagged Template Literals](#tagged-template-literal-of-rows)
3839

@@ -109,6 +110,7 @@ const each = require('jest-each');
109110
- `%f` - Floating point value.
110111
- `%j` - JSON.
111112
- `%o` - Object.
113+
- `%#` - Index of the test case.
112114
- `%%` - single percent sign ('%'). This does not consume an argument.
113115
- testFn: `Function` the test logic, this is the function that will receive the parameters of each row as function arguments
114116

@@ -130,6 +132,7 @@ const each = require('jest-each');
130132
- `%f` - Floating point value.
131133
- `%j` - JSON.
132134
- `%o` - Object.
135+
- `%#` - Index of the test case.
133136
- `%%` - single percent sign ('%'). This does not consume an argument.
134137
- 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
135138

packages/jest-each/src/__tests__/array.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,20 @@ describe('jest-each', () => {
102102
],
103103
]);
104104
const testFunction = get(eachObject, keyPath);
105-
testFunction('expected string: %s %d %s %s %d %j %s %j %d %d', noop);
105+
testFunction('expected string: %s %d %s %s %d %j %s %j %d %d %#', noop);
106106

107107
const globalMock = get(globalTestMocks, keyPath);
108108
expect(globalMock).toHaveBeenCalledTimes(2);
109109
expect(globalMock).toHaveBeenCalledWith(
110110
`expected string: hello 1 null undefined 1.2 ${JSON.stringify({
111111
foo: 'bar',
112-
})} () => {} [] Infinity NaN`,
112+
})} () => {} [] Infinity NaN 0`,
113113
expectFunction,
114114
);
115115
expect(globalMock).toHaveBeenCalledWith(
116116
`expected string: world 1 null undefined 1.2 ${JSON.stringify({
117117
baz: 'qux',
118-
})} () => {} [] Infinity NaN`,
118+
})} () => {} [] Infinity NaN 1`,
119119
expectFunction,
120120
);
121121
});

packages/jest-each/src/bind.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ const EXPECTED_COLOR = chalk.green;
2121
const RECEIVED_COLOR = chalk.red;
2222
const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
2323
const PRETTY_PLACEHOLDER = '%p';
24+
const INDEX_PLACEHOLDER = '%#';
2425

2526
export default (cb: Function) => (...args: any) =>
2627
function eachBind(title: string, test: Function): void {
2728
if (args.length === 1) {
2829
const table: Table = args[0].every(Array.isArray)
2930
? args[0]
3031
: args[0].map(entry => [entry]);
31-
return table.forEach(row =>
32-
cb(arrayFormat(title, ...row), applyRestParams(row, test)),
32+
return table.forEach((row, i) =>
33+
cb(arrayFormat(title, i, ...row), applyRestParams(row, test)),
3334
);
3435
}
3536

@@ -76,7 +77,7 @@ const getPrettyIndexes = placeholders =>
7677
[],
7778
);
7879

79-
const arrayFormat = (title, ...args) => {
80+
const arrayFormat = (title, rowIndex, ...args) => {
8081
const placeholders = title.match(SUPPORTED_PLACEHOLDERS) || [];
8182
const prettyIndexes = getPrettyIndexes(placeholders);
8283

@@ -101,7 +102,7 @@ const arrayFormat = (title, ...args) => {
101102
);
102103

103104
return util.format(
104-
prettyTitle,
105+
prettyTitle.replace(INDEX_PLACEHOLDER, rowIndex.toString()),
105106
...remainingArgs.slice(0, placeholders.length - prettyIndexes.length),
106107
);
107108
};

0 commit comments

Comments
 (0)