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] Fix pluralising missing arguments error #6369

Merged
merged 4 commits into from
Jun 2, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

### Fixes

- `[jest-each]` Stop test title concatenating extra args ([##6346](https://github.com/facebook/jest/pull/#6346))
- `[jest-each]` Fix pluralising missing arguments error ([#6369](https://github.com/facebook/jest/pull/#6369))
- `[jest-each]` Stop test title concatenating extra args ([#6346](https://github.com/facebook/jest/pull/#6346))
Copy link
Member

Choose a reason for hiding this comment

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

the link should not have the #

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

- `[expect]` toHaveBeenNthCalledWith/nthCalledWith gives wrong call messages if not matched ([#6340](https://github.com/facebook/jest/pull/6340))
- `[jest-each]` Make sure invalid arguments to `each` points back to the user's code ([#6347](https://github.com/facebook/jest/pull/6347))
- `[expect]` toMatchObject throws TypeError when a source property is null ([#6313](https://github.com/facebook/jest/pull/6313))
Expand Down
28 changes: 26 additions & 2 deletions e2e/__tests__/__snapshots__/each.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ exports[`runs only the describe.only.each tests 1`] = `

exports[`shows error message when not enough arguments are supplied to tests 1`] = `
"FAIL __tests__/each-exception.test.js
✕ throws exception when one argument too few are supplied $left == $right
✕ throws exception when not enough arguments are supplied $left == $right

● throws exception when not enough arguments are supplied $left == $right
● throws exception when one argument too few are supplied $left == $right

Not enough arguments supplied for given headings:
left | right
Expand All @@ -27,7 +28,7 @@ exports[`shows error message when not enough arguments are supplied to tests 1`]
true,
]

Missing 1 arguments
Missing 1 argument

6 | */
7 |
Expand All @@ -39,6 +40,29 @@ exports[`shows error message when not enough arguments are supplied to tests 1`]

at __tests__/each-exception.test.js:8:1

● throws exception when not enough arguments are supplied $left == $right

Not enough arguments supplied for given headings:
left | right | up | down

Received:
Array [
true,
true,
]

Missing 2 arguments
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about using snapshot-diff to only check the difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we use this already in core?


17 | );
18 |
> 19 | it.each\`
| ^
20 | left | right | up | down
21 | \${true} | \${true}
22 | \`(

at __tests__/each-exception.test.js:19:1

"
`;

Expand Down
10 changes: 10 additions & 0 deletions e2e/each/__tests__/each-exception.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ it.each`
left | right
${true} | ${true}
${true}
`(
'throws exception when one argument too few are supplied $left == $right',
({left, right}) => {
expect(left).toBe(right);
}
);

it.each`
left | right | up | down
${true} | ${true}
`(
'throws exception when not enough arguments are supplied $left == $right',
({left, right}) => {
Expand Down
12 changes: 10 additions & 2 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ export default (cb: Function) => (...args: any) =>
const keys = getHeadingKeys(templateStrings[0]);
const table = buildTable(data, keys.length, keys);

if (data.length % keys.length !== 0) {
const missingData = data.length % keys.length;

if (missingData > 0) {
const error = new Error(
'Not enough arguments supplied for given headings:\n' +
EXPECTED_COLOR(keys.join(' | ')) +
'\n\n' +
'Received:\n' +
RECEIVED_COLOR(pretty(data)) +
'\n\n' +
`Missing ${RECEIVED_COLOR(`${data.length % keys.length}`)} arguments`,
`Missing ${RECEIVED_COLOR(missingData.toString())} ${pluralize(
'argument',
missingData,
)}`,
);

if (Error.captureStackTrace) {
Expand Down Expand Up @@ -98,3 +103,6 @@ const applyObjectParams = (obj: any, test: Function) => {

return () => test(obj);
};

const pluralize = (word: string, count: number) =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about using pluralize from jest-matcher-utils?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure if it's worth to import whole package though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah it didn't seem worth it for such a small function

Copy link
Collaborator

Choose a reason for hiding this comment

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

nope

word + (count === 1 ? '' : 's');