Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!