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

332
Visualizações
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 à 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