toHaveBeenCalledWith indifferent to parameters that have `undefined` or `null` as a value
See original GitHub issue🐛 Bug Report
When mocking a function which takes parameters, if one of the parameter’s value is undefined, toHaveBeenCalledWith can be called with or without that same parameter as an expected parameter, and the assertion will pass.
To Reproduce
Code:
# call-with-fn.js
function callFnWith(a, fn) {
const result = fn({ arg: a });
return result;
}
module.exports = callFnWith;
Test:
# call-with-fn-test.js
const callFnWith = require('./call-fn-with');
describe('undefined arg', () => {
const fn = jest.fn(() => ({ arg: 3, result: undefined }));
callFnWith(3, fn);
it('should include an undefined arg', () => {
// I would have assumed I needed to include "result: undefined" below:
expect(fn).toHaveBeenCalledWith({ arg: 3 });
});
});
The test passes with both variants of this assertion:
expect(fn).toHaveBeenCalledWith({ arg: 3 });
expect(fn).toHaveBeenCalledWith({ arg: 3, result: undefined });
Expected behavior
I would have expected the assertion to fail with the first variant above.
Link to repl or repo (highly encouraged)
https://repl.it/repls/PaltryNuttyEmacs
Issues without a reproduction link are likely to stall.
Run npx envinfo --preset jest
Paste the results here:
System:
OS: macOS 10.14.4
CPU: (8) x64 Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
Binaries:
Node: 10.15.1 - ~/.nvm/versions/node/v10.15.1/bin/node
npm: 6.8.0 - ~/.nvm/versions/node/v10.15.1/bin/npm
npmPackages:
jest: ^24.6.0 => 24.6.0
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Loose match one value in jest.toHaveBeenCalledWith
If you have a method that accepts multiple parameters (not an object) and you only want to match some of these parameters then...
Read more >Using Jest toHaveBeenCalledWith for testing primitive data ...
In this post, you will learn how to use Jest toHaveBeenCalledWith for testing various scenarios like a partial array, partial object, multiple ...
Read more >Jest assert over single or specific argument/parameters with ...
All our tests will center around the values getPingConfigs is called with (using .toHaveBeenCalledWith assertions). Let's create some tests that ...
Read more >Using Optional and Nullable Properties in API Requests
Nullable Parameters. OpenAPI supports values of data types to be null. To specify, one can use the “nullable: true” property while defining a ......
Read more >What is the difference between 'undefined' and 'unsupplied ...
The undefined value is assigned to any variable or array element that has been ... parameters that have not been supplied when the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@twelve17 in addition to what Tim said in preceding comment, study your example code to see:
fnis called with{ arg: 3 }fnreturns{ arg: 3, result: undefined }For more information:
If you make some assumptions about number of calls, you can write specific assertions:
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. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.