From 71b8f4d12d88123336d09a31714f9337d831ab84 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 31 May 2018 22:19:58 +0100 Subject: [PATCH 1/3] Add pluralize to missing arguments error --- e2e/__tests__/__snapshots__/each.test.js.snap | 28 +++++++++++++++++-- e2e/each/__tests__/each-exception.test.js | 10 +++++++ packages/jest-each/src/bind.js | 12 ++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/e2e/__tests__/__snapshots__/each.test.js.snap b/e2e/__tests__/__snapshots__/each.test.js.snap index fa69c42a75f1..0300453130ce 100644 --- a/e2e/__tests__/__snapshots__/each.test.js.snap +++ b/e2e/__tests__/__snapshots__/each.test.js.snap @@ -13,9 +13,10 @@ exports[`runs only the describe.only.each tests 1`] = ` exports[`shows error message when not enough arguments are supplied to tests 1`] = ` "FAIL __tests__/each-exception.test.js + ✕ throws exception when one argument too few are supplied $left == $right ✕ throws exception when not enough arguments are supplied $left == $right - ● throws exception when not enough arguments are supplied $left == $right + ● throws exception when one argument too few are supplied $left == $right Not enough arguments supplied for given headings: left | right @@ -27,7 +28,7 @@ exports[`shows error message when not enough arguments are supplied to tests 1`] true, ] - Missing 1 arguments + Missing 1 argument 6 | */ 7 | @@ -39,6 +40,29 @@ exports[`shows error message when not enough arguments are supplied to tests 1`] at __tests__/each-exception.test.js:8:1 + ● throws exception when not enough arguments are supplied $left == $right + + Not enough arguments supplied for given headings: + left | right | up | down + + Received: + Array [ + true, + true, + ] + + Missing 2 arguments + + 17 | ); + 18 | + > 19 | it.each\` + | ^ + 20 | left | right | up | down + 21 | \${true} | \${true} + 22 | \`( + + at __tests__/each-exception.test.js:19:1 + " `; diff --git a/e2e/each/__tests__/each-exception.test.js b/e2e/each/__tests__/each-exception.test.js index 6920a828aa27..9f052f5de724 100644 --- a/e2e/each/__tests__/each-exception.test.js +++ b/e2e/each/__tests__/each-exception.test.js @@ -9,6 +9,16 @@ it.each` left | right ${true} | ${true} ${true} +`( + 'throws exception when one argument too few are supplied $left == $right', + ({left, right}) => { + expect(left).toBe(right); + } +); + +it.each` + left | right | up | down + ${true} | ${true} `( 'throws exception when not enough arguments are supplied $left == $right', ({left, right}) => { diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index fc3103ce8a62..e51675630b0a 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -34,7 +34,9 @@ export default (cb: Function) => (...args: any) => const keys = getHeadingKeys(templateStrings[0]); const table = buildTable(data, keys.length, keys); - if (data.length % keys.length !== 0) { + const missingData = data.length % keys.length; + + if (missingData > 0) { const error = new Error( 'Not enough arguments supplied for given headings:\n' + EXPECTED_COLOR(keys.join(' | ')) + @@ -42,7 +44,10 @@ export default (cb: Function) => (...args: any) => 'Received:\n' + RECEIVED_COLOR(pretty(data)) + '\n\n' + - `Missing ${RECEIVED_COLOR(`${data.length % keys.length}`)} arguments`, + `Missing ${RECEIVED_COLOR(missingData.toString())} ${pluralize( + 'argument', + missingData, + )}`, ); if (Error.captureStackTrace) { @@ -98,3 +103,6 @@ const applyObjectParams = (obj: any, test: Function) => { return () => test(obj); }; + +const pluralize = (word: string, count: number) => + word + (count === 1 ? '' : 's'); From a0eb69f51ae54357fd246ebde125944656bbb257 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 31 May 2018 22:22:33 +0100 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba166e2b1565..b626981452a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,8 @@ ### Fixes -- `[jest-each]` Stop test title concatenating extra args ([##6346](https://github.com/facebook/jest/pull/#6346)) +- `[jest-each]` Fix pluralising missing arguments error ([#6369](https://github.com/facebook/jest/pull/#6369)) +- `[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)) From 83c7bd8f07fd8af87463e49378e0276bf1591c48 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Fri, 1 Jun 2018 10:09:05 +0100 Subject: [PATCH 3/3] Fix pr links --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b626981452a6..fadab775ac07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,8 +12,8 @@ ### Fixes -- `[jest-each]` Fix pluralising missing arguments error ([#6369](https://github.com/facebook/jest/pull/#6369)) -- `[jest-each]` Stop test title concatenating extra args ([#6346](https://github.com/facebook/jest/pull/#6346)) +- `[jest-each]` Fix pluralising missing arguments error ([#6369](https://github.com/facebook/jest/pull/6369)) +- `[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))