diff --git a/CHANGELOG.md b/CHANGELOG.md index b3e4e6ff26ef..7c560a26de7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Chore & Maintenance +* `[jest-jasemine2]` Add dependency on jest-each ([#6308](https://github.com/facebook/jest/pull/#6308)) * `[jest-each]` Move jest-each into core Jest ([#6278](https://github.com/facebook/jest/pull/6278)) ### Fixes diff --git a/integration-tests/__tests__/__snapshots__/each.test.js.snap b/integration-tests/__tests__/__snapshots__/each.test.js.snap index 40bc48c72317..9fd5481571a6 100644 --- a/integration-tests/__tests__/__snapshots__/each.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/each.test.js.snap @@ -18,7 +18,7 @@ exports[`shows error message when not enough arguments are supplied to tests 1`] Missing 1 arguments - at packages/jest-jasmine2/build/each.js:84:17 + at packages/jest-each/build/bind.js:81:17 " `; diff --git a/packages/jest-each/package.json b/packages/jest-each/package.json index c27eadd98b51..53e535df276e 100644 --- a/packages/jest-each/package.json +++ b/packages/jest-each/package.json @@ -14,5 +14,9 @@ "each" ], "author": "Matt Phillips (mattphillips)", - "license": "MIT" + "license": "MIT", + "dependencies": { + "chalk": "^2.0.1", + "pretty-format": "^23.0.0" + } } diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js new file mode 100644 index 000000000000..0df9276d660d --- /dev/null +++ b/packages/jest-each/src/bind.js @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2018-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. + * + * @flow + */ + +import util from 'util'; +import chalk from 'chalk'; +import pretty from 'pretty-format'; + +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)), + ); + } + + 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) { + 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`, + ); + }); + } + + 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); + + return () => test(...params); +}; + +const getHeadingKeys = (headings: string): Array => + headings.replace(/\s/g, '').split('|'); + +const buildTable = ( + data: Array, + rowSize: number, + keys: Array, +): Array => + Array.from({length: data.length / rowSize}) + .map((_, index) => data.slice(index * rowSize, index * rowSize + rowSize)) + .map(row => + row.reduce( + (acc, value, index) => Object.assign({}, acc, {[keys[index]]: value}), + {}, + ), + ); + +const interpolate = (title: string, data: any) => + Object.keys(data).reduce( + (acc, key) => acc.replace('$' + key, data[key]), + title, + ); + +const applyObjectParams = (obj: any, test: Function) => { + if (test.length > 1) return done => test(obj, done); + + return () => test(obj); +}; diff --git a/packages/jest-each/src/index.js b/packages/jest-each/src/index.js index f7bd69994ef6..7bfc789ee88b 100644 --- a/packages/jest-each/src/index.js +++ b/packages/jest-each/src/index.js @@ -1,3 +1,4 @@ +import bind from './bind'; import arrayEach from './array'; import templateEach from './template'; @@ -17,4 +18,6 @@ each.withGlobal = g => (...args) => { return arrayEach(g)(...args); }; +export {bind}; + export default each; diff --git a/packages/jest-jasmine2/package.json b/packages/jest-jasmine2/package.json index 1c820136c46e..ce048a9dd48f 100644 --- a/packages/jest-jasmine2/package.json +++ b/packages/jest-jasmine2/package.json @@ -13,6 +13,7 @@ "expect": "^23.0.0", "is-generator-fn": "^1.0.0", "jest-diff": "^23.0.0", + "jest-each": "^23.0.0", "jest-matcher-utils": "^23.0.0", "jest-message-util": "^23.0.0", "jest-snapshot": "^23.0.0", diff --git a/packages/jest-jasmine2/src/each.js b/packages/jest-jasmine2/src/each.js index 92fcacdb2c6e..282af6d1faf7 100644 --- a/packages/jest-jasmine2/src/each.js +++ b/packages/jest-jasmine2/src/each.js @@ -9,14 +9,7 @@ import type {Environment} from 'types/Environment'; -import util from 'util'; -import chalk from 'chalk'; -import pretty from 'pretty-format'; - -type Table = Array>; - -const EXPECTED_COLOR = chalk.green; -const RECEIVED_COLOR = chalk.red; +import {bind as bindEach} from 'jest-each'; export default (environment: Environment): void => { environment.global.it.each = bindEach(environment.global.it); @@ -26,74 +19,3 @@ export default (environment: Environment): void => { environment.global.xdescribe.each = bindEach(environment.global.xdescribe); environment.global.fdescribe.each = bindEach(environment.global.fdescribe); }; - -const bindEach = (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)), - ); - } - - 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) { - 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`, - ); - }); - } - - 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); - - return () => test(...params); -}; - -const getHeadingKeys = (headings: string): Array => - headings.replace(/\s/g, '').split('|'); - -const buildTable = ( - data: Array, - rowSize: number, - keys: Array, -): Array => - Array.from({length: data.length / rowSize}) - .map((_, index) => data.slice(index * rowSize, index * rowSize + rowSize)) - .map(row => - row.reduce( - (acc, value, index) => Object.assign({}, acc, {[keys[index]]: value}), - {}, - ), - ); - -const interpolate = (title: string, data: any) => - Object.keys(data).reduce( - (acc, key) => acc.replace('$' + key, data[key]), - title, - ); - -const applyObjectParams = (obj: any, test: Function) => { - if (test.length > 1) return done => test(obj, done); - - return () => test(obj); -};