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

Complain if expect is passed multiple arguments #4237

Merged
merged 1 commit into from
Aug 10, 2017
Merged

Complain if expect is passed multiple arguments #4237

merged 1 commit into from
Aug 10, 2017

Conversation

gustavnikolaj
Copy link
Contributor

Summary

When using unexpected it's common to import it under the name expect. Consider this case:

import expect from 'unexpected';

it('should be unexpected', () => {
  expect(true, 'to be false');
});

If a user by accident forgets to import unexpected, they will not immediately notice, as the builtin expect in jest will ignore any further arguments passed to it. Which will result in a passing test instead of the failure you would expect.

This PR adds a check to the expect method, so that it will throw an error if it is called with more than one argument.

@Munter already discussed a solution to this with @cpojer. The solution I implemented here is a bit different. Instead of using a check on arguments.length I opted to use ...rest and check that it has a length of 0. The expect method was implemented as an arrow function, and arrow functions do not expose arguments, so it was either refactoring it into a normal function or using the ...rest alternative. From my humble benchmarking I didn't observe any significant difference, so I opted for the least invasive change, that also happens to be following the Code Conventions the closest - prefering es6 syntax where possible.

To further enhance the check one could add a check for the case of expect being called with no arguments. It's less likely to cause users to shoot themselves in the feet, so I left it out.

Test plan

The test implemented covers the case that the check is implementing.

All tests in the project is passing. I ran yarn test in the root of the repo to verify that the change had no unintended side effects.

@Munter
Copy link

Munter commented Aug 10, 2017

Discussion was in jest-core #general on Aug 9th

@cpojer cpojer merged commit 8a0bdd2 into jestjs:master Aug 10, 2017
@cpojer
Copy link
Member

cpojer commented Aug 10, 2017

Sounds good, and still enables us to allow more arguments in the future if we want to.

tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
@gustavnikolaj gustavnikolaj deleted the feature/errorOnTooManyArgsToExpect branch May 12, 2018 10:40
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants