Replies: 10 comments 18 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.
-
|
I am still running into this issue, is there any solution |
Beta Was this translation helpful? Give feedback.
-
|
Hi Team, I recently faced this issue and tried to resolve this via using SINON but its of no rescue. |
Beta Was this translation helpful? Give feedback.
-
|
Hi all, ran into this issue. I found a clunky workaround: I really wanted this to work without all the dynamic imports but, alas, this was the only version I could get working. You could extend this to use |
Beta Was this translation helpful? Give feedback.
-
|
The simple workaround to this is to have your source file import itself. For myFile.js: import * as myFile from '@/utils/myFile.js';
export function functionA() {
return myFile.functionB() * 2;
}
export function functionB() {
return 10;
}Then, in your test file, you'll be able to mock the function with a spy: import * as myFile from '@/utils/myFile.js';
describe(`functionA`, () => {
const FUNCTION_B_RESULT = 100;
let functionBSpy;
beforeEach(()=>{
functionBSpy = vi.spyOn(myFile, 'functionB').mockReturnValue(FUNCTION_B_RESULT);
})
it(`SHOULD call functionB.`, () => {
myFile.functionA();
expect(functionBSpy).toHaveBeenCalledWith();
})
it(`SHOULD return the result of functionB, multiplied BY 2.`, () => {
const actual = myFile.functionA();
expect(actual).toStrictEqual(FUNCTION_B_RESULT * 2);
})
})Incidentally, this workaround works for Jest as well. I absolutely disagree with the maintainers that "if you need to mock other functions in the same file, your code is bad". having a file grouping your string utils, for instance, where some of them call each other, absolutely makes sense. Sure, I COULD make it an object/class for no reason other than to please Vitest, but making a class that isn't meant to be instantiated is just weird. Or I COULD test the whole behaviour without mocking the inner function, but at this point, it's not an unit test, it's an integration test. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I have a question with
viand 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 mockgetUserFromCacheandgetUserFromDband 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