question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
pedrottimarkcommented, Apr 29, 2019

@twelve17 in addition to what Tim said in preceding comment, study your example code to see:

  • fn is called with { arg: 3 }
  • fn returns { arg: 3, result: undefined }

For more information:

If you make some assumptions about number of calls, you can write specific assertions:

describe('undefined arg', () => {
  const fn = jest.fn(() => ({ arg: 3, result: undefined })); // see results
  callFnWith(3, fn);

  test('calls', () => {
    expect(fn).toHaveBeenCalledWith({ arg: 3 });
    expect(fn).toHaveBeenCalledWith({ arg: 3, result: undefined }); // confusing

    console.log(fn.mock.calls);
    // [ [ { arg: 3 } ] ]

    expect(fn.mock.calls[0][0]).toStrictEqual({ arg: 3 });
    expect(fn.mock.calls[0][0]).not.toStrictEqual({ arg: 3, result: undefined });
  });
  test('results', () => {
    expect(fn).toHaveReturnedWith({ arg: 3 }); // confusing
    expect(fn).toHaveReturnedWith({ arg: 3, result: undefined });

    console.log(fn.mock.results);
    // [ { type: 'return', value: { arg: 3, result: undefined } } ]

    expect(fn.mock.results[0].value).not.toStrictEqual({ arg: 3 });
    expect(fn.mock.results[0].value).toStrictEqual({ arg: 3, result: undefined });
  });
});
0reactions
github-actions[bot]commented, May 11, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found