From 06209caf4126c5e9f4bfa810a6b69f4519d8b14d Mon Sep 17 00:00:00 2001 From: Ryo Ochiai Date: Thu, 23 Nov 2023 00:47:42 +0900 Subject: [PATCH 1/3] Introduce %$ option to add number of the test to its title --- packages/jest-each/src/__tests__/array.test.ts | 6 +++--- packages/jest-each/src/table/array.ts | 12 +++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/jest-each/src/__tests__/array.test.ts b/packages/jest-each/src/__tests__/array.test.ts index 7bd21be8e52d..f496747524b0 100644 --- a/packages/jest-each/src/__tests__/array.test.ts +++ b/packages/jest-each/src/__tests__/array.test.ts @@ -144,7 +144,7 @@ describe('jest-each', () => { ]); const testFunction = get(eachObject, keyPath); testFunction( - 'expected string: %% %%s %s %d %s %s %d %j %s %j %d %d %#', + 'expected string: %% %%s %s %d %s %s %d %j %s %j %d %d %# %$', noop, ); @@ -153,14 +153,14 @@ describe('jest-each', () => { expect(globalMock).toHaveBeenCalledWith( `expected string: % %s hello 1 null undefined 1.2 ${JSON.stringify({ foo: 'bar', - })} () => {} [] Infinity NaN 0`, + })} () => {} [] Infinity NaN 0 1`, expectFunction, undefined, ); expect(globalMock).toHaveBeenCalledWith( `expected string: % %s world 1 null undefined 1.2 ${JSON.stringify({ baz: 'qux', - })} () => {} [] Infinity NaN 1`, + })} () => {} [] Infinity NaN 1 2`, expectFunction, undefined, ); diff --git a/packages/jest-each/src/table/array.ts b/packages/jest-each/src/table/array.ts index 1b1101c26a37..86ddb8470fff 100644 --- a/packages/jest-each/src/table/array.ts +++ b/packages/jest-each/src/table/array.ts @@ -15,6 +15,7 @@ import {type Templates, interpolateVariables} from './interpolation'; const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp#]/g; const PRETTY_PLACEHOLDER = '%p'; const INDEX_PLACEHOLDER = '%#'; +const NUMBER_PLACEHOLDER = '%$'; const PLACEHOLDER_PREFIX = '%'; const ESCAPED_PLACEHOLDER_PREFIX = /%%/g; const JEST_EACH_PLACEHOLDER_ESCAPE = '@@__JEST_EACH_PLACEHOLDER_ESCAPE__@@'; @@ -71,7 +72,10 @@ const formatTitle = ( return util.format(formattedTitle, normalisedValue); }, - interpolateTitleIndex(interpolateEscapedPlaceholders(title), rowIndex), + interpolateTitleIndexAndNumber( + interpolateEscapedPlaceholders(title), + rowIndex, + ), ) .replace(new RegExp(JEST_EACH_PLACEHOLDER_ESCAPE, 'g'), PLACEHOLDER_PREFIX); @@ -89,8 +93,10 @@ const getMatchingPlaceholders = (title: string) => const interpolateEscapedPlaceholders = (title: string) => title.replace(ESCAPED_PLACEHOLDER_PREFIX, JEST_EACH_PLACEHOLDER_ESCAPE); -const interpolateTitleIndex = (title: string, index: number) => - title.replace(INDEX_PLACEHOLDER, index.toString()); +const interpolateTitleIndexAndNumber = (title: string, index: number) => + title + .replace(INDEX_PLACEHOLDER, index.toString()) + .replace(NUMBER_PLACEHOLDER, (index + 1).toString()); const interpolatePrettyPlaceholder = (title: string, value: unknown) => title.replace(PRETTY_PLACEHOLDER, pretty(value, {maxDepth: 1, min: true})); From 7bf14047f3e84c94a0fadf0a3bfeddbce7009b61 Mon Sep 17 00:00:00 2001 From: Ryo Ochiai Date: Thu, 23 Nov 2023 00:48:32 +0900 Subject: [PATCH 2/3] Update docs about introducing %$ option --- docs/GlobalAPI.md | 3 +++ packages/jest-each/README.md | 3 +++ 2 files changed, 6 insertions(+) diff --git a/docs/GlobalAPI.md b/docs/GlobalAPI.md index 2a04d4e352a3..2e98f3ce980b 100644 --- a/docs/GlobalAPI.md +++ b/docs/GlobalAPI.md @@ -248,6 +248,7 @@ Use `describe.each` if you keep duplicating the same test suites with different - `%j` - JSON. - `%o` - Object. - `%#` - Index of the test case. + - `%$` - Number of the test case. - `%%` - single percent sign ('%'). This does not consume an argument. - Or generate unique test titles by injecting properties of test case object with `$variable` - 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 - `%j` - JSON. - `%o` - Object. - `%#` - Index of the test case. + - `%$` - Number of the test case. - `%%` - single percent sign ('%'). This does not consume an argument. - `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**. - 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 - `%j` - JSON. - `%o` - Object. - `%#` - Index of the test case. + - `%$` - Number of the test case. - `%%` - single percent sign ('%'). This does not consume an argument. - Or generate unique test titles by injecting properties of test case object with `$variable` - To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` diff --git a/packages/jest-each/README.md b/packages/jest-each/README.md index 3b4384387ca4..fd4be0917eed 100644 --- a/packages/jest-each/README.md +++ b/packages/jest-each/README.md @@ -40,6 +40,7 @@ jest-each allows you to provide multiple arguments to your `test`/`describe` whi - `%j` - JSON. - `%o` - Object. - `%#` - Index of the test case. + - `%$` - Number of the test case. - `%%` - single percent sign ('%'). This does not consume an argument. - Unique test titles by injecting properties of test case object - 🖖 Spock like data tables with [Tagged Template Literals](#tagged-template-literal-of-rows) @@ -118,6 +119,7 @@ const each = require('jest-each').default; - `%j` - JSON. - `%o` - Object. - `%#` - Index of the test case. + - `%$` - Number of the test case. - `%%` - single percent sign ('%'). This does not consume an argument. - Or generate unique test titles by injecting properties of test case object with `$variable` - 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; - `%j` - JSON. - `%o` - Object. - `%#` - Index of the test case. + - `%$` - Number of the test case. - `%%` - single percent sign ('%'). This does not consume an argument. - Or generate unique test titles by injecting properties of test case object with `$variable` - To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` From 04599b2d1abae0f4c3b2587f23856b6456fbba19 Mon Sep 17 00:00:00 2001 From: Ryo Ochiai Date: Thu, 23 Nov 2023 01:06:17 +0900 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd908cf69363..0d89e1367b24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - `[jest-snapshot]` [**BREAKING**] Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in snapshots ([#13965](https://github.com/facebook/jest/pull/13965)) - `[jest-snapshot]` Support Prettier 3 ([#14566](https://github.com/facebook/jest/pull/14566)) - `[pretty-format]` [**BREAKING**] Do not render empty string children (`''`) in React plugin ([#14470](https://github.com/facebook/jest/pull/14470)) +- `[jest-each]` Introduce `%$` option to add number of the test to its title ([#14710](https://github.com/jestjs/jest/pull/14710)) ### Fixes