A
A
Artem2018-09-05 20:58:22
JavaScript
Artem, 2018-09-05 20:58:22

How to lock a function that returns a Promise?

Hello.
Project on NestJS ( nestjs.com ). I want to write a test.
I have a controller:

@Get()
async findAll(@Res() res): Promise<any[]> {
  return await this.service.getItems().then(result => res.json(result));
}

I write the corresponding test:
describe('findAll', () => {
    it('should return an array of cats', async () => {
      const result = ['test'];
      jest.spyOn(catsService, 'getItems').mockImplementation(() => Promise.resolve(result));

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

But it gives an error that my getItems mock returns undefined. What am I doing wrong?
Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question