toHaveBeenCalledOnce expects a spy to be called multiple times
See original GitHub issueDescribe the bug
When I was cutting over jest tests in vueuse to vitest, the assertions on spies for the useSwipe composable had issues with the number of calls.
In particular,
expect(onSwipe).toHaveBeenCalledOnce()
was expecting the onSwipe spy to have been called twice.
Reproduction
I wasn’t able to create a minimum reproducible issue – the issue has something to do with how the spies are invoked. When the spies are immediately invoked, there is no bug, however within the useSwipe example in vueuse, this issue shows up.
When I simply invoke the spies directly within the useSwipe (subject under test) method, the calls counter works correctly. Given that, I’m betting the issues have something to do with how addEventListener is invoking the spy.
Here’s the PR/branch with a reproduction.
- Check out this repo
- Run
pnpm install - Run
pnpm vitest --config vitest.config.ts useSwipe
System Info
System:
OS: macOS 11.4
CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 184.71 MB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.17.0 - ~/.nvm/versions/node/v14.17.0/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 6.14.13 - ~/.nvm/versions/node/v14.17.0/bin/npm
Browsers:
Chrome Canary: 99.0.4779.0
Firefox: 95.0
Safari: 14.1.1
npmPackages:
vite: ^2.7.3 => 2.6.7
vitest: ^0.0.105 => 0.0.105
Used Package Manager
pnpm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (9 by maintainers)
Top Results From Across the Web
toHaveBeenCalledWith works not with multiple calls #228
The toHaveBeenCalledWith matcher supports arguments from a single call only because there's no clear way to distinguish between calls in the matcher arguments....
Read more >How to test the expectation on the eventSpy - Stack Overflow
Jasmine has no function toHaveBeenCalledOnce . You need to check the count yourself. expect(this.eventSpy).toHaveBeenCalled(); expect(this.
Read more >Expect - Jest
The expect function is used every time you want to test a value. You will rarely call expect by itself. Instead, you will...
Read more >Jasmine Spy Matchers: toHaveBeenCalled ... - SCRIPTVERSE
The toHaveBeenCalledTimes() matcher verifies whether the spied method has been called a specified number of times (which is passed as an argument). In...
Read more >introduction.js - Jasmine Documentation
A spec contains one or more expectations that test the state of the code. ... it("tracks that the spy was called x times",...
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

It s not a bug in clearing them, they were cleared properly in
beforeEach. The issue was that the test 2 really called event listener twice, and number of calls was correctly 2 instead of expected 1. Have a look what test 1 and test 2 actually do, they add same event listener onto the same target, and event listener gets called twice in the end in second test.nah it’s just me not used to monorepos and pnpm, My daily work took a toll at me not being in sync with all new stuff 😄 I am sure the issue is real, that is why I wanted to debug it using old good debugger and walking the callstack.