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

[jest-each] Feature: handle 1d array #6351

Merged
merged 5 commits into from
May 30, 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 @@

### Features

* `[jest-each]` Support one dimensional array of data ([#6351](https://github.com/facebook/jest/pull/6351))
* `[jest-watch]` create new package `jest-watch` to ease custom watch plugin development ([#6318](https://github.com/facebook/jest/pull/6318))

### Fixes
Expand Down
110 changes: 82 additions & 28 deletions e2e/__tests__/__snapshots__/each.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `
✓ template table fails on one row expected: true == true
✕ template table fails on all rows expected: 1 == 2
✕ template table fails on all rows expected: 3 == 4
✕ The word red contains the letter 'z'
✕ The word green contains the letter 'z'
✕ The word bean contains the letter 'z'
template table describe fails on all rows expected a == b
✕ fails
template table describe fails on all rows expected c == d
Expand Down Expand Up @@ -187,22 +190,73 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `

at __tests__/failure.test.js:40:18

● The word red contains the letter 'z'

expect(received).toBe(expected) // Object.is equality

Expected: true
Received: false

45 | \\"The word %s contains the letter 'z'\\",
46 | word => {
> 47 | expect(/z/.test(word)).toBe(true);
| ^
48 | }
49 | );
50 |

at __tests__/failure.test.js:47:28

● The word green contains the letter 'z'

expect(received).toBe(expected) // Object.is equality

Expected: true
Received: false

45 | \\"The word %s contains the letter 'z'\\",
46 | word => {
> 47 | expect(/z/.test(word)).toBe(true);
| ^
48 | }
49 | );
50 |

at __tests__/failure.test.js:47:28

● The word bean contains the letter 'z'

expect(received).toBe(expected) // Object.is equality

Expected: true
Received: false

45 | \\"The word %s contains the letter 'z'\\",
46 | word => {
> 47 | expect(/z/.test(word)).toBe(true);
| ^
48 | }
49 | );
50 |

at __tests__/failure.test.js:47:28

● template table describe fails on all rows expected a == b › fails

expect(received).toBe(expected) // Object.is equality

Expected: \\"b\\"
Received: \\"a\\"

50 | ({left, right}) => {
51 | it('fails ', () => {
> 52 | expect(left).toBe(right);
57 | ({left, right}) => {
58 | it('fails ', () => {
> 59 | expect(left).toBe(right);
| ^
53 | });
54 | }
55 | );
60 | });
61 | }
62 | );

at __tests__/failure.test.js:52:20
at __tests__/failure.test.js:59:20

● template table describe fails on all rows expected c == d › fails

Expand All @@ -211,15 +265,15 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `
Expected: \\"d\\"
Received: \\"c\\"

50 | ({left, right}) => {
51 | it('fails ', () => {
> 52 | expect(left).toBe(right);
57 | ({left, right}) => {
58 | it('fails ', () => {
> 59 | expect(left).toBe(right);
| ^
53 | });
54 | }
55 | );
60 | });
61 | }
62 | );

at __tests__/failure.test.js:52:20
at __tests__/failure.test.js:59:20

● array table describe fails on all rows expected a == b › fails

Expand All @@ -228,15 +282,15 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `
Expected: \\"b\\"
Received: \\"a\\"

59 | (left, right) => {
60 | it('fails', () => {
> 61 | expect(left).toBe(right);
66 | (left, right) => {
67 | it('fails', () => {
> 68 | expect(left).toBe(right);
| ^
62 | });
63 | }
64 | );
69 | });
70 | }
71 | );

at __tests__/failure.test.js:61:20
at __tests__/failure.test.js:68:20

● array table describe fails on all rows expected c == d › fails

Expand All @@ -245,15 +299,15 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `
Expected: \\"d\\"
Received: \\"c\\"

59 | (left, right) => {
60 | it('fails', () => {
> 61 | expect(left).toBe(right);
66 | (left, right) => {
67 | it('fails', () => {
> 68 | expect(left).toBe(right);
| ^
62 | });
63 | }
64 | );
69 | });
70 | }
71 | );

at __tests__/failure.test.js:61:20
at __tests__/failure.test.js:68:20

"
`;
7 changes: 7 additions & 0 deletions e2e/each/__tests__/failure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ it.each`
}
);

test.each(['red', 'green', 'bean'])(
"The word %s contains the letter 'z'",
word => {
expect(/z/.test(word)).toBe(true);
}
);

describe.each`
left | right
${'a'} | ${'b'}
Expand Down
7 changes: 7 additions & 0 deletions e2e/each/__tests__/success.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

test.each(['red', 'green', 'bean'])(
"The word %s contains the letter 'e'",
word => {
expect(/e/.test(word)).toBe(true);
}
);

it.each([[true, true], [true, true]])(
'passes one row expected %s == %s',
(left, right) => {
Expand Down
20 changes: 19 additions & 1 deletion packages/jest-each/src/__tests__/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,25 @@ describe('jest-each', () => {
);
});

test('calls global with cb function containing all parameters of each test case', () => {
test('calls global with cb function containing all parameters of each test case when given 1d array', () => {
const globalTestMocks = getGlobalTestMocks();
const testCallBack = jest.fn();
const eachObject = each.withGlobal(globalTestMocks)(['hello', 'world']);
const testFunction = get(eachObject, keyPath);
testFunction('expected string', testCallBack);

const globalMock = get(globalTestMocks, keyPath);

globalMock.mock.calls[0][1]();
expect(testCallBack).toHaveBeenCalledTimes(1);
expect(testCallBack).toHaveBeenCalledWith('hello');

globalMock.mock.calls[1][1]();
expect(testCallBack).toHaveBeenCalledTimes(2);
expect(testCallBack).toHaveBeenCalledWith('world');
});

test('calls global with cb function containing all parameters of each test case 2d array', () => {
const globalTestMocks = getGlobalTestMocks();
const testCallBack = jest.fn();
const eachObject = each.withGlobal(globalTestMocks)([
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const RECEIVED_COLOR = chalk.red;
export default (cb: Function) => (...args: any) =>
function eachBind(title: string, test: Function): void {
if (args.length === 1) {
const table: Table = args[0];
const table: Table = args[0].every(Array.isArray)
? args[0]
: args[0].map(entry => [entry]);
return table.forEach(row =>
cb(util.format(title, ...row), applyRestParams(row, test)),
);
Expand Down