Tengo los siguientes dos módulos.
// exchange.ts export async function fetchBalances(exchangeIds: string[]): Promise<ExchangeBalance> { /* ... */} // normalizer.ts import { fetchBalances } from './exchange'; export async function scaleFactor() { const balances = await fetchBalances([...]); // .... } Estoy tratando de fetchBalances of exchange cuando pruebo el normalizer de la siguiente manera:
// normalizer.test.ts beforeEach(() => { jest.resetModules(); }); async function mockExchange() { jest.doMock('../src/exchange', () => { return { __esModule: true, fetchBalances: async (exchangeIds: string[]): Promise<ExchangeBalance> => { return {/* ... */}; } }; }); return (await import('../src/exchange')); } test('returns 1', async () => { // 1 const moduleName = await mockExchange(); console.log(await moduleName.fetchBalances([...])); // 2 const scaleFactor = await scaleFactor(); expect(scaleFactor).toEqual(1); });moduleName usa simulacro y no llama a la API. Todo bien aquí.scaleFactor no usan la implementación real de fetchBalances . ¿Cómo puedo hacer que el normalizer use simulacros de fetchBalances ?