Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda