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.

Vitest spyOn has different behaviour than jest's

See original GitHub issue

Describe the bug

Hey! It seems the vitest spyOn doesn’t have the same behaviour as the jest’s (see reproduction)

Reproduction

vitest project reproduction: https://stackblitz.com/edit/vitest-dev-vitest-ne8pti?file=src%2Fexample.ts,test%2Fbasic.test.ts,package.json&initialPath=__vitest__

jest project reproduction: https://github.com/galkatz373/jest-example

System Info

macOS 12.0.1, node 16.6.0

Used Package Manager

npm

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
sheremet-vacommented, May 18, 2022

If you store methods on an object or class, you can spy on them without problem:

// funcs.ts
class Helper {
  func() {}
  
  combine() {
    this.func()
  }
}

export const helper = new Helper()

// or

export const helper = {
  func: () => {},
  combine: () => {
    helper.func()
  }
}

// test.ts
import { helper } from './funcs'

vi.spyOn(helper, 'func').mockReturnOnce(100)
0reactions
sheremet-vacommented, Oct 7, 2022

@sheremet-va works as well without instances but class level? For example, Helper class is injected by DI inside a Component and I want to test the component by mocking the class methods. Does Vitest support something like vi.spyOn(Helper.prototype, "func").mockReturnOnce(100)? Thanks.

Yes, it works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference | Vitest
A blazing fast unit test framework powered by Vite.
Read more >
Using Jest, how do I spyon an extended component's method ...
However, I tried changing this around and didn't have any success or different behavior. So my question is, how do I spy on...
Read more >
Jest Full and Partial Mock/Spy of CommonJS and ES6 Module ...
This post goes through how to achieve different types of module mocking scenarios with Jest. From simple Import interception, ...
Read more >
Mock Functions or Spies Demystified - How Does jest.fn() Work?
This mock function has an implementation, which is called internally. The act of passing a mock function to greetWorld allows us to spy...
Read more >
Timer Mocks - Jest
... implementation of setTimeout() and other timer functions. Timers can be restored to their normal behavior with jest.useRealTimers() .
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