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

Make sure invalid jest-each points to user code #6347

Merged
merged 3 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -6,6 +6,7 @@

### Fixes

* `[jest-each]` Make sure invalid arguments to `each` points back to the user's code
* `[expect]` toMatchObject throws TypeError when a source property is null ([#6313](https://github.com/facebook/jest/pull/6313))
* `[jest-cli]` Normalize slashes in paths in CLI output on Windows ([#6310](https://github.com/facebook/jest/pull/6310))

Expand Down
11 changes: 10 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,16 @@ 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
6 | */
7 |
> 8 | it.each\`
| ^
9 | left | right
10 | \${true} | \${true}
11 | \${true} |

at packages/jest-each/build/bind.js:80:23
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would be lovely if we could strip this internal call

Copy link
Contributor

Choose a reason for hiding this comment

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

That would be awesome!

Copy link
Member Author

Choose a reason for hiding this comment

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

it's 2 or 3 layers up, so a bit painful (we'd have to create the error in index and pass it into bind)

Copy link
Member Author

Choose a reason for hiding this comment

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

HAH, I lied. Pushed 🙂

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

"
`;
Expand Down
20 changes: 11 additions & 9 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ export default (cb: Function) => (...args: any) => (
const table = buildTable(data, keys.length, keys);

if (data.length % keys.length !== 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`,
Copy link
Member Author

Choose a reason for hiding this comment

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

last s here should be gone, when 1. We've got a pluralize helper or two sticking around somewhere

Copy link
Contributor

Choose a reason for hiding this comment

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

Want me to send a PR for this? I can't amend this branch ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

yes please 🙂

);

return cb(title, () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

@mattphillips the idea is to create the error within the same async tick as the method call from the user - the moment you cross an async barrier (like executing the body of a test) the original stack is lost

Copy link
Contributor

Choose a reason for hiding this comment

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

mind blown

Copy link
Member Author

@SimenB SimenB May 29, 2018

Choose a reason for hiding this comment

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

throwing a string instead of a new error would probably work as well, as we'd discover there was no stack, and use the one we keep around for async errors. Throwing an error though gives us a stack - it just doesn't point to user code

throw 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`,
);
throw error;
});
}

Expand Down