Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move jest-jasemine2 each code to jest-each #6308

Merged
merged 6 commits into from
May 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be good to point to user code here, but that can be fixed later


"
`;
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-each/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
"each"
],
"author": "Matt Phillips (mattphillips)",
"license": "MIT"
"license": "MIT",
"dependencies": {
"chalk": "^2.0.1",
"pretty-format": "^23.0.0"
}
}
88 changes: 88 additions & 0 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
@@ -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<Array<any>>;

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<any>, test: Function) => {
if (params.length < test.length) return done => test(...params, done);

return () => test(...params);
};

const getHeadingKeys = (headings: string): Array<string> =>
headings.replace(/\s/g, '').split('|');

const buildTable = (
data: Array<any>,
rowSize: number,
keys: Array<string>,
): Array<any> =>
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);
};
3 changes: 3 additions & 0 deletions packages/jest-each/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import bind from './bind';
import arrayEach from './array';
import templateEach from './template';

Expand All @@ -17,4 +18,6 @@ each.withGlobal = g => (...args) => {
return arrayEach(g)(...args);
};

export {bind};

export default each;
1 change: 1 addition & 0 deletions packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
80 changes: 1 addition & 79 deletions packages/jest-jasmine2/src/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<any>>;

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);
Expand All @@ -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<any>, test: Function) => {
if (params.length < test.length) return done => test(...params, done);

return () => test(...params);
};

const getHeadingKeys = (headings: string): Array<string> =>
headings.replace(/\s/g, '').split('|');

const buildTable = (
data: Array<any>,
rowSize: number,
keys: Array<string>,
): Array<any> =>
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);
};