Основной пример: < /p>
Код: Выделить всё
// test.ts
import { a } from './asd';
const { f } = vi.hoisted(() => {
return {
f: vi.fn(),
};
});
vi.mock('../../../api/ApiProvider', () => ({
f,
}));
describe('Test', () => {
it('sample test case', () => {
f.mockReturnValue(42);
expect(a).toEqual(1);
});
it('sample test case2', () => {
f.mockReturnValue(43);
expect(a).toEqual(1);
});
});
< /code>
// asd.ts
import { f } from '../../../api/ApiProvider';
console.log('IMPORTING API PROVIDER', f());
export const a = 1;
< /code>
The issue seems to stem from the fact that when the ApiProvider module is imported, it is cached and uses the initial mocking implementation of vi.mock which is an empty mock function, hence why IMPORTING API PROVIDER returns undefined. The f.mockReturnValue()
Подробнее здесь: https://stackoverflow.com/questions/795 ... -test-case