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

toHaveBeenNthCalledWith has a wrong test report when using objects #6614

Closed
maximelebastard opened this issue Jul 4, 2018 · 4 comments
Closed

Comments

@maximelebastard
Copy link

🐛 Bug Report

When used with objects or classes for parameters expectations, toHaveBeenNthCalledWith error messages report the wrong call argument. It reports the last call arguments as received arguments.

To Reproduce

This test using expect.toHaveBeenNthCalledWith with objects has a false failure report

class Bar {
  constructor(toto) {
    this.toto = toto;
  }
}

function foo(mock) {
  mock.titi(new Bar('a'));
  mock.titi(new Bar('not b at all'));
  mock.titi(new Bar('c'));
}
test('toHaveBeenNthCalledWith bad report', () => {
  const mock = {
    titi: jest.fn()
  };

  foo(mock);

  expect(mock.titi).toHaveBeenNthCalledWith(1, new Bar('a'));
  expect(mock.titi).toHaveBeenNthCalledWith(2, new Bar('b'));
  expect(mock.titi).toHaveBeenNthCalledWith(3, new Bar('c'));
});

The test fails because the second mock.titi call received new Bar('not b at all') instead of new Bar('b') as a parameter.
But the error messages says it was called with new Bar('c') which is the last call.

  ● toHaveBeenNthCalledWith bad report

    expect(jest.fn()).toHaveBeenNthCalledWith(expected)

    Expected mock function second call to have been called with:
      {"toto": "b"}
    as argument 1, but it was called with
      {"toto": "c"}.

    Difference:

    - Expected
    + Received

      Bar {
    -   "toto": "b",
    +   "toto": "c",
      }

      18 |
      19 |   expect(mock.titi).toHaveBeenNthCalledWith(1, new Bar('a'));
    > 20 |   expect(mock.titi).toHaveBeenNthCalledWith(2, new Bar('b'));
         |                     ^
      21 |   expect(mock.titi).toHaveBeenNthCalledWith(3, new Bar('c'));
      22 | });
      23 |

      at Object.<anonymous>.test (src/report.test.js:20:21)

Expected behavior

I expect the test report to give the real argument the mock was called with.

Run npx envinfo --preset jest

npx envinfo --preset jest
npx: installed 1 in 1.606s

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  Binaries:
    Node: 8.10.0 - /usr/local/bin/node
    Yarn: 1.5.1 - /opt/local/bin/yarn
    npm: 6.1.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^23.0.1 => 23.0.1
@SimenB
Copy link
Member

SimenB commented Jul 4, 2018

Wanna send a PR for it? :)

@maximelebastard
Copy link
Author

I'll try to find the bug when I get some time ;-)

@maximelebastard
Copy link
Author

I was about to start then I saw that
#6340

I'm just using an outdated version, sorry for the false report and thanks to @chinesedfan for the fix 👍

@github-actions
Copy link

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.

@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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants