Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jest-each] Fix bug with placeholder values #8289

Merged
merged 3 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
23 changes: 23 additions & 0 deletions packages/jest-each/src/__tests__/array.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 normalisedValued = normalisePlaceholderValue(value);
SimenB marked this conversation as resolved.
Show resolved Hide resolved
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) || [];
Expand Down