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

fix(jest-each) Throws an error when too much arguments are passed #9818

Merged
merged 2 commits into from Apr 15, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@

- `[expect]` Restore support for passing functions to `toHaveLength` matcher ([#9796](https://github.com/facebook/jest/pull/9796))
- `[jest-changed-files]` `--only-changed` should include staged files ([#9799](https://github.com/facebook/jest/pull/9799))
- `[jest-each]` `each` will throw an error when called with too many arguments ([#9818](https://github.com/facebook/jest/pull/9818))

### Chore & Maintenance

Expand Down
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`throws an error when not called with the right number of arguments 1`] = `"\`.each\` must only be called with an Array or Tagged Template Literal."`;
14 changes: 14 additions & 0 deletions packages/jest-each/src/__tests__/index.test.ts
Expand Up @@ -32,3 +32,17 @@ describe('template', () => {
});
});
});

test('throws an error when not called with the right number of arguments', () => {
expect(() =>
each(
[
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
],
'seems like a title but should not be here',
() => {},
),
).toThrowErrorMatchingSnapshot();
});
7 changes: 7 additions & 0 deletions packages/jest-each/src/index.ts
Expand Up @@ -15,6 +15,13 @@ const install = (
table: Global.EachTable,
...data: Global.TemplateData
) => {
const bindingWithArray = data.length === 0;
const bindingWithTemplate = Array.isArray(table) && !!(table as any).raw;
if (!bindingWithArray && !bindingWithTemplate) {
throw new Error(
'`.each` must only be called with an Array or Tagged Template Literal.',
);
}
const test = (title: string, test: Global.EachTestFn, timeout?: number) =>
bind(g.test)(table, ...data)(title, test, timeout);
test.skip = bind(g.test.skip)(table, ...data);
Expand Down