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.

toHaveBeenCalledOnce expects a spy to be called multiple times

See original GitHub issue

Describe 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.

Screen Shot 2021-12-21 at 3 28 53 PM

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.

  1. Check out this repo
  2. Run pnpm install
  3. 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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
Smrtnykcommented, Dec 22, 2021

Oh, I see, you reset them manually. Maybe there is a bug with that

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.

1reaction
Smrtnykcommented, Dec 21, 2021

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.

Read more comments on GitHub >

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

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