Skip to content

Commit

Permalink
[jest-each] Fix each array title concatenation (jestjs#6346)
Browse files Browse the repository at this point in the history
* Fix each array title concatenation

* Update changelog

* Fix node 6 placeholders
  • Loading branch information
mattphillips authored and cpojer committed May 30, 2018
1 parent bed7e17 commit dcac849
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Fixes

* `[jest-each]` Stop test title concatenating extra args ([##6346](https://github.com/facebook/jest/pull/#6346))
* `[expect]` toHaveBeenNthCalledWith/nthCalledWith gives wrong call messages if not matched ([#6340](https://github.com/facebook/jest/pull/6340))
* `[jest-each]` Make sure invalid arguments to `each` points back to the user's code ([#6347](https://github.com/facebook/jest/pull/6347))
* `[expect]` toMatchObject throws TypeError when a source property is null ([#6313](https://github.com/facebook/jest/pull/6313))
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) =>
function eachBind(title: string, test: Function): void {
Expand All @@ -23,7 +24,7 @@ export default (cb: Function) => (...args: any) =>
? args[0]
: args[0].map(entry => [entry]);
return table.forEach(row =>
cb(util.format(title, ...row), applyRestParams(row, test)),
cb(arrayFormat(title, ...row), applyRestParams(row, test)),
);
}

Expand Down Expand Up @@ -58,6 +59,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));
};

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

Expand Down

0 comments on commit dcac849

Please sign in to comment.