Skip to content

Commit b7541ac

Browse files
authored
feat: introduce %$ option to add number of the test to its title (#14710)
1 parent 03d9036 commit b7541ac

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- `[jest-snapshot]` Support Prettier 3 ([#14566](https://github.com/facebook/jest/pull/14566))
3737
- `[@jest/util-snapshot]` Extract utils used by tooling from `jest-snapshot` into its own package ([#15095](https://github.com/facebook/jest/pull/15095))
3838
- `[pretty-format]` [**BREAKING**] Do not render empty string children (`''`) in React plugin ([#14470](https://github.com/facebook/jest/pull/14470))
39+
- `[jest-each]` Introduce `%$` option to add number of the test to its title ([#14710](https://github.com/jestjs/jest/pull/14710))
3940

4041
### Fixes
4142

docs/GlobalAPI.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ Use `describe.each` if you keep duplicating the same test suites with different
248248
- `%j` - JSON.
249249
- `%o` - Object.
250250
- `%#` - Index of the test case.
251+
- `%$` - Number of the test case.
251252
- `%%` - single percent sign ('%'). This does not consume an argument.
252253
- Or generate unique test titles by injecting properties of test case object with `$variable`
253254
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value`
@@ -550,6 +551,7 @@ Use `test.concurrent.each` if you keep duplicating the same test with different
550551
- `%j` - JSON.
551552
- `%o` - Object.
552553
- `%#` - Index of the test case.
554+
- `%$` - Number of the test case.
553555
- `%%` - single percent sign ('%'). This does not consume an argument.
554556
- `fn`: `Function` the test to be run, this is the function that will receive the parameters in each row as function arguments, **this will have to be an asynchronous function**.
555557
- Optionally, you can provide a `timeout` (in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
@@ -692,6 +694,7 @@ Use `test.each` if you keep duplicating the same test with different data. `test
692694
- `%j` - JSON.
693695
- `%o` - Object.
694696
- `%#` - Index of the test case.
697+
- `%$` - Number of the test case.
695698
- `%%` - single percent sign ('%'). This does not consume an argument.
696699
- Or generate unique test titles by injecting properties of test case object with `$variable`
697700
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value`

packages/jest-each/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jest-each allows you to provide multiple arguments to your `test`/`describe` whi
4040
- `%j` - JSON.
4141
- `%o` - Object.
4242
- `%#` - Index of the test case.
43+
- `%$` - Number of the test case.
4344
- `%%` - single percent sign ('%'). This does not consume an argument.
4445
- Unique test titles by injecting properties of test case object
4546
- 🖖 Spock like data tables with [Tagged Template Literals](#tagged-template-literal-of-rows)
@@ -118,6 +119,7 @@ const each = require('jest-each').default;
118119
- `%j` - JSON.
119120
- `%o` - Object.
120121
- `%#` - Index of the test case.
122+
- `%$` - Number of the test case.
121123
- `%%` - single percent sign ('%'). This does not consume an argument.
122124
- Or generate unique test titles by injecting properties of test case object with `$variable`
123125
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value`
@@ -144,6 +146,7 @@ const each = require('jest-each').default;
144146
- `%j` - JSON.
145147
- `%o` - Object.
146148
- `%#` - Index of the test case.
149+
- `%$` - Number of the test case.
147150
- `%%` - single percent sign ('%'). This does not consume an argument.
148151
- Or generate unique test titles by injecting properties of test case object with `$variable`
149152
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value`

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ describe('jest-each', () => {
144144
]);
145145
const testFunction = get(eachObject, keyPath);
146146
testFunction(
147-
'expected string: %% %%s %s %d %s %s %d %j %s %j %d %d %#',
147+
'expected string: %% %%s %s %d %s %s %d %j %s %j %d %d %# %$',
148148
noop,
149149
);
150150

@@ -153,14 +153,14 @@ describe('jest-each', () => {
153153
expect(globalMock).toHaveBeenCalledWith(
154154
`expected string: % %s hello 1 null undefined 1.2 ${JSON.stringify({
155155
foo: 'bar',
156-
})} () => {} [] Infinity NaN 0`,
156+
})} () => {} [] Infinity NaN 0 1`,
157157
expectFunction,
158158
undefined,
159159
);
160160
expect(globalMock).toHaveBeenCalledWith(
161161
`expected string: % %s world 1 null undefined 1.2 ${JSON.stringify({
162162
baz: 'qux',
163-
})} () => {} [] Infinity NaN 1`,
163+
})} () => {} [] Infinity NaN 1 2`,
164164
expectFunction,
165165
undefined,
166166
);

packages/jest-each/src/table/array.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {type Templates, interpolateVariables} from './interpolation';
1515
const SUPPORTED_PLACEHOLDERS = /%[#Odfijops]/g;
1616
const PRETTY_PLACEHOLDER = '%p';
1717
const INDEX_PLACEHOLDER = '%#';
18+
const NUMBER_PLACEHOLDER = '%$';
1819
const PLACEHOLDER_PREFIX = '%';
1920
const ESCAPED_PLACEHOLDER_PREFIX = /%%/g;
2021
const JEST_EACH_PLACEHOLDER_ESCAPE = '@@__JEST_EACH_PLACEHOLDER_ESCAPE__@@';
@@ -71,7 +72,10 @@ const formatTitle = (
7172

7273
return util.format(formattedTitle, normalisedValue);
7374
},
74-
interpolateTitleIndex(interpolateEscapedPlaceholders(title), rowIndex),
75+
interpolateTitleIndexAndNumber(
76+
interpolateEscapedPlaceholders(title),
77+
rowIndex,
78+
),
7579
)
7680
.replaceAll(
7781
new RegExp(JEST_EACH_PLACEHOLDER_ESCAPE, 'g'),
@@ -92,8 +96,10 @@ const getMatchingPlaceholders = (title: string) =>
9296
const interpolateEscapedPlaceholders = (title: string) =>
9397
title.replaceAll(ESCAPED_PLACEHOLDER_PREFIX, JEST_EACH_PLACEHOLDER_ESCAPE);
9498

95-
const interpolateTitleIndex = (title: string, index: number) =>
96-
title.replace(INDEX_PLACEHOLDER, index.toString());
99+
const interpolateTitleIndexAndNumber = (title: string, index: number) =>
100+
title
101+
.replace(INDEX_PLACEHOLDER, index.toString())
102+
.replace(NUMBER_PLACEHOLDER, (index + 1).toString());
97103

98104
const interpolatePrettyPlaceholder = (title: string, value: unknown) =>
99105
title.replace(PRETTY_PLACEHOLDER, pretty(value, {maxDepth: 1, min: true}));

0 commit comments

Comments
 (0)