Skip to content

Commit

Permalink
fix(jest-each) Throws an error when too much arguments are passed
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostd committed Apr 15, 2020
1 parent 99c6fb1 commit c926d9f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
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`] = `[Error: \`.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

0 comments on commit c926d9f

Please sign in to comment.