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
You could try wrapper.vm.fnInside = vi.fn().mockReturnValue(1) but I think that will fail.
I don't think what you trying to do is doable. Think of it that way:
it('should mock the function fnInside',()=>{// this is the setup of your componentconstfactory=()=>{constfnInside=()=>10constfnOutside=()=>fnInside()return{ fnInside, fnOutside }}// this is the mount(Component)constobj=factory()// override fnInsideobj.fnInside=vi.fn().mockReturnValue(1)expect(obj.fnInside()).toBe(1)expect(obj.fnOutside()).toBe(1)// fails, returns 10, because it uses an internal reference to fnInside and not our override})
Usually, you'll want to mock a function call to something external to your component (a HTTP call, a call to a composable, etc).
In that case, it's pretty straightforward using vi.mock
Describe the bug
Mocking a function in a function in a Vue component if these 2 functions are declared in the same vue file doesn't work.
To Reproduce
Minimum reproduction:
https://github.com/mattaiod/VITEST-cant-mock-fn-in-fn-in-component
Expected behavior
Since I have mocked
fnInside
with1
andfnInside
is returned infnOutside
, I should get1
and not10
Related information:
@vue/test-utils
: 2.1.0vitest
: 0.23.4Vue
: 3.2.40node
: v16.13.1pnpm
: 7.8.0The text was updated successfully, but these errors were encountered: