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?
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.