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 each array title concatenation #6346

Merged
merged 4 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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]` Stop test title concatenating extra args ([##6346](https://github.com/facebook/jest/pull/#6346))
* `[expect]` toMatchObject throws TypeError when a source property is null ([#6313](https://github.com/facebook/jest/pull/6313))
* `[jest-cli]` Normalize slashes in paths in CLI output on Windows ([#6310](https://github.com/facebook/jest/pull/6310))

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/each.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ exports[`shows error message when not enough arguments are supplied to tests 1`]

Missing 1 arguments

at packages/jest-each/build/bind.js:81:17
at packages/jest-each/build/bind.js:82:17

"
`;
Expand Down
59 changes: 53 additions & 6 deletions packages/jest-each/src/__tests__/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,70 @@ describe('jest-each', () => {
);
});

test('calls global with title containing param values when using sprintf format', () => {
test('calls global with title containing param values when using printf format', () => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)([
['hello', 1],
['world', 2],
[
'hello',
1,
null,
undefined,
1.2,
{foo: 'bar'},
() => {},
[],
Infinity,
NaN,
],
[
'world',
1,
null,
undefined,
1.2,
{baz: 'qux'},
() => {},
[],
Infinity,
NaN,
],
]);
const testFunction = get(eachObject, keyPath);
testFunction('expected string: %s %s', noop);
testFunction('expected string: %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',
`expected string: hello 1 null undefined 1.2 ${JSON.stringify({
foo: 'bar',
})} () => {} [] Infinity NaN`,
expectFunction,
);
expect(globalMock).toHaveBeenCalledWith(
'expected string: world 2',
`expected string: world 1 null undefined 1.2 ${JSON.stringify({
baz: 'qux',
})} () => {} [] Infinity NaN`,
expectFunction,
);
});

test('does not call global test with title containing more param values than sprintf placeholders', () => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)([
['hello', 1, 2, 3, 4, 5],
['world', 1, 2, 3, 4, 5],
]);
const testFunction = get(eachObject, keyPath);
testFunction('expected string: %s', noop);

const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledTimes(2);
expect(globalMock).toHaveBeenCalledWith(
'expected string: hello',
expectFunction,
);
expect(globalMock).toHaveBeenCalledWith(
'expected string: world',
expectFunction,
);
});
Expand Down
8 changes: 7 additions & 1 deletion packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Table = Array<Array<any>>;

const EXPECTED_COLOR = chalk.green;
const RECEIVED_COLOR = chalk.red;
const SUPPORTED_PLACEHOLDERS = /%[sdifjoO%]/g;

export default (cb: Function) => (...args: any) => (
title: string,
Expand All @@ -23,7 +24,7 @@ export default (cb: Function) => (...args: any) => (
if (args.length === 1) {
const table: Table = args[0];
return table.forEach(row =>
cb(util.format(title, ...row), applyRestParams(row, test)),
cb(arrayFormat(title, ...row), applyRestParams(row, test)),
);
}

Expand Down Expand Up @@ -52,6 +53,11 @@ export default (cb: Function) => (...args: any) => (
);
};

const arrayFormat = (str, ...args) => {
const matches = (str.match(SUPPORTED_PLACEHOLDERS) || []).length;
return util.format(str, ...args.slice(0, matches));
};
Copy link
Member

@SimenB SimenB May 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about matches.length > args.length?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.slice is greedy and will take all of the args still, there will just be some placeholders that aren't replaced. Do we care about this case? I think it should be pretty obvious that the placeholders aren't replaced as there isn't a value for them 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we throw? Probably not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it should fail the tests, I wouldn't say its an error case. Just a developer mistake, perhaps we could log a warning?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add a lint rule for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it 😄


const applyRestParams = (params: Array<any>, test: Function) => {
if (params.length < test.length) return done => test(...params, done);

Expand Down