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

275
Visualizações
Jest error in mockImplementation: Type 'string[]' is not assignable to type 'Cat[]'

I'm trying to write a unit test for my API using Jest and I'm using some boilerplate code but it's leading me to an error that I don't understand. Here is the test:

describe('findAll', () => {
it('should return an array of cats', async () => {
  const result = ['test'];
  jest.spyOn(catsService, 'findAll').mockImplementation(() => result);

  expect(await catsController.findAll()).toBe(result);
  });
});

The error is in the part () => result:

Type 'string[]' is not assignable to type 'Cat[]'

My findAll method within catsController is very straightforward, it just return an array of Cat objects:

findAll(): Cat[] {
return this.catsService.findAll();
}

What is incorrect about my mockImplementation?

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

0

Jest knows that you should be returning an array of cat. You tell jest to return an array of string. Type string is not the same as type Cat so you get a TS error. Update the return to be a cat instead of the string "result" and the error will go away.

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