diff --git a/CHANGELOG.md b/CHANGELOG.md index e4ed1c2d8212..65abdf7c8c0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/e2e/__tests__/__snapshots__/each.test.js.snap b/e2e/__tests__/__snapshots__/each.test.js.snap index 6a074807acbe..ad2dbde73203 100644 --- a/e2e/__tests__/__snapshots__/each.test.js.snap +++ b/e2e/__tests__/__snapshots__/each.test.js.snap @@ -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 " `; diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index 0df9276d660d..98e7c26c7c77 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -16,26 +16,23 @@ type Table = Array>; 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' + @@ -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, test: Function) => { if (params.length < test.length) return done => test(...params, done);