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

no-useless-undefined: undefined removed from jest function matchers #807

Closed
danut-t opened this issue Aug 11, 2020 · 11 comments · Fixed by #876
Closed

no-useless-undefined: undefined removed from jest function matchers #807

danut-t opened this issue Aug 11, 2020 · 11 comments · Fixed by #876

Comments

@danut-t
Copy link

danut-t commented Aug 11, 2020

test('function called with different parameters that could be undefined', () => {
	expect(someFunction).toHaveBeenCalledWith(1, 2, 3);
	expect(someFunction).toHaveBeenCalledWith(1, undefined, 3);
	expect(someFunction).toHaveBeenCalledWith(1, 2, undefined);
});

This is breaking as jest toHaveBeenCalledWith (and other similar matchers) explicitly checks for nth argument to be undefined

Would be better if this rule would accept and array of "acceptable" functions that have undefined?

@silverwind
Copy link
Contributor

silverwind commented Aug 25, 2020

I'd say you better disable the rule in that case. Generally, there are always cases where explicit undefined makes a difference. Even some builtin functions sadly have it:

> new Date()
2020-08-25T07:28:34.351Z
> new Date(undefined)
Invalid Date

@papb
Copy link

papb commented Aug 27, 2020

Maybe the rule could be automatically disabled for files in which the 'jest' environment is enabled?

@silverwind
Copy link
Contributor

silverwind commented Aug 27, 2020

The issue has nothing to do with jest. Any function that checks the length of its arguments or is implemtend in native code like the Date constructor can be affected and which ones are can probably not be statically determined.

@papb
Copy link

papb commented Aug 27, 2020

I know, but it would be a way to at least lower the amount of false-positives. I agree that false positives would still exist.

@papb
Copy link

papb commented Aug 27, 2020

Wait, actually it seems @fisker already did this in #758

Edit: it was done with an allowlist of methods and toHaveBeenCalledWith was not one of them.

@fisker
Copy link
Collaborator

fisker commented Aug 27, 2020

Yap, the simplest way to fix this is add this name to that list.

@fisker
Copy link
Collaborator

fisker commented Aug 27, 2020

If this issue is about toHaveBeenCalledWith only, we can add this to the compareFunctionNames list, PR welcome.

If it's about arguments.length, we can continue discussion in #760

@silverwind
Copy link
Contributor

Essentially it's a unsolvable problem but I guess a allowlist of common usages of undefined is a bandaid fix that might ease the pain in some cases. Still I have the rule disabled and would not recommend enabling it 😉.

@fisker
Copy link
Collaborator

fisker commented Aug 27, 2020

What do you prefer?

  • an option to ignore CallExpression
  • a configurable list

Or better solution?

@silverwind
Copy link
Contributor

Neither, I would personally just put a eslint-disable-line comment I guess. But I'm probably not the right person to ask this question.

@BPetronel
Copy link

BPetronel commented Aug 27, 2020

The changes made in #758 were to add the missing cases to allow-list,so adding a configurable list might be useful for new checker aliases or test runners too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants