Skip to content

Commit fdc272a

Browse files
mattphillipsSimenB
authored andcommitted
Move jest-jasemine2 each code to jest-each (jestjs#6308)
* Add chalk and pretty-format dependenecies to jest-each * Add bind logic to jest-each * Add jest-each dependency to jest-jasmeine2 * Replace jest-jasmine2 each logic with jest-each * Fix broken snapshot * Update changelog
1 parent 8816df7 commit fdc272a

File tree

7 files changed

+100
-81
lines changed

7 files changed

+100
-81
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Chore & Maintenance
44

5+
* `[jest-jasemine2]` Add dependency on jest-each ([#6308](https://github.com/facebook/jest/pull/#6308))
56
* `[jest-each]` Move jest-each into core Jest ([#6278](https://github.com/facebook/jest/pull/6278))
67

78
### Fixes

integration-tests/__tests__/__snapshots__/each.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ exports[`shows error message when not enough arguments are supplied to tests 1`]
1818
1919
Missing 1 arguments
2020
21-
at packages/jest-jasmine2/build/each.js:84:17
21+
at packages/jest-each/build/bind.js:81:17
2222
2323
"
2424
`;

packages/jest-each/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
"each"
1515
],
1616
"author": "Matt Phillips (mattphillips)",
17-
"license": "MIT"
17+
"license": "MIT",
18+
"dependencies": {
19+
"chalk": "^2.0.1",
20+
"pretty-format": "^23.0.0"
21+
}
1822
}

packages/jest-each/src/bind.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright (c) 2018-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import util from 'util';
11+
import chalk from 'chalk';
12+
import pretty from 'pretty-format';
13+
14+
type Table = Array<Array<any>>;
15+
16+
const EXPECTED_COLOR = chalk.green;
17+
const RECEIVED_COLOR = chalk.red;
18+
19+
export default (cb: Function) => (...args: any) => (
20+
title: string,
21+
test: Function,
22+
): void => {
23+
if (args.length === 1) {
24+
const table: Table = args[0];
25+
return table.forEach(row =>
26+
cb(util.format(title, ...row), applyRestParams(row, test)),
27+
);
28+
}
29+
30+
const templateStrings = args[0];
31+
const data = args.slice(1);
32+
33+
const keys = getHeadingKeys(templateStrings[0]);
34+
const table = buildTable(data, keys.length, keys);
35+
36+
if (data.length % keys.length !== 0) {
37+
return cb(title, () => {
38+
throw new Error(
39+
'Not enough arguments supplied for given headings:\n' +
40+
EXPECTED_COLOR(keys.join(' | ')) +
41+
'\n\n' +
42+
'Received:\n' +
43+
RECEIVED_COLOR(pretty(data)) +
44+
'\n\n' +
45+
`Missing ${RECEIVED_COLOR(`${data.length % keys.length}`)} arguments`,
46+
);
47+
});
48+
}
49+
50+
return table.forEach(row =>
51+
cb(interpolate(title, row), applyObjectParams(row, test)),
52+
);
53+
};
54+
55+
const applyRestParams = (params: Array<any>, test: Function) => {
56+
if (params.length < test.length) return done => test(...params, done);
57+
58+
return () => test(...params);
59+
};
60+
61+
const getHeadingKeys = (headings: string): Array<string> =>
62+
headings.replace(/\s/g, '').split('|');
63+
64+
const buildTable = (
65+
data: Array<any>,
66+
rowSize: number,
67+
keys: Array<string>,
68+
): Array<any> =>
69+
Array.from({length: data.length / rowSize})
70+
.map((_, index) => data.slice(index * rowSize, index * rowSize + rowSize))
71+
.map(row =>
72+
row.reduce(
73+
(acc, value, index) => Object.assign({}, acc, {[keys[index]]: value}),
74+
{},
75+
),
76+
);
77+
78+
const interpolate = (title: string, data: any) =>
79+
Object.keys(data).reduce(
80+
(acc, key) => acc.replace('$' + key, data[key]),
81+
title,
82+
);
83+
84+
const applyObjectParams = (obj: any, test: Function) => {
85+
if (test.length > 1) return done => test(obj, done);
86+
87+
return () => test(obj);
88+
};

packages/jest-each/src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import bind from './bind';
12
import arrayEach from './array';
23
import templateEach from './template';
34

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

21+
export {bind};
22+
2023
export default each;

packages/jest-jasmine2/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"expect": "^23.0.0",
1414
"is-generator-fn": "^1.0.0",
1515
"jest-diff": "^23.0.0",
16+
"jest-each": "^23.0.0",
1617
"jest-matcher-utils": "^23.0.0",
1718
"jest-message-util": "^23.0.0",
1819
"jest-snapshot": "^23.0.0",

packages/jest-jasmine2/src/each.js

+1-79
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,7 @@
99

1010
import type {Environment} from 'types/Environment';
1111

12-
import util from 'util';
13-
import chalk from 'chalk';
14-
import pretty from 'pretty-format';
15-
16-
type Table = Array<Array<any>>;
17-
18-
const EXPECTED_COLOR = chalk.green;
19-
const RECEIVED_COLOR = chalk.red;
12+
import {bind as bindEach} from 'jest-each';
2013

2114
export default (environment: Environment): void => {
2215
environment.global.it.each = bindEach(environment.global.it);
@@ -26,74 +19,3 @@ export default (environment: Environment): void => {
2619
environment.global.xdescribe.each = bindEach(environment.global.xdescribe);
2720
environment.global.fdescribe.each = bindEach(environment.global.fdescribe);
2821
};
29-
30-
const bindEach = (cb: Function) => (...args: any) => (
31-
title: string,
32-
test: Function,
33-
): void => {
34-
if (args.length === 1) {
35-
const table: Table = args[0];
36-
return table.forEach(row =>
37-
cb(util.format(title, ...row), applyRestParams(row, test)),
38-
);
39-
}
40-
41-
const templateStrings = args[0];
42-
const data = args.slice(1);
43-
44-
const keys = getHeadingKeys(templateStrings[0]);
45-
const table = buildTable(data, keys.length, keys);
46-
47-
if (data.length % keys.length !== 0) {
48-
return cb(title, () => {
49-
throw new Error(
50-
'Not enough arguments supplied for given headings:\n' +
51-
EXPECTED_COLOR(keys.join(' | ')) +
52-
'\n\n' +
53-
'Received:\n' +
54-
RECEIVED_COLOR(pretty(data)) +
55-
'\n\n' +
56-
`Missing ${RECEIVED_COLOR(`${data.length % keys.length}`)} arguments`,
57-
);
58-
});
59-
}
60-
61-
return table.forEach(row =>
62-
cb(interpolate(title, row), applyObjectParams(row, test)),
63-
);
64-
};
65-
66-
const applyRestParams = (params: Array<any>, test: Function) => {
67-
if (params.length < test.length) return done => test(...params, done);
68-
69-
return () => test(...params);
70-
};
71-
72-
const getHeadingKeys = (headings: string): Array<string> =>
73-
headings.replace(/\s/g, '').split('|');
74-
75-
const buildTable = (
76-
data: Array<any>,
77-
rowSize: number,
78-
keys: Array<string>,
79-
): Array<any> =>
80-
Array.from({length: data.length / rowSize})
81-
.map((_, index) => data.slice(index * rowSize, index * rowSize + rowSize))
82-
.map(row =>
83-
row.reduce(
84-
(acc, value, index) => Object.assign({}, acc, {[keys[index]]: value}),
85-
{},
86-
),
87-
);
88-
89-
const interpolate = (title: string, data: any) =>
90-
Object.keys(data).reduce(
91-
(acc, key) => acc.replace('$' + key, data[key]),
92-
title,
93-
);
94-
95-
const applyObjectParams = (obj: any, test: Function) => {
96-
if (test.length > 1) return done => test(obj, done);
97-
98-
return () => test(obj);
99-
};

0 commit comments

Comments
 (0)