From 80e36c78c628fc97e4bd4b13dba87281ec5cb333 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 23 Oct 2018 14:22:49 +0100 Subject: [PATCH 1/5] Add empty array check to jest-each --- .../__snapshots__/array.test.js.snap | 40 +++++++++++++++++++ .../jest-each/src/__tests__/array.test.js | 13 ++++++ packages/jest-each/src/bind.js | 10 +++++ 3 files changed, 63 insertions(+) diff --git a/packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap b/packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap index 75cc81d64769..00c548f24eb8 100644 --- a/packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap +++ b/packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap @@ -1,5 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`jest-each .describe throws an error when called with an empty array 1`] = ` +"Error: \`.each\` called with an empty array of table data. +" +`; + exports[`jest-each .describe throws an error when not called with an array 1`] = ` "\`.each\` must be called with an Array or Tagged Template String. @@ -7,6 +12,11 @@ Instead was called with: undefined " `; +exports[`jest-each .describe.only throws an error when called with an empty array 1`] = ` +"Error: \`.each\` called with an empty array of table data. +" +`; + exports[`jest-each .describe.only throws an error when not called with an array 1`] = ` "\`.each\` must be called with an Array or Tagged Template String. @@ -14,6 +24,11 @@ Instead was called with: undefined " `; +exports[`jest-each .fdescribe throws an error when called with an empty array 1`] = ` +"Error: \`.each\` called with an empty array of table data. +" +`; + exports[`jest-each .fdescribe throws an error when not called with an array 1`] = ` "\`.each\` must be called with an Array or Tagged Template String. @@ -21,6 +36,11 @@ Instead was called with: undefined " `; +exports[`jest-each .fit throws an error when called with an empty array 1`] = ` +"Error: \`.each\` called with an empty array of table data. +" +`; + exports[`jest-each .fit throws an error when not called with an array 1`] = ` "\`.each\` must be called with an Array or Tagged Template String. @@ -28,6 +48,11 @@ Instead was called with: undefined " `; +exports[`jest-each .it throws an error when called with an empty array 1`] = ` +"Error: \`.each\` called with an empty array of table data. +" +`; + exports[`jest-each .it throws an error when not called with an array 1`] = ` "\`.each\` must be called with an Array or Tagged Template String. @@ -35,6 +60,11 @@ Instead was called with: undefined " `; +exports[`jest-each .it.only throws an error when called with an empty array 1`] = ` +"Error: \`.each\` called with an empty array of table data. +" +`; + exports[`jest-each .it.only throws an error when not called with an array 1`] = ` "\`.each\` must be called with an Array or Tagged Template String. @@ -42,6 +72,11 @@ Instead was called with: undefined " `; +exports[`jest-each .test throws an error when called with an empty array 1`] = ` +"Error: \`.each\` called with an empty array of table data. +" +`; + exports[`jest-each .test throws an error when not called with an array 1`] = ` "\`.each\` must be called with an Array or Tagged Template String. @@ -49,6 +84,11 @@ Instead was called with: undefined " `; +exports[`jest-each .test.only throws an error when called with an empty array 1`] = ` +"Error: \`.each\` called with an empty array of table data. +" +`; + exports[`jest-each .test.only throws an error when not called with an array 1`] = ` "\`.each\` must be called with an Array or Tagged Template String. diff --git a/packages/jest-each/src/__tests__/array.test.js b/packages/jest-each/src/__tests__/array.test.js index 594b25ef0161..95268afa597d 100644 --- a/packages/jest-each/src/__tests__/array.test.js +++ b/packages/jest-each/src/__tests__/array.test.js @@ -60,6 +60,19 @@ describe('jest-each', () => { ).toThrowErrorMatchingSnapshot(); }); + test('throws an error when called with an empty array', () => { + const globalTestMocks = getGlobalTestMocks(); + const eachObject = each.withGlobal(globalTestMocks)([]); + const testFunction = get(eachObject, keyPath); + + testFunction('expected string', noop); + const globalMock = get(globalTestMocks, keyPath); + + expect(() => + globalMock.mock.calls[0][1](), + ).toThrowErrorMatchingSnapshot(); + }); + test('calls global with given title', () => { const globalTestMocks = getGlobalTestMocks(); const eachObject = each.withGlobal(globalTestMocks)([[]]); diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index 9d78fc25dc81..b59711285b62 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -42,6 +42,16 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) => throw error; }); } + + if (tableArg.length === 0) { + const error = new ErrorWithStack( + 'Error: `.each` called with an empty array of table data.\n', + eachBind, + ); + return cb(title, () => { + throw error; + }); + } const table: Table = tableArg.every(Array.isArray) ? tableArg : tableArg.map(entry => [entry]); From 2eaf9903bb83917b8c62126f2c92f7cb6fcce423 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 23 Oct 2018 14:26:39 +0100 Subject: [PATCH 2/5] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d66593d8068..c868b024d08b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ ### Fixes +- `[jest-each]` Add empty array validation check ([#7249](https://github.com/facebook/jest/pull/7249)) - `[jest-cli]` Interrupt tests if interactive watch plugin key is pressed ([#7222](https://github.com/facebook/jest/pull/7222)) - `[jest-cli]` Fix coverage summary reporting ([#7058](https://github.com/facebook/jest/pull/7058)) - `[jest-each]` Add each array validation check ([#7033](https://github.com/facebook/jest/pull/7033)) From 56081b5f804c29cdd8b77c492d19d8037dd411cc Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 23 Oct 2018 15:46:05 +0100 Subject: [PATCH 3/5] Add empty string check to jest-each --- .../__snapshots__/template.test.js.snap | 40 +++++++++++++++++++ .../jest-each/src/__tests__/template.test.js | 15 +++++++ packages/jest-each/src/bind.js | 14 +++++++ 3 files changed, 69 insertions(+) diff --git a/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap b/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap index f99bb2662c27..e7e83464810c 100644 --- a/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap +++ b/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap @@ -1,5 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`jest-each .describe throws an error when called with an empty string 1`] = ` +"Error: \`.each\` called with an empty tagged template string of table data. +" +`; + exports[`jest-each .describe throws error when there are fewer arguments than headings over multiple rows 1`] = ` "Not enough arguments supplied for given headings: a | b | expected @@ -29,6 +34,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .describe.only throws an error when called with an empty string 1`] = ` +"Error: \`.each\` called with an empty tagged template string of table data. +" +`; + exports[`jest-each .describe.only throws error when there are fewer arguments than headings over multiple rows 1`] = ` "Not enough arguments supplied for given headings: a | b | expected @@ -58,6 +68,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .fdescribe throws an error when called with an empty string 1`] = ` +"Error: \`.each\` called with an empty tagged template string of table data. +" +`; + exports[`jest-each .fdescribe throws error when there are fewer arguments than headings over multiple rows 1`] = ` "Not enough arguments supplied for given headings: a | b | expected @@ -87,6 +102,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .fit throws an error when called with an empty string 1`] = ` +"Error: \`.each\` called with an empty tagged template string of table data. +" +`; + exports[`jest-each .fit throws error when there are fewer arguments than headings over multiple rows 1`] = ` "Not enough arguments supplied for given headings: a | b | expected @@ -116,6 +136,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .it throws an error when called with an empty string 1`] = ` +"Error: \`.each\` called with an empty tagged template string of table data. +" +`; + exports[`jest-each .it throws error when there are fewer arguments than headings over multiple rows 1`] = ` "Not enough arguments supplied for given headings: a | b | expected @@ -145,6 +170,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .it.only throws an error when called with an empty string 1`] = ` +"Error: \`.each\` called with an empty tagged template string of table data. +" +`; + exports[`jest-each .it.only throws error when there are fewer arguments than headings over multiple rows 1`] = ` "Not enough arguments supplied for given headings: a | b | expected @@ -174,6 +204,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .test throws an error when called with an empty string 1`] = ` +"Error: \`.each\` called with an empty tagged template string of table data. +" +`; + exports[`jest-each .test throws error when there are fewer arguments than headings over multiple rows 1`] = ` "Not enough arguments supplied for given headings: a | b | expected @@ -203,6 +238,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .test.only throws an error when called with an empty string 1`] = ` +"Error: \`.each\` called with an empty tagged template string of table data. +" +`; + exports[`jest-each .test.only throws error when there are fewer arguments than headings over multiple rows 1`] = ` "Not enough arguments supplied for given headings: a | b | expected diff --git a/packages/jest-each/src/__tests__/template.test.js b/packages/jest-each/src/__tests__/template.test.js index 30f368ef54ce..194e02b1d646 100644 --- a/packages/jest-each/src/__tests__/template.test.js +++ b/packages/jest-each/src/__tests__/template.test.js @@ -83,6 +83,21 @@ describe('jest-each', () => { expect(testCallBack).not.toHaveBeenCalled(); }); + test('throws an error when called with an empty string', () => { + const globalTestMocks = getGlobalTestMocks(); + const eachObject = each.withGlobal(globalTestMocks)` `; + const testFunction = get(eachObject, keyPath); + const testCallBack = jest.fn(); + testFunction('this will blow up :(', testCallBack); + + const globalMock = get(globalTestMocks, keyPath); + + expect(() => + globalMock.mock.calls[0][1](), + ).toThrowErrorMatchingSnapshot(); + expect(testCallBack).not.toHaveBeenCalled(); + }); + test('calls global with given title', () => { const globalTestMocks = getGlobalTestMocks(); const eachObject = each.withGlobal(globalTestMocks)` diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index b59711285b62..32a52d02dd30 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -43,6 +43,20 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) => }); } + if ( + tableArg.length === 1 && + typeof tableArg[0] === 'string' && + tableArg[0].trim() === '' + ) { + const error = new ErrorWithStack( + 'Error: `.each` called with an empty tagged template string of table data.\n', + eachBind, + ); + return cb(title, () => { + throw error; + }); + } + if (tableArg.length === 0) { const error = new ErrorWithStack( 'Error: `.each` called with an empty array of table data.\n', From 8768e581d17533bdb8ece7a558642734e91c84e7 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 23 Oct 2018 20:00:40 +0100 Subject: [PATCH 4/5] Fix types in error messages --- .../__snapshots__/array.test.js.snap | 32 +++++++++---------- .../__snapshots__/template.test.js.snap | 16 +++++----- packages/jest-each/src/bind.js | 6 ++-- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap b/packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap index 00c548f24eb8..d779a5e5df50 100644 --- a/packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap +++ b/packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap @@ -1,96 +1,96 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`jest-each .describe throws an error when called with an empty array 1`] = ` -"Error: \`.each\` called with an empty array of table data. +"Error: \`.each\` called with an empty Array of table data. " `; exports[`jest-each .describe throws an error when not called with an array 1`] = ` -"\`.each\` must be called with an Array or Tagged Template String. +"\`.each\` must be called with an Array or Tagged Template Literal. Instead was called with: undefined " `; exports[`jest-each .describe.only throws an error when called with an empty array 1`] = ` -"Error: \`.each\` called with an empty array of table data. +"Error: \`.each\` called with an empty Array of table data. " `; exports[`jest-each .describe.only throws an error when not called with an array 1`] = ` -"\`.each\` must be called with an Array or Tagged Template String. +"\`.each\` must be called with an Array or Tagged Template Literal. Instead was called with: undefined " `; exports[`jest-each .fdescribe throws an error when called with an empty array 1`] = ` -"Error: \`.each\` called with an empty array of table data. +"Error: \`.each\` called with an empty Array of table data. " `; exports[`jest-each .fdescribe throws an error when not called with an array 1`] = ` -"\`.each\` must be called with an Array or Tagged Template String. +"\`.each\` must be called with an Array or Tagged Template Literal. Instead was called with: undefined " `; exports[`jest-each .fit throws an error when called with an empty array 1`] = ` -"Error: \`.each\` called with an empty array of table data. +"Error: \`.each\` called with an empty Array of table data. " `; exports[`jest-each .fit throws an error when not called with an array 1`] = ` -"\`.each\` must be called with an Array or Tagged Template String. +"\`.each\` must be called with an Array or Tagged Template Literal. Instead was called with: undefined " `; exports[`jest-each .it throws an error when called with an empty array 1`] = ` -"Error: \`.each\` called with an empty array of table data. +"Error: \`.each\` called with an empty Array of table data. " `; exports[`jest-each .it throws an error when not called with an array 1`] = ` -"\`.each\` must be called with an Array or Tagged Template String. +"\`.each\` must be called with an Array or Tagged Template Literal. Instead was called with: undefined " `; exports[`jest-each .it.only throws an error when called with an empty array 1`] = ` -"Error: \`.each\` called with an empty array of table data. +"Error: \`.each\` called with an empty Array of table data. " `; exports[`jest-each .it.only throws an error when not called with an array 1`] = ` -"\`.each\` must be called with an Array or Tagged Template String. +"\`.each\` must be called with an Array or Tagged Template Literal. Instead was called with: undefined " `; exports[`jest-each .test throws an error when called with an empty array 1`] = ` -"Error: \`.each\` called with an empty array of table data. +"Error: \`.each\` called with an empty Array of table data. " `; exports[`jest-each .test throws an error when not called with an array 1`] = ` -"\`.each\` must be called with an Array or Tagged Template String. +"\`.each\` must be called with an Array or Tagged Template Literal. Instead was called with: undefined " `; exports[`jest-each .test.only throws an error when called with an empty array 1`] = ` -"Error: \`.each\` called with an empty array of table data. +"Error: \`.each\` called with an empty Array of table data. " `; exports[`jest-each .test.only throws an error when not called with an array 1`] = ` -"\`.each\` must be called with an Array or Tagged Template String. +"\`.each\` must be called with an Array or Tagged Template Literal. Instead was called with: undefined " diff --git a/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap b/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap index e7e83464810c..2a86aec3807d 100644 --- a/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap +++ b/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`jest-each .describe throws an error when called with an empty string 1`] = ` -"Error: \`.each\` called with an empty tagged template string of table data. +"Error: \`.each\` called with an empty Tagged Template Literal of table data. " `; @@ -35,7 +35,7 @@ Missing 2 arguments" `; exports[`jest-each .describe.only throws an error when called with an empty string 1`] = ` -"Error: \`.each\` called with an empty tagged template string of table data. +"Error: \`.each\` called with an empty Tagged Template Literal of table data. " `; @@ -69,7 +69,7 @@ Missing 2 arguments" `; exports[`jest-each .fdescribe throws an error when called with an empty string 1`] = ` -"Error: \`.each\` called with an empty tagged template string of table data. +"Error: \`.each\` called with an empty Tagged Template Literal of table data. " `; @@ -103,7 +103,7 @@ Missing 2 arguments" `; exports[`jest-each .fit throws an error when called with an empty string 1`] = ` -"Error: \`.each\` called with an empty tagged template string of table data. +"Error: \`.each\` called with an empty Tagged Template Literal of table data. " `; @@ -137,7 +137,7 @@ Missing 2 arguments" `; exports[`jest-each .it throws an error when called with an empty string 1`] = ` -"Error: \`.each\` called with an empty tagged template string of table data. +"Error: \`.each\` called with an empty Tagged Template Literal of table data. " `; @@ -171,7 +171,7 @@ Missing 2 arguments" `; exports[`jest-each .it.only throws an error when called with an empty string 1`] = ` -"Error: \`.each\` called with an empty tagged template string of table data. +"Error: \`.each\` called with an empty Tagged Template Literal of table data. " `; @@ -205,7 +205,7 @@ Missing 2 arguments" `; exports[`jest-each .test throws an error when called with an empty string 1`] = ` -"Error: \`.each\` called with an empty tagged template string of table data. +"Error: \`.each\` called with an empty Tagged Template Literal of table data. " `; @@ -239,7 +239,7 @@ Missing 2 arguments" `; exports[`jest-each .test.only throws an error when called with an empty string 1`] = ` -"Error: \`.each\` called with an empty tagged template string of table data. +"Error: \`.each\` called with an empty Tagged Template Literal of table data. " `; diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index 32a52d02dd30..600ff17b7e2d 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -31,7 +31,7 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) => if (!Array.isArray(tableArg)) { const error = new ErrorWithStack( - '`.each` must be called with an Array or Tagged Template String.\n\n' + + '`.each` must be called with an Array or Tagged Template Literal.\n\n' + `Instead was called with: ${pretty(tableArg, { maxDepth: 1, min: true, @@ -49,7 +49,7 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) => tableArg[0].trim() === '' ) { const error = new ErrorWithStack( - 'Error: `.each` called with an empty tagged template string of table data.\n', + 'Error: `.each` called with an empty Tagged Template Literal of table data.\n', eachBind, ); return cb(title, () => { @@ -59,7 +59,7 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) => if (tableArg.length === 0) { const error = new ErrorWithStack( - 'Error: `.each` called with an empty array of table data.\n', + 'Error: `.each` called with an empty Array of table data.\n', eachBind, ); return cb(title, () => { From 7f68746235e35ba63badb31e67dd4f20b6504e90 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Wed, 24 Oct 2018 09:39:39 +0100 Subject: [PATCH 5/5] Add tagged template literal with no data validation check --- .../__snapshots__/template.test.js.snap | 40 +++++++++++++++++++ .../jest-each/src/__tests__/template.test.js | 17 ++++++++ packages/jest-each/src/bind.js | 24 +++++++---- 3 files changed, 74 insertions(+), 7 deletions(-) diff --git a/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap b/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap index 2a86aec3807d..f5097aa6218d 100644 --- a/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap +++ b/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap @@ -34,6 +34,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .describe throws error when there are no arguments for given headings 1`] = ` +"Error: \`.each\` called with a Tagged Template Literal with no data, remember to interpolate with \${expression} syntax. +" +`; + exports[`jest-each .describe.only throws an error when called with an empty string 1`] = ` "Error: \`.each\` called with an empty Tagged Template Literal of table data. " @@ -68,6 +73,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .describe.only throws error when there are no arguments for given headings 1`] = ` +"Error: \`.each\` called with a Tagged Template Literal with no data, remember to interpolate with \${expression} syntax. +" +`; + exports[`jest-each .fdescribe throws an error when called with an empty string 1`] = ` "Error: \`.each\` called with an empty Tagged Template Literal of table data. " @@ -102,6 +112,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .fdescribe throws error when there are no arguments for given headings 1`] = ` +"Error: \`.each\` called with a Tagged Template Literal with no data, remember to interpolate with \${expression} syntax. +" +`; + exports[`jest-each .fit throws an error when called with an empty string 1`] = ` "Error: \`.each\` called with an empty Tagged Template Literal of table data. " @@ -136,6 +151,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .fit throws error when there are no arguments for given headings 1`] = ` +"Error: \`.each\` called with a Tagged Template Literal with no data, remember to interpolate with \${expression} syntax. +" +`; + exports[`jest-each .it throws an error when called with an empty string 1`] = ` "Error: \`.each\` called with an empty Tagged Template Literal of table data. " @@ -170,6 +190,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .it throws error when there are no arguments for given headings 1`] = ` +"Error: \`.each\` called with a Tagged Template Literal with no data, remember to interpolate with \${expression} syntax. +" +`; + exports[`jest-each .it.only throws an error when called with an empty string 1`] = ` "Error: \`.each\` called with an empty Tagged Template Literal of table data. " @@ -204,6 +229,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .it.only throws error when there are no arguments for given headings 1`] = ` +"Error: \`.each\` called with a Tagged Template Literal with no data, remember to interpolate with \${expression} syntax. +" +`; + exports[`jest-each .test throws an error when called with an empty string 1`] = ` "Error: \`.each\` called with an empty Tagged Template Literal of table data. " @@ -238,6 +268,11 @@ Received: Missing 2 arguments" `; +exports[`jest-each .test throws error when there are no arguments for given headings 1`] = ` +"Error: \`.each\` called with a Tagged Template Literal with no data, remember to interpolate with \${expression} syntax. +" +`; + exports[`jest-each .test.only throws an error when called with an empty string 1`] = ` "Error: \`.each\` called with an empty Tagged Template Literal of table data. " @@ -271,3 +306,8 @@ Received: Missing 2 arguments" `; + +exports[`jest-each .test.only throws error when there are no arguments for given headings 1`] = ` +"Error: \`.each\` called with a Tagged Template Literal with no data, remember to interpolate with \${expression} syntax. +" +`; diff --git a/packages/jest-each/src/__tests__/template.test.js b/packages/jest-each/src/__tests__/template.test.js index 194e02b1d646..ed5f9664d2eb 100644 --- a/packages/jest-each/src/__tests__/template.test.js +++ b/packages/jest-each/src/__tests__/template.test.js @@ -46,6 +46,23 @@ describe('jest-each', () => { ['describe', 'only'], ].forEach(keyPath => { describe(`.${keyPath.join('.')}`, () => { + test('throws error when there are no arguments for given headings', () => { + const globalTestMocks = getGlobalTestMocks(); + const eachObject = each.withGlobal(globalTestMocks)` + a | b | expected + `; + const testFunction = get(eachObject, keyPath); + const testCallBack = jest.fn(); + testFunction('this will blow up :(', testCallBack); + + const globalMock = get(globalTestMocks, keyPath); + + expect(() => + globalMock.mock.calls[0][1](), + ).toThrowErrorMatchingSnapshot(); + expect(testCallBack).not.toHaveBeenCalled(); + }); + test('throws error when there are fewer arguments than headings when given one row', () => { const globalTestMocks = getGlobalTestMocks(); const eachObject = each.withGlobal(globalTestMocks)` diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index 600ff17b7e2d..d2a7d1ebd550 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -43,13 +43,19 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) => }); } - if ( - tableArg.length === 1 && - typeof tableArg[0] === 'string' && - tableArg[0].trim() === '' - ) { + if (isTaggedTemplateLiteral(tableArg)) { + if (isEmptyString(tableArg[0])) { + const error = new ErrorWithStack( + 'Error: `.each` called with an empty Tagged Template Literal of table data.\n', + eachBind, + ); + return cb(title, () => { + throw error; + }); + } + const error = new ErrorWithStack( - 'Error: `.each` called with an empty Tagged Template Literal of table data.\n', + 'Error: `.each` called with a Tagged Template Literal with no data, remember to interpolate with ${expression} syntax.\n', eachBind, ); return cb(title, () => { @@ -57,7 +63,7 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) => }); } - if (tableArg.length === 0) { + if (isEmptyTable(tableArg)) { const error = new ErrorWithStack( 'Error: `.each` called with an empty Array of table data.\n', eachBind, @@ -115,6 +121,10 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) => ); }; +const isTaggedTemplateLiteral = array => array.raw !== undefined; +const isEmptyTable = table => table.length === 0; +const isEmptyString = str => typeof str === 'string' && str.trim() === ''; + const getPrettyIndexes = placeholders => placeholders.reduce( (indexes, placeholder, index) =>