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

274
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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