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

535
Visualizações
Error: Uncaught [TypeError:default is not a constructor] jest mock

I have a service class FooService.js which I'm using in my component

export default class FooService {
  saveInfo = (params) => {
    //method implementation here.
  };
}

I'm using this in my component Foo.tsx in saveData method as below:

const saveData = (message: string) => {
    const fooService = new FooService();
    fooService.saveInfo(params);
  };

I'm trying to mock my FooService as below in Foo.test.tsx

jest.mock('src/js/services/FooService', () => {
  return {
    _esModule: true,
    default: jest.fn().mockImplementation(() => {
      return {
        saveInfo: jest.fn(),
      };
    }),
  };

When I run my test case from Foo.test.tsx file in debug mode, I'm able to get inside saveData method. But, when I hit const fooService = new FooService(); line, it throws the following error: TypeError: FooService_1.default is not a constructor

Please let me know what is that I'm missing in jest.mock. Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Replacing _esModules with __esModules solved the issue.

about 4 years ago · Juan Pablo Isaza Relatório
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