Replies: 6 comments 12 replies
-
we are facing the same issue with mocking exported functions in the same module. |
Beta Was this translation helpful? Give feedback.
-
Also same problem here. I tried this (doesn't work): vi.mock('~/helpers/Utility.js', async (importOriginal) => {
const mod: object = await importOriginal()
return {
...mod,
contestOutcomes: vi.fn((outcome) => contestOutcomes.SUCCESS),
}
})
const spy = vi.spyOn(importOriginal, 'contestOutcomes'); |
Beta Was this translation helpful? Give feedback.
-
Any update on this issues? I faced this problem recently an found zero workaround on the internet. Maybe the solution is to just separate functions into different files |
Beta Was this translation helpful? Give feedback.
-
This should be a bug report I guess. Without this working the lib seems useless to me... |
Beta Was this translation helpful? Give feedback.
-
Also ran into this issue but it is mentioned in vitest Mocking Pitfalls docs. |
Beta Was this translation helpful? Give feedback.
-
i just ran into this exact same issue trying to migrate tests from jest to vitest works in jest (at least kind of) and old code has a lot of module functions just like that surely it is a bad practice but it is a major blocker for switching from jest to vitest 😢 |
Beta Was this translation helpful? Give feedback.
-
Hello,
I have a question with
vi
and mocking. I search since 3 days and I found zero response or nothing works like I want.I have this file :
I have 3 parts of test in my file :
getUserFromDb
: I mock the database and generate the complexe result to see if all works like I wantgetUserFromCache
: I mock redis part and saw if all returns like I wantgetUser
: I want to mockgetUserFromCache
andgetUserFromDb
and show if all are called correctly according the use cases.I have the problem for the third part. I don't find how to do this. I test
vi.spyOn
,vi.mock
, ... and none works. The first is ignored, the second mock at all cases.How to do that ?
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions