1

I'm writing unit tests for a NestJS Controller and I'm trying to mock its Service. My test suite currently works fine and my tests are passing, but I was wondering if there was a cleaner way to mock the Service as opposed to the current approach I've taken.

I followed the documentation on the NestJS website and defined a mock factory to apply to all of my missing dependencies (NestJS says this is useful for cases where you have a large number of dependencies in a class and mocking all of them will take a long time and a lot of setup). With this mock factory approach, I'm manually mocking every single method I have in my service, example: { findAll: jest.fn(), question: jest.fn(), questions: jest.fn() }. Is there a cleaner way of mocking this Service, without having to manually declare each method in that Service to be equal to a jest.fn()?

I've added an image of my code. Thanks all!

code screenshot

simon777
  • 51
  • 1
  • 5
  • 2
    I believe `useMocker((token) => createMock())` where `createMock` is from `@golevelup/ts-jest` works well – Jay McDoniel Nov 08 '22 at 20:32
  • 1
    Welcome to Stack Overflow! Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. For more information, please see the Meta FAQ entry [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors/285557#285557) – Fastnlight Nov 08 '22 at 21:15

0 Answers0