From 23076435185989d81d8d353a57ecddd7649d41da Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 29 May 2018 22:00:44 +0100 Subject: [PATCH] [jest-each] Refactor logic (#6345) --- CHANGELOG.md | 1 + .../__snapshots__/template.test.js.snap | 232 ++++++++++++++---- .../jest-each/src/__tests__/array.test.js | 8 + .../jest-each/src/__tests__/index.test.js | 8 + .../jest-each/src/__tests__/template.test.js | 8 + packages/jest-each/src/array.js | 40 --- packages/jest-each/src/index.js | 49 +++- packages/jest-each/src/template.js | 88 ------- 8 files changed, 245 insertions(+), 189 deletions(-) delete mode 100644 packages/jest-each/src/array.js delete mode 100644 packages/jest-each/src/template.js diff --git a/CHANGELOG.md b/CHANGELOG.md index f1c33291235b..e4ed1c2d8212 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### Chore & Maintenance +* `[jest-each]` Refactor each to use shared implementation with core ([#6345](https://github.com/facebook/jest/pull/6345)) * `[jest-each]` Update jest-each docs for serialising values into titles ([#6337](https://github.com/facebook/jest/pull/6337)) * `[jest-circus]` Add dependency on jest-each ([#6309](https://github.com/facebook/jest/pull/6309)) * `[filenames]` Rename "integration-tests" to "e2e" ([#6315](https://github.com/facebook/jest/pull/6315)) 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 0d04a0bafa8c..f99bb2662c27 100644 --- a/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap +++ b/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap @@ -1,97 +1,233 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`jest-each .describe throws error when there are fewer arguments than headings over multiple rows 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1,1,1,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, + 1, + 1, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .describe throws error when there are fewer arguments than headings when given one row 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .describe.only throws error when there are fewer arguments than headings over multiple rows 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1,1,1,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, + 1, + 1, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .describe.only throws error when there are fewer arguments than headings when given one row 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .fdescribe throws error when there are fewer arguments than headings over multiple rows 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1,1,1,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, + 1, + 1, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .fdescribe throws error when there are fewer arguments than headings when given one row 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .fit throws error when there are fewer arguments than headings over multiple rows 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1,1,1,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, + 1, + 1, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .fit throws error when there are fewer arguments than headings when given one row 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .it throws error when there are fewer arguments than headings over multiple rows 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1,1,1,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, + 1, + 1, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .it throws error when there are fewer arguments than headings when given one row 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .it.only throws error when there are fewer arguments than headings over multiple rows 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1,1,1,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, + 1, + 1, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .it.only throws error when there are fewer arguments than headings when given one row 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .test throws error when there are fewer arguments than headings over multiple rows 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1,1,1,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, + 1, + 1, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .test throws error when there are fewer arguments than headings when given one row 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .test.only throws error when there are fewer arguments than headings over multiple rows 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1,1,1,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, + 1, + 1, + 1, +] + +Missing 2 arguments" `; exports[`jest-each .test.only throws error when there are fewer arguments than headings when given one row 1`] = ` -"Tagged Template Literal test error: -Not enough arguments supplied for given headings: a | b | expected -Received: 0,1" +"Not enough arguments supplied for given headings: +a | b | expected + +Received: +Array [ + 0, + 1, +] + +Missing 2 arguments" `; diff --git a/packages/jest-each/src/__tests__/array.test.js b/packages/jest-each/src/__tests__/array.test.js index eb24d76efecf..a4a04cd0e4d7 100644 --- a/packages/jest-each/src/__tests__/array.test.js +++ b/packages/jest-each/src/__tests__/array.test.js @@ -1,3 +1,11 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + import each from '../'; const noop = () => {}; diff --git a/packages/jest-each/src/__tests__/index.test.js b/packages/jest-each/src/__tests__/index.test.js index 6c63790edde2..aa6ded3a4497 100644 --- a/packages/jest-each/src/__tests__/index.test.js +++ b/packages/jest-each/src/__tests__/index.test.js @@ -1,3 +1,11 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + import each from '../'; describe('array', () => { diff --git a/packages/jest-each/src/__tests__/template.test.js b/packages/jest-each/src/__tests__/template.test.js index 2c7da7d1d045..2e5adeeae1f8 100644 --- a/packages/jest-each/src/__tests__/template.test.js +++ b/packages/jest-each/src/__tests__/template.test.js @@ -1,3 +1,11 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + import each from '../'; const noop = () => {}; diff --git a/packages/jest-each/src/array.js b/packages/jest-each/src/array.js deleted file mode 100644 index 43e84b54c913..000000000000 --- a/packages/jest-each/src/array.js +++ /dev/null @@ -1,40 +0,0 @@ -import util from 'util'; - -export default defaultGlobal => parameterRows => { - const tests = parameterisedTests(parameterRows); - - const globalTest = defaultGlobal.test; - const test = tests(globalTest); - test.skip = tests(globalTest.skip); - test.only = tests(globalTest.only); - - const globalIt = defaultGlobal.it; - const it = tests(globalIt); - it.skip = tests(globalIt.skip); - it.only = tests(globalIt.only); - - const xtest = tests(defaultGlobal.xtest); - const xit = tests(defaultGlobal.xit); - const fit = tests(defaultGlobal.fit); - - const globalDescribe = defaultGlobal.describe; - const describe = tests(globalDescribe); - describe.skip = tests(globalDescribe.skip); - describe.only = tests(globalDescribe.only); - const fdescribe = tests(defaultGlobal.fdescribe); - const xdescribe = tests(defaultGlobal.xdescribe); - - return {describe, fdescribe, fit, it, test, xdescribe, xit, xtest}; -}; - -const parameterisedTests = parameterRows => globalCb => (title, test) => { - parameterRows.forEach(params => - globalCb(util.format(title, ...params), applyTestParams(params, test)), - ); -}; - -const applyTestParams = (params, test) => { - if (params.length < test.length) return done => test(...params, done); - - return () => test(...params); -}; diff --git a/packages/jest-each/src/index.js b/packages/jest-each/src/index.js index faa1310b8a5c..ae04951f9d1a 100644 --- a/packages/jest-each/src/index.js +++ b/packages/jest-each/src/index.js @@ -8,25 +8,48 @@ */ import bind from './bind'; -import arrayEach from './array'; -import templateEach from './template'; -const each = (...args: Array) => { - if (args.length > 1) { - return templateEach(global)(...args); - } - - return arrayEach(global)(...args); +type GlobalCallbacks = { + test(title: string, test: Function): void, + xtest(title: string, test: Function): void, + it(title: string, test: Function): void, + fit(title: string, test: Function): void, + xit(title: string, test: Function): void, + describe(title: string, test: Function): void, + fdescribe(title: string, test: Function): void, + xdescribe(title: string, test: Function): void, }; -each.withGlobal = g => (...args: Array) => { - if (args.length > 1) { - return templateEach(g)(...args); - } +const install = (g: GlobalCallbacks, ...args: Array) => { + const test = (title: string, test: Function) => + bind(g.test)(...args)(title, test); + test.skip = bind(g.test.skip)(...args); + test.only = bind(g.test.only)(...args); + + const it = (title: string, test: Function) => + bind(g.it)(...args)(title, test); + it.skip = bind(g.it.skip)(...args); + it.only = bind(g.it.only)(...args); + + const xit = bind(g.xit)(...args); + const fit = bind(g.fit)(...args); + const xtest = bind(g.xtest)(...args); - return arrayEach(g)(...args); + const describe = (title: string, suite: Function) => + bind(g.describe)(...args)(title, suite); + describe.skip = bind(g.describe.skip)(...args); + describe.only = bind(g.describe.only)(...args); + const fdescribe = bind(g.fdescribe)(...args); + const xdescribe = bind(g.xdescribe)(...args); + + return {describe, fdescribe, fit, it, test, xdescribe, xit, xtest}; }; +const each = (...args: Array) => install(global, ...args); + +each.withGlobal = (g: GlobalCallbacks) => (...args: Array) => + install(g, ...args); + export {bind}; export default each; diff --git a/packages/jest-each/src/template.js b/packages/jest-each/src/template.js deleted file mode 100644 index d741a1f5334f..000000000000 --- a/packages/jest-each/src/template.js +++ /dev/null @@ -1,88 +0,0 @@ -export default defaultGlobal => ([headings], ...data) => { - const keys = getHeadingKeys(headings); - - const keysLength = keys.length; - - if (data.length % keysLength !== 0) { - const errorFunction = notEnoughDataError(keys, data); - - const test = errorFunction(defaultGlobal.test); - test.only = errorFunction(defaultGlobal.test.only); - - const it = errorFunction(defaultGlobal.it); - it.only = errorFunction(defaultGlobal.it.only); - - const fit = errorFunction(defaultGlobal.fit); - - const describe = errorFunction(defaultGlobal.describe); - describe.only = errorFunction(defaultGlobal.describe.only); - - const fdescribe = errorFunction(defaultGlobal.fdescribe); - - return {describe, fdescribe, fit, it, test}; - } - - const parameterRows = Array.from({length: data.length / keysLength}) - .map((_, index) => - data.slice(index * keysLength, index * keysLength + keysLength), - ) - .map(row => - row.reduce( - (acc, value, index) => Object.assign({}, acc, {[keys[index]]: value}), - {}, - ), - ); - - const tests = parameterisedTests(parameterRows); - - const globalTest = defaultGlobal.test; - const test = tests(globalTest); - test.skip = tests(globalTest.skip); - test.only = tests(globalTest.only); - - const globalIt = defaultGlobal.it; - const it = tests(globalIt); - it.skip = tests(globalIt.skip); - it.only = tests(globalIt.only); - - const xtest = tests(defaultGlobal.xtest); - const xit = tests(defaultGlobal.xit); - const fit = tests(defaultGlobal.fit); - - const globalDescribe = defaultGlobal.describe; - const describe = tests(globalDescribe); - describe.skip = tests(globalDescribe.skip); - describe.only = tests(globalDescribe.only); - const fdescribe = tests(defaultGlobal.fdescribe); - const xdescribe = tests(defaultGlobal.xdescribe); - - return {describe, fdescribe, fit, it, test, xdescribe, xit, xtest}; -}; - -const notEnoughDataError = (keys, data) => cb => title => - cb(title, () => { - throw new Error( - `Tagged Template Literal test error:\nNot enough arguments supplied for given headings: ${keys.join( - ' | ', - )}\nReceived: ${data}`, - ); - }); - -const getHeadingKeys = headings => headings.replace(/\s/g, '').split('|'); - -const parameterisedTests = parameterRows => globalCb => (title, test) => { - parameterRows.forEach(params => - globalCb(interpolate(title, params), applyTestParams(params, test)), - ); -}; - -const interpolate = (title, data) => { - const keys = Object.keys(data); - return keys.reduce((acc, key) => acc.replace('$' + key, data[key]), title); -}; - -const applyTestParams = (params, test) => { - if (test.length > 1) return done => test(params, done); - - return () => test(params); -};