You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It looks like mockReturnValue does not always work as expected (at least when used with classes).
Let's say you have an App class, that calls a method run on each of its deps:
classDep{run(): boolean{// does something random}}classApp{constructor(privatedeps: Array<Dep>){}start(): Array<Dep>{return(this.deps// run every dep// and only keeps the ones that started.filter((dep)=>dep.run()));}}
The following simple test fails:
describe('App',()=>{letdep1: Dep;letdep2: Dep;letapp: App;beforeEach(()=>{dep1=newDep();// first dep startsvi.spyOn(dep1,'run').mockReturnValue(true);dep2=newDep();// second dep does notvi.spyOn(dep2,'run').mockReturnValue(false);app=newApp([dep1,dep2]);});test('should return only the deps that started',()=>{construnningDeps: Array<Dep>=app.start();expect(dep1.run).toHaveBeenCalled();expect(dep2.run).toHaveBeenCalled();// fails because `runningDeps` is emptyexpect(runningDeps).toEqual([dep1]);});});
Describe the bug
It looks like
mockReturnValue
does not always work as expected (at least when used with classes).Let's say you have an
App
class, that calls a methodrun
on each of its deps:The following simple test fails:
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-iucjaa?file=test/suite.test.ts
Note that the same test in Jest does succeed
System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: