From be7c5d7f1401239a9cb183d992ec584ff87541a6 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 29 May 2018 23:13:24 +0200 Subject: [PATCH 1/3] Make sure invalid jest-each points to user code --- CHANGELOG.md | 1 + e2e/__tests__/__snapshots__/each.test.js.snap | 11 +++++++++- packages/jest-each/src/bind.js | 20 ++++++++++--------- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4ed1c2d8212..9c3ab92764a2 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 * `[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..c81850d52dcd 100644 --- a/e2e/__tests__/__snapshots__/each.test.js.snap +++ b/e2e/__tests__/__snapshots__/each.test.js.snap @@ -29,7 +29,16 @@ 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 packages/jest-each/build/bind.js:80:23 + 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..e4bbbe8e6de4 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -34,16 +34,18 @@ export default (cb: Function) => (...args: any) => ( const table = buildTable(data, keys.length, keys); if (data.length % keys.length !== 0) { + const error = new Error( + 'Not enough arguments supplied for given headings:\n' + + EXPECTED_COLOR(keys.join(' | ')) + + '\n\n' + + 'Received:\n' + + RECEIVED_COLOR(pretty(data)) + + '\n\n' + + `Missing ${RECEIVED_COLOR(`${data.length % keys.length}`)} arguments`, + ); + return cb(title, () => { - throw new Error( - 'Not enough arguments supplied for given headings:\n' + - EXPECTED_COLOR(keys.join(' | ')) + - '\n\n' + - 'Received:\n' + - RECEIVED_COLOR(pretty(data)) + - '\n\n' + - `Missing ${RECEIVED_COLOR(`${data.length % keys.length}`)} arguments`, - ); + throw error; }); } From c476df8fab11b7ed609180240845239c203ea9b5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 29 May 2018 23:15:40 +0200 Subject: [PATCH 2/3] link to pr --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c3ab92764a2..65abdf7c8c0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ ### Fixes -* `[jest-each]` Make sure invalid arguments to `each` points back to the user's code +* `[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)) From b7deda91c8c0f5191bf498ebdd35a34e3045cf29 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 29 May 2018 23:36:56 +0200 Subject: [PATCH 3/3] Remove bind from trace --- e2e/__tests__/__snapshots__/each.test.js.snap | 1 - packages/jest-each/src/bind.js | 72 ++++++++++--------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/e2e/__tests__/__snapshots__/each.test.js.snap b/e2e/__tests__/__snapshots__/each.test.js.snap index c81850d52dcd..ad2dbde73203 100644 --- a/e2e/__tests__/__snapshots__/each.test.js.snap +++ b/e2e/__tests__/__snapshots__/each.test.js.snap @@ -37,7 +37,6 @@ exports[`shows error message when not enough arguments are supplied to tests 1`] 10 | \${true} | \${true} 11 | \${true} | - at packages/jest-each/build/bind.js:80:23 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 e4bbbe8e6de4..98e7c26c7c77 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -16,43 +16,45 @@ 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]; +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 keys = getHeadingKeys(templateStrings[0]); + const table = buildTable(data, keys.length, keys); + + if (data.length % keys.length !== 0) { + const error = new Error( + 'Not enough arguments supplied for given headings:\n' + + EXPECTED_COLOR(keys.join(' | ')) + + '\n\n' + + 'Received:\n' + + RECEIVED_COLOR(pretty(data)) + + '\n\n' + + `Missing ${RECEIVED_COLOR(`${data.length % keys.length}`)} arguments`, + ); + + if (Error.captureStackTrace) { + Error.captureStackTrace(error, eachBind); + } + + return cb(title, () => { + throw error; + }); + } + return table.forEach(row => - cb(util.format(title, ...row), applyRestParams(row, test)), - ); - } - - const templateStrings = args[0]; - const data = args.slice(1); - - const keys = getHeadingKeys(templateStrings[0]); - const table = buildTable(data, keys.length, keys); - - if (data.length % keys.length !== 0) { - const error = new Error( - 'Not enough arguments supplied for given headings:\n' + - EXPECTED_COLOR(keys.join(' | ')) + - '\n\n' + - 'Received:\n' + - RECEIVED_COLOR(pretty(data)) + - '\n\n' + - `Missing ${RECEIVED_COLOR(`${data.length % keys.length}`)} arguments`, + cb(interpolate(title, row), applyObjectParams(row, test)), ); - - 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);