Recently, I've been writing a unit test for my API using Jest and leveraging some boilerplate code. However, I hit a snag when an error popped up that left me scratching my head. Here is the snippet of code that's causing trouble:
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 points to this part: () => result
The error message reads: 'Type 'string[]' is not assignable to type 'Cat[]'
In catsController, the findAll method returns an array of Cat objects:
findAll(): Cat[] {
return this.catsService.findAll();
}
I'm puzzled about what might be wrong with my mockImplementation. Can anyone provide insights?