I have following two modules.
// exchange.ts
export async function fetchBalances(exchangeIds: string[]): Promise<ExchangeBalance> { /* ... */}
// normalizer.ts
import { fetchBalances } from './exchange';
export async function scaleFactor() {
const balances = await fetchBalances([...]);
// ....
}
I am trying to mock fetchBalances of exchange when testing normalizer as follows:
// 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 uses mock & doesnt call API. All good here.scaleFactor does not use mock & runs real implementation of fetchBalances.How can I make normalizer to use mocks of fetchBalances ?