From dc8cdab5a8b2135bf19d35a9feb17d4db1046dd5 Mon Sep 17 00:00:00 2001 From: Tetsuro Aoki Date: Sat, 1 May 2021 13:48:31 +0900 Subject: [PATCH] fix(jest-each): fix wrong interpolation --- CHANGELOG.md | 1 + packages/jest-each/src/__tests__/array.test.ts | 8 +++++++- packages/jest-each/src/table/array.ts | 7 +++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4697a0a1282e..37513c8a5370 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ - `[jest-each]` [**BREAKING**] Ignore excess words in headings ([#8766](https://github.com/facebook/jest/pull/8766)) - `[jest-each]` Support array index with template strings ([#10763](https://github.com/facebook/jest/pull/10763)) - `[jest-each]` Interpolate `%%` correctly ([#11364](https://github.com/facebook/jest/pull/11364)) +- `[jest-each]` Fix wrong interpolation when the value of array contains multiple `%` ([#11364](https://github.com/facebook/jest/pull/11364)) - `[jest-environment]` [**BREAKING**] Drop support for `runScript` for test environments ([#11155](https://github.com/facebook/jest/pull/11155)) - `[jest-environment-jsdom]` Use inner realm’s `ArrayBuffer` constructor ([#10885](https://github.com/facebook/jest/pull/10885)) - `[jest-environment-jsdom]` [**BREAKING**] Remove Node globals `setImmediate` and `clearImmediate` [#11222](https://github.com/facebook/jest/pull/11222) diff --git a/packages/jest-each/src/__tests__/array.test.ts b/packages/jest-each/src/__tests__/array.test.ts index 471dd172c88f..c76609d80505 100644 --- a/packages/jest-each/src/__tests__/array.test.ts +++ b/packages/jest-each/src/__tests__/array.test.ts @@ -403,12 +403,13 @@ describe('jest-each', () => { const eachObject = each.withGlobal(globalTestMocks)([ ['hello', '%d', 10, '%s', {foo: 'bar'}], ['world', '%i', 1991, '%p', {foo: 'bar'}], + ['joe', '%d %d', 10, '%%s', {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).toHaveBeenCalledTimes(3); expect(globalMock).toHaveBeenCalledWith( 'expected string: hello %d 10 %s {"foo": "bar"}', expectFunction, @@ -419,6 +420,11 @@ describe('jest-each', () => { expectFunction, undefined, ); + expect(globalMock).toHaveBeenCalledWith( + 'expected string: joe %d %d 10 %%s {"foo": "bar"}', + expectFunction, + undefined, + ); }); }); }); diff --git a/packages/jest-each/src/table/array.ts b/packages/jest-each/src/table/array.ts index 4fc272982976..fe245e8e77bd 100644 --- a/packages/jest-each/src/table/array.ts +++ b/packages/jest-each/src/table/array.ts @@ -51,8 +51,11 @@ const formatTitle = ( .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) + typeof value === 'string' + ? value.replace( + new RegExp(PLACEHOLDER_PREFIX, 'g'), + JEST_EACH_PLACEHOLDER_ESCAPE, + ) : value; const getMatchingPlaceholders = (title: string) =>