From 7de00faef4988107f5a714f593008be38be63e48 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Mon, 8 Apr 2019 22:00:51 +0100 Subject: [PATCH 1/3] Fix jest-each bug with placeholder values --- .../jest-each/src/__tests__/array.test.ts | 23 ++++++++++++++++++ packages/jest-each/src/table/array.ts | 24 +++++++++++++------ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/packages/jest-each/src/__tests__/array.test.ts b/packages/jest-each/src/__tests__/array.test.ts index ceeffe197a92..64bced0cee50 100644 --- a/packages/jest-each/src/__tests__/array.test.ts +++ b/packages/jest-each/src/__tests__/array.test.ts @@ -385,6 +385,29 @@ describe('jest-each', () => { undefined, ); }); + + test('calls global with title with placeholder values correctly interpolated', () => { + const globalTestMocks = getGlobalTestMocks(); + const eachObject = each.withGlobal(globalTestMocks)([ + ['hello', '%d', 10, '%s', {foo: 'bar'}], + ['world', '%i', 1991, '%p', {foo: 'bar'}], + ]); + const testFunction = get(eachObject, keyPath); + testFunction('expected string: %s %s %d %s %p', () => {}); + + const globalMock = get(globalTestMocks, keyPath); + expect(globalMock).toHaveBeenCalledTimes(2); + expect(globalMock).toHaveBeenCalledWith( + 'expected string: hello %d 10 %s {"foo": "bar"}', + expectFunction, + undefined, + ); + expect(globalMock).toHaveBeenCalledWith( + 'expected string: world %i 1991 %p {"foo": "bar"}', + expectFunction, + undefined, + ); + }); }); }); }); diff --git a/packages/jest-each/src/table/array.ts b/packages/jest-each/src/table/array.ts index de9be8c0c57b..b67e28ae9e74 100644 --- a/packages/jest-each/src/table/array.ts +++ b/packages/jest-each/src/table/array.ts @@ -15,6 +15,8 @@ import {EachTests} from '../bind'; const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g; const PRETTY_PLACEHOLDER = '%p'; const INDEX_PLACEHOLDER = '%#'; +const PLACEHOLDER_PREFIX = '%'; +const JEST_EACH_PLACEHOLDER_ESCAPE = '@@__JEST_EACH_PLACEHOLDER_ESCAPE__@@'; export default (title: string, arrayTable: Global.ArrayTable): EachTests => normaliseTable(arrayTable).map((row, index) => ({ @@ -35,15 +37,23 @@ const formatTitle = ( row: Global.Row, rowIndex: number, ): string => - row.reduce((formattedTitle, value) => { - const [placeholder] = getMatchingPlaceholders(formattedTitle); - if (!placeholder) return formattedTitle; + row + .reduce((formattedTitle, value) => { + const [placeholder] = getMatchingPlaceholders(formattedTitle); + const normalisedValued = normalisePlaceholderValue(value); + if (!placeholder) return formattedTitle; - if (placeholder === PRETTY_PLACEHOLDER) - return interpolatePrettyPlaceholder(formattedTitle, value); + if (placeholder === PRETTY_PLACEHOLDER) + return interpolatePrettyPlaceholder(formattedTitle, normalisedValued); - return util.format(formattedTitle, value); - }, interpolateTitleIndex(title, rowIndex)); + return util.format(formattedTitle, normalisedValued); + }, interpolateTitleIndex(title, rowIndex)) + .replace(new RegExp(JEST_EACH_PLACEHOLDER_ESCAPE, 'g'), PLACEHOLDER_PREFIX); + +const normalisePlaceholderValue = (value: unknown) => + typeof value === 'string' && SUPPORTED_PLACEHOLDERS.test(value) + ? value.replace(PLACEHOLDER_PREFIX, JEST_EACH_PLACEHOLDER_ESCAPE) + : value; const getMatchingPlaceholders = (title: string) => title.match(SUPPORTED_PLACEHOLDERS) || []; From d8ba932dcc2e23349b60d616188e1effe295929c Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Mon, 8 Apr 2019 22:04:35 +0100 Subject: [PATCH 2/3] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8a1714270f7..5183de7e030d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Fixes +- `[jest-each]` Fix bug with placeholder values ([#8289](https://github.com/facebook/jest/pull/8289)) - `[jest-snapshot]` Inline snapshots: do not indent empty lines ([#8277](https://github.com/facebook/jest/pull/8277)) - `[jest-core]` Make `detectOpenHandles` imply `runInBand` ([#8283](https://github.com/facebook/jest/pull/8283)) From 45d93221faccead73daa4ba2d8a77f518ba6c83f Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 9 Apr 2019 09:55:10 +0100 Subject: [PATCH 3/3] Fix typo --- packages/jest-each/src/table/array.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-each/src/table/array.ts b/packages/jest-each/src/table/array.ts index b67e28ae9e74..292a9e5e26a0 100644 --- a/packages/jest-each/src/table/array.ts +++ b/packages/jest-each/src/table/array.ts @@ -40,13 +40,13 @@ const formatTitle = ( row .reduce((formattedTitle, value) => { const [placeholder] = getMatchingPlaceholders(formattedTitle); - const normalisedValued = normalisePlaceholderValue(value); + const normalisedValue = normalisePlaceholderValue(value); if (!placeholder) return formattedTitle; if (placeholder === PRETTY_PLACEHOLDER) - return interpolatePrettyPlaceholder(formattedTitle, normalisedValued); + return interpolatePrettyPlaceholder(formattedTitle, normalisedValue); - return util.format(formattedTitle, normalisedValued); + return util.format(formattedTitle, normalisedValue); }, interpolateTitleIndex(title, rowIndex)) .replace(new RegExp(JEST_EACH_PLACEHOLDER_ESCAPE, 'g'), PLACEHOLDER_PREFIX);