Skip to content

Commit

Permalink
[jest-each] Fix bug with placeholder values (#8289)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips authored and SimenB committed Apr 16, 2019
1 parent b9c5df8 commit 14b7efa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,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/runtime, @jest/transform]` Allow custom transforms for JSON dependencies ([#2578](https://github.com/facebook/jest/pull/2578))
- `[jest-core]` Make `detectOpenHandles` imply `runInBand` ([#8283](https://github.com/facebook/jest/pull/8283))
Expand Down
23 changes: 23 additions & 0 deletions packages/jest-each/src/__tests__/array.test.ts
Expand Up @@ -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,
);
});
});
});
});
24 changes: 17 additions & 7 deletions packages/jest-each/src/table/array.ts
Expand Up @@ -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) => ({
Expand All @@ -35,15 +37,23 @@ const formatTitle = (
row: Global.Row,
rowIndex: number,
): string =>
row.reduce<string>((formattedTitle, value) => {
const [placeholder] = getMatchingPlaceholders(formattedTitle);
if (!placeholder) return formattedTitle;
row
.reduce<string>((formattedTitle, value) => {
const [placeholder] = getMatchingPlaceholders(formattedTitle);
const normalisedValue = normalisePlaceholderValue(value);
if (!placeholder) return formattedTitle;

if (placeholder === PRETTY_PLACEHOLDER)
return interpolatePrettyPlaceholder(formattedTitle, value);
if (placeholder === PRETTY_PLACEHOLDER)
return interpolatePrettyPlaceholder(formattedTitle, normalisedValue);

return util.format(formattedTitle, value);
}, interpolateTitleIndex(title, rowIndex));
return util.format(formattedTitle, normalisedValue);
}, 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) || [];
Expand Down

0 comments on commit 14b7efa

Please sign in to comment.