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

320
Vistas
Using jest.spyOn with import module syntax

I'm writing some tests for my node application with Jest.

My unit tests rely on spyOn to mock some functions:

const instrumentsData = require('../../../../dataAccess/instrumentsData');

describe('Test updateInstrument', () => {
    let getSpyInstruments: jest.SpyInstance;

    it('Update instrument - success', async () => {
        getSpyInstruments = jest.spyOn(instrumentsData, 'update');


        getSpyInstruments.mockReturnValueOnce({_id: 'testID', ...mockInstrumentParams});

InstrumentsData is a module that exports various function, like update.

Since I want to use the import syntax I changed the import like the following:

import * as instrumentsData from '../../../../dataAccess/instrumentsData';

With instrumentsData it works, because there are multiple functions being returned, without a default export. If I'm doing the same with a file with a single default export (like currenciesData, that contains the default export for findOneCurrency), it triggers this error:

        getSpyCurrencies = jest.spyOn(currenciesData, 'findOneCurrency');

No overload matches this call. Overload 1 of 4, '(object: typeof import("project/src/dataAccess/currenciesData"), method: "default"): SpyInstance<Promise<LeanDocument<ICurrencyModel & { _id: any; }>>, [id: ...]>', gave the following error. Argument of type '"findOneCurrency"' is not assignable to parameter of type '"default"'. Overload 2 of 4, '(object: typeof import("project/src/dataAccess/currenciesData"), method: never): SpyInstance<never, never>', gave the following error. Argument of type 'string' is not assignable to parameter of type 'never'.ts(2769)

How can I fix this?

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