title: NestJS Unit Testing for Controller: Mocking the Delete from Service Not Working
body: <p>I am currently unit testing my NestJS Service. My entity is called 'User' and I established a basic Service that allows me to interact with a MS SQL server, with GET and POST endpoints established in my Controller.</p>
  <p>I have a <code>delete</code> method in my Controller file that basically just calls the <code>delete</code> method from my Service:</p>
  <pre><code>@Injectable()
  export class ServiceName {
    constructor(@InjectEntityManager() private manager: EntityManager) {}
  
  
    async delete(id: number): Promise&lt;any&gt; {
      return await this.manager.delete(User, id);
    }
  // some other functions 
  
  </code></pre>
  <p>To test the <code>delete</code> method of my Controller, I mock the same method from the Service. However, when I defined a clear mock return value for the delete method from the service, this mocked return value does not match the return value from the delete method from the Controller, although they should be the same.</p>
  <p>Here are my configurations in my spec.ts file:</p>
  <pre><code>    const mockService = {
          delete: jest.fn(),
      };
  
      beforeEach(async () =&gt; { 
          const module: TestingModule = await Test.createTestingModule({
              controllers: [ControllerName],
              providers: [ServiceName],
          })
              .overrideProvider(ServiceName)
              .useValue(mockService) 
              .compile();
  
          service = module.get&lt;ServiceName&gt;(ServiceName);
          controller = module.get&lt;ControllerName&gt;(ControllerName);
      });
  </code></pre>
  <p>Here's the test case in my spec.ts file:</p>
  <pre><code>        it('testing delete', async () =&gt; {
              mockService.delete.mockReturnValue(5);
              expect(controller.delete(5)).toEqual(5);
          }) 
  </code></pre>
  <p>The above test case fails, and below is shown the error message:</p>
  <pre><code>    expect(received).toEqual(expected) // deep equality
  
      Expected: 5
      Received: {}
  
        122 |         it('testing delete', async () =&gt; {
        123 |             mockService.delete.mockReturnValue(5);
      &gt; 124 |             expect(controller.delete(5)).toEqual(5);
            |                                          ^
        125 |         })
        126 |     });
        127 | });
  </code></pre>
  <p>Any help is greatly appreciated!</p>