I would like to use `vi.spyOn` to monitor the call to an sideEffect function in a module to make sure is being called by another function in a module.
I did this on jest without problems but it does not seem to work on vitest.
Here is a simplified example
aModule.ts
export function a() {
return sideEffect();
}
export function sideEffect() {
return 'a';
}
Here is the test file:
import { vi, expect, test } from 'vitest';
import * as aModule from '../src/aModule';
test('expect "sideEffect" to be called at least once', async () => {
const sideEffectSpy = vi.spyOn(aModule, 'sideEffect').mockReturnValue('b');
const aSpy = vi.spyOn(aModule, 'a');
const res = aModule.a(); // This function calls sideEffect internally.
expect(res).toBe('b'); // This fails - it returns 'a' so the spyOn is not workng
expect(sideEffectSpy).toHaveBeenCalled(); // This fails as well :(
});
I did try a few variations on this, but could not make it work. Any ideas?
Thx.