-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Test fail for optional parameters in "toHaveBeenCalledWith" #5197
Comments
Yeah, we could do that, and use function.length or something to pad it. |
Feels like a footgun, doesn't it? I would prefer it to not be the default if added, I like being explicit. Can use This feels more like a bug with the Not sure why not... Can dig into it tomorrow |
Yeah, I’m fine with either: padding undefined values at the end or improving the error message. |
It seems weird to me that a test author should be forced to specify optional parameters when a function does not require them. I'd expect the test to pass - and padding with undefined seems like it would provide the expected behavior. @SimenB, can you elaborate why you see it as a footgun? What about a case where there's an optional parameter that sets a default value? I understand your viewpoint of wanting to be explicit, but IMO that's an argument against optional params in an api in general rather than jest's treatment of such apis. |
I guess the concern would be jest saying that a test passed when required parameters weren't actually supplied. I'm still not fully convinced though since I don't think it's jest's job to be a linter, and taking a step back, I think it makes sense for the test to pass in this scenario. |
const spy1 = jest.fn();
const spy2 = jest.fn();
spy1('hello', 'world');
spy2('hello', undefined);
expect(spy1).toHaveBeenCalledWith('hello');
expect(spy2).toHaveBeenCalledWith('hello'); In your suggestion, only the first assertion would fail, not the second. I disagree If so, we should have a |
That makes sense, thanks for the example @SimenB. I'll publish a PR that has a better error message. |
No worries. Sorry about the late response, I somehow missed your replies in here. |
…unexpected argument
…unexpected argument
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
Unit test fails when an optional parameter isn't explicitly passed to
toHaveBeenCalledWith
. Within the terminal, nothing is printed out unless the user is explicit to pass in eitherundefined
or something to fail on purpose.Example is in TypeScript but it is reproducible in JavaScript as well. The repository below has both examples.
Example:
File:
Test:
The output:
If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can
yarn install
andyarn test
.Repo: https://github.com/mrfunkycold/jest-demo
There is a typescript and javascript version. Easiest to just execute
npm run watch:test
and run all the tests to see the failures.What is the expected behavior?
Not exactly sure. I would have expected the output to either do one of the following:
Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.
OS: MacOS 10.12.6
Jest: 22.0.4 (though this has failed for earlier versions)
typescript: 2.6.2
node: v8.4.0
npm: 5.6.0
The text was updated successfully, but these errors were encountered: