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
Vitest spyOn has different behaviour than jest's #1329
Comments
This is not about Vite transforms files to something like this: const func = () => {}
const combine = () => {
func()
}
Object.definePropertry(__vite_exports__, 'func', {
get: () => func
})
Object.definePropertry(__vite_exports__, 'combine', {
get: () => combine
})
I assume, babel saves pointers to functions somehow. This is out of scope of Vitest. Maybe you can point to Vite repo for the solution. I will not close this issue for now, in case you have any questions. |
Thanks for the quick response! what would you say the workaround? besides potentially changing vite itself |
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) |
@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 |
Yes, it works. |
are there any docs or examples regarding class level spies? i've scoured the docs site, but didn't see any references. not sure if i'm just overlooking them, however. i'm finding that call counts are not recorded with a class level spy. my goal is to ensure the method
none of the patterns above got my test working. any thoughts? (no bug btw, i'm just doing something wrong and looking for a working example haha 😅) |
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
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: