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 3 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
56 changes: 55 additions & 1 deletion e2e/__tests__/__snapshots__/each.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ exports[`shows error message when not enough arguments are supplied to tests 1`]

Missing 1 arguments

at packages/jest-each/build/bind.js:81:17
at packages/jest-each/build/bind.js:84:17

"
`;
Expand Down Expand Up @@ -68,6 +68,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 @@ -247,5 +250,56 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `

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

● The word red contains the letter 'z'

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

Expected: true
Received: false

67 | \\"The word %s contains the letter 'z'\\",
68 | word => {
> 69 | expect(/z/.test(word)).toBe(true);
| ^
70 | }
71 | );
72 |

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

● The word green contains the letter 'z'

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

Expected: true
Received: false

67 | \\"The word %s contains the letter 'z'\\",
68 | word => {
> 69 | expect(/z/.test(word)).toBe(true);
| ^
70 | }
71 | );
72 |

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

● The word bean contains the letter 'z'

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

Expected: true
Received: false

67 | \\"The word %s contains the letter 'z'\\",
68 | word => {
> 69 | expect(/z/.test(word)).toBe(true);
| ^
70 | }
71 | );
72 |

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

"
`;
7 changes: 7 additions & 0 deletions e2e/each/__tests__/failure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,10 @@ describe.each([['a', 'b'], ['c', 'd']])(
});
}
);

test.each(['red', 'green', 'bean'])(
"The word %s contains the letter 'z'",
word => {
expect(/z/.test(word)).toBe(true);
}
);
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
5 changes: 4 additions & 1 deletion packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export default (cb: Function) => (...args: any) => (
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]);
Copy link
Member

Choose a reason for hiding this comment

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

should we be clever and only wrap those not already arrays? or would that never happen in practice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it would look strange:

test.each([
  ['foo'],
  'bar',
  ['baz'],
  'qux'
])('do something %s', word => {});

But in saying that it would be pretty trivial to just wrap everything that isn't an array in an array. I'm happy to update if you think it makes more sense 😄

Copy link
Member

Choose a reason for hiding this comment

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

Imagine something like this:

test.each([
  ['foo', 'bar'],
  'foobar',
  ['baz', 'zzz', 'woke', 'blazingmeansgood'],
  'qux'
])('do something %s', words => {});

Copy link
Member

Choose a reason for hiding this comment

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

Again, not sure if it's useful at all or if we should just throw

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that all rows should be the same length, i.e. the test callback should have the same number of args. So maybe we should throw an error like the tagged template string? Inconsistent number of arguments in rows, 0, 1, 3. Test function expected X

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, throwing on mismatching lengths sounds good


return table.forEach(row =>
cb(util.format(title, ...row), applyRestParams(row, test)),
);
Expand Down