How do you mock a module multiple times (now) ? #4124
Answered
by
sheremet-va
RossRogers
asked this question in
Q&A
-
Similar to this thread #1255, I want to mock a module multiple times. I have 3 files:
As the documents say, this will fail because
sheremet-va's answer to that previous post doesn't seem to work in this scenario. The only hacky work-around that I've found is to use global variables at the module level:
This works, but I don't like it. What's the alternative to the above? |
Beta Was this translation helpful? Give feedback.
Answered by
sheremet-va
Sep 13, 2023
Replies: 1 comment 5 replies
-
You can use vi.hoisted instead: const api = vi.hoisted(() => vi.fn())
vi.mock("./path.js". () => ({
api
})
api.mockReturnValue(42)
api.mockReturnValue(0) |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
RossRogers
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use vi.hoisted instead: