Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
How to mock a function inside a nested ES6 imports using jest?

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);
});
  1. Direct call to moduleName uses mock & doesnt call API. All good here.
  2. However, calls to scaleFactor does not use mock & runs real implementation of fetchBalances.

How can I make normalizer to use mocks of fetchBalances ?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda