Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(jest-each): interpolate %% correctly
  • Loading branch information
frozenbonito committed May 1, 2021
commit 896d89c401c4bc9a001d6f284568ec6771763688
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
- `[jest-core]` Use `WeakRef` to hold timers when detecting open handles ([#11277](https://github.com/facebook/jest/pull/11277))
- `[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-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)
Expand Down
9 changes: 6 additions & 3 deletions packages/jest-each/src/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,22 @@ describe('jest-each', () => {
],
]);
const testFunction = get(eachObject, keyPath);
testFunction('expected string: %s %d %s %s %d %j %s %j %d %d %#', noop);
testFunction(
'expected string: %% %%s %s %d %s %s %d %j %s %j %d %d %#',
noop,
);

const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledTimes(2);
expect(globalMock).toHaveBeenCalledWith(
`expected string: hello 1 null undefined 1.2 ${JSON.stringify({
`expected string: % %s hello 1 null undefined 1.2 ${JSON.stringify({
foo: 'bar',
})} () => {} [] Infinity NaN 0`,
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
`expected string: world 1 null undefined 1.2 ${JSON.stringify({
`expected string: % %s world 1 null undefined 1.2 ${JSON.stringify({
baz: 'qux',
})} () => {} [] Infinity NaN 1`,
expectFunction,
Expand Down
8 changes: 6 additions & 2 deletions packages/jest-each/src/table/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import type {Global} from '@jest/types';
import {format as pretty} from 'pretty-format';
import type {EachTests} from '../bind';

const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp]/g;
const PRETTY_PLACEHOLDER = '%p';
const INDEX_PLACEHOLDER = '%#';
const PLACEHOLDER_PREFIX = '%';
const ESCAPED_PLACEHOLDER_PREFIX = /%%/g;
const JEST_EACH_PLACEHOLDER_ESCAPE = '@@__JEST_EACH_PLACEHOLDER_ESCAPE__@@';

export default (title: string, arrayTable: Global.ArrayTable): EachTests =>
Expand Down Expand Up @@ -46,7 +47,7 @@ const formatTitle = (
return interpolatePrettyPlaceholder(formattedTitle, normalisedValue);

return util.format(formattedTitle, normalisedValue);
}, interpolateTitleIndex(title, rowIndex))
}, interpolateTitleIndex(interpolateEscapedPlaceholders(title), rowIndex))
.replace(new RegExp(JEST_EACH_PLACEHOLDER_ESCAPE, 'g'), PLACEHOLDER_PREFIX);

const normalisePlaceholderValue = (value: unknown) =>
Expand All @@ -57,6 +58,9 @@ const normalisePlaceholderValue = (value: unknown) =>
const getMatchingPlaceholders = (title: string) =>
title.match(SUPPORTED_PLACEHOLDERS) || [];

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());

Expand Down