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 does not check call times #5417

Closed
6 tasks done
Takeno opened this issue Mar 22, 2024 · 1 comment · Fixed by #5420
Closed
6 tasks done

toHaveBeenNthCalledWith does not check call times #5417

Takeno opened this issue Mar 22, 2024 · 1 comment · Fixed by #5420
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@Takeno
Copy link

Takeno commented Mar 22, 2024

Describe the bug

The assertion of toHaveBeenNthCalledWith compares the arguments but doesn't check if the spy has been called Nth times. Doing this, the assertion error is about the mismatch between arguments instead of the number of invocations.

 FAIL  index.test.js > should work
AssertionError: expected 3rd "spy" call to have been called with [ 'fail' ]

- Expected: 
Array [
  "fail",
]

+ Received: 
undefined

For reference, the jest output:

n: 3
Expected: "fail"
Received
       2: "baz"

Number of calls: 2

Reproduction

Here is a repository to check jest vs vitest outputs: https://github.com/Takeno/vitest-toHaveBeenNthCalledWith

To reproduce the test:

import { expect, test, vi } from 'vitest';

test('should work', () => {
  const f = vi.fn();

  f('foo');
  f('baz');

  expect(f).toHaveBeenNthCalledWith(1, 'foo');
  expect(f).toHaveBeenNthCalledWith(2, 'baz');
  expect(f).toHaveBeenNthCalledWith(3, 'fail');
});

System Info

System:
    OS: macOS 13.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 69.88 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.18.0 - ~/.nvm/versions/node/v18.18.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.18.0/bin/yarn
    npm: 9.8.1 - ~/.nvm/versions/node/v18.18.0/bin/npm
    pnpm: 8.15.0 - ~/Library/pnpm/pnpm
    Watchman: 2024.01.22.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 123.0.6312.58
    Safari: 16.5.2

Used Package Manager

npm

Validations

@hi-ogawa
Copy link
Contributor

Thanks for the issue with reproduction. Yeah, Vitest's error message is not really clear, but I'm not sure Jest's output does so much better either.

The call arguments (array) are checked against undefined, so I don't think this will make assertions to pass when it should fail. Probably, we can just tweak the error message to append something like this:

  • expected 3rd "spy" call to have been called with [ 'fail' ], but only called 2 times.

@hi-ogawa hi-ogawa added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Mar 23, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants