From c40d82ff41a461fc02be77c230829e4e24ad9206 Mon Sep 17 00:00:00 2001 From: feng Date: Mon, 28 Feb 2022 23:33:53 +0800 Subject: [PATCH] fix(jest-each): `%#` is not replaced with index of the test case --- CHANGELOG.md | 1 + .../jest-each/src/__tests__/array.test.ts | 46 +++++++++++++++++++ packages/jest-each/src/table/array.ts | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a8c3863378b..c583e938da01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ - `[jest-config]` Pass `moduleTypes` to `ts-node` to enforce CJS when transpiling ([#12397](https://github.com/facebook/jest/pull/12397)) - `[jest-config, jest-haste-map]` Allow searching for tests in `node_modules` by exposing `retainAllFiles` ([#11084](https://github.com/facebook/jest/pull/11084)) - `[jest-core]` [**BREAKING**] Exit with status `1` if no tests are found with `--findRelatedTests` flag ([#12487](https://github.com/facebook/jest/pull/12487)) +- `[jest-each]` `%#` is not replaced with index of the test case ([#12517](https://github.com/facebook/jest/pull/12517)) - `[jest-environment-jsdom]` Make `jsdom` accessible to extending environments again ([#12232](https://github.com/facebook/jest/pull/12232)) - `[jest-environment-jsdom]` Log JSDOM errors more cleanly ([#12386](https://github.com/facebook/jest/pull/12386)) - `[@jest/expect-utils]` [**BREAKING**] Fix false positives when looking for `undefined` prop ([#8923](https://github.com/facebook/jest/pull/8923)) diff --git a/packages/jest-each/src/__tests__/array.test.ts b/packages/jest-each/src/__tests__/array.test.ts index a7eb3e0e11f4..b89e31f49d32 100644 --- a/packages/jest-each/src/__tests__/array.test.ts +++ b/packages/jest-each/src/__tests__/array.test.ts @@ -368,6 +368,52 @@ describe('jest-each', () => { undefined, ); }); + + test('calls global with title containing param values when using %#', () => { + const globalTestMocks = getGlobalTestMocks(); + const eachObject = each.withGlobal(globalTestMocks)([ + {name: 'foo'}, + {name: 'bar'}, + ]); + const testFunction = get(eachObject, keyPath); + testFunction('expected index: %#', () => {}); + + const globalMock = get(globalTestMocks, keyPath); + expect(globalMock).toHaveBeenCalledTimes(2); + expect(globalMock).toHaveBeenCalledWith( + 'expected index: 0', + expectFunction, + undefined, + ); + expect(globalMock).toHaveBeenCalledWith( + 'expected index: 1', + expectFunction, + undefined, + ); + }); + + test('calls global with title containing param values when using $#', () => { + const globalTestMocks = getGlobalTestMocks(); + const eachObject = each.withGlobal(globalTestMocks)([ + {name: 'foo'}, + {name: 'bar'}, + ]); + const testFunction = get(eachObject, keyPath); + testFunction('expected index: $#', () => {}); + + const globalMock = get(globalTestMocks, keyPath); + expect(globalMock).toHaveBeenCalledTimes(2); + expect(globalMock).toHaveBeenCalledWith( + 'expected index: 0', + expectFunction, + undefined, + ); + expect(globalMock).toHaveBeenCalledWith( + 'expected index: 1', + expectFunction, + undefined, + ); + }); }); }); diff --git a/packages/jest-each/src/table/array.ts b/packages/jest-each/src/table/array.ts index a4fb9d5d0c63..14bc5df89e07 100644 --- a/packages/jest-each/src/table/array.ts +++ b/packages/jest-each/src/table/array.ts @@ -13,7 +13,7 @@ import type {EachTests} from '../bind'; import type {Templates} from './interpolation'; import {interpolateVariables} from './interpolation'; -const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp]/g; +const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp#]/g; const PRETTY_PLACEHOLDER = '%p'; const INDEX_PLACEHOLDER = '%#'; const PLACEHOLDER_PREFIX = '%';