Skip to content

Commit

Permalink
fix(jest-each): interpolate %% correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
frozenbonito committed May 1, 2021
1 parent 26cb29a commit c7ce1c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 6 additions & 3 deletions packages/jest-each/src/__tests__/array.test.ts
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
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

0 comments on commit c7ce1c3

Please sign in to comment.