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

Make sure invalid jest-each points to user code #6347

Merged
merged 3 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all 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]` 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))
* `[jest-cli]` Normalize slashes in paths in CLI output on Windows ([#6310](https://github.com/facebook/jest/pull/6310))

Expand Down
10 changes: 9 additions & 1 deletion e2e/__tests__/__snapshots__/each.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ 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
6 | */
7 |
> 8 | it.each\`
| ^
9 | left | right
10 | \${true} | \${true}
11 | \${true} |

at __tests__/each-exception.test.js:8:1

"
`;
Expand Down
50 changes: 27 additions & 23 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,23 @@ type Table = Array<Array<any>>;
const EXPECTED_COLOR = chalk.green;
const RECEIVED_COLOR = chalk.red;

export default (cb: Function) => (...args: any) => (
title: string,
test: Function,
): void => {
if (args.length === 1) {
const table: Table = args[0];
return table.forEach(row =>
cb(util.format(title, ...row), applyRestParams(row, test)),
);
}
export default (cb: Function) => (...args: any) =>
function eachBind(title: string, test: Function): void {
if (args.length === 1) {
const table: Table = args[0];
return table.forEach(row =>
cb(util.format(title, ...row), applyRestParams(row, test)),
);
}

const templateStrings = args[0];
const data = args.slice(1);
const templateStrings = args[0];
const data = args.slice(1);

const keys = getHeadingKeys(templateStrings[0]);
const table = buildTable(data, keys.length, keys);
const keys = getHeadingKeys(templateStrings[0]);
const table = buildTable(data, keys.length, keys);

if (data.length % keys.length !== 0) {
return cb(title, () => {
throw new Error(
if (data.length % keys.length !== 0) {
const error = new Error(
'Not enough arguments supplied for given headings:\n' +
EXPECTED_COLOR(keys.join(' | ')) +
'\n\n' +
Expand All @@ -44,13 +41,20 @@ export default (cb: Function) => (...args: any) => (
'\n\n' +
`Missing ${RECEIVED_COLOR(`${data.length % keys.length}`)} arguments`,
);
});
}

return table.forEach(row =>
cb(interpolate(title, row), applyObjectParams(row, test)),
);
};
if (Error.captureStackTrace) {
Error.captureStackTrace(error, eachBind);
}

return cb(title, () => {
throw error;
});
}

return table.forEach(row =>
cb(interpolate(title, row), applyObjectParams(row, test)),
);
};

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