title: SpyOn TypeORM repository to change the return value for unit testing NestJS
body: <p>I would like to unittest corner cases for my TypeORM database calls. I have already mocked all my TypeORM repositories with valid data. But I would like to SpyOn the repository and change the return value form TypeORM. How do I do that?</p>
  <pre class="lang-js prettyprint-override"><code>import {INestApplication} from '@nestjs/common';
  import {Test} from '@nestjs/testing';
  import {CommonModule} from '@src/common/common.module';
  import {AuthService} from './auth.service';
  import {Repository} from 'typeorm';
  import {V3User} from '@src/database/entity/user.v3entity';
      
  describe('AuthService', () =&gt; {
      let service: AuthService;
      let app: INestApplication;
          
      beforeEach(async () =&gt; {
          const module = await Test.createTestingModule({
              imports: [CommonModule.forRoot(`${process.env.DEV_ENV}`)],
              providers: [
                  AuthService,     
                  {provide: 'V3USER_REPOSITORY', useValue: mockRepositoryV3User()},
              ],
          }).compile();
      
          app = module.createNestApplication();
          await app.init();
      
          service = module.get&lt;AuthService&gt;(AuthService);
      });
      
  
      
      it('test auth service - with non existing user in v3 db', async () =&gt; {
                  
          jest.spyOn(?????? , 'findOne').mockImplementation(() =&gt; undefined);
      
          const res = await service.loginUser(&quot;bad token&quot;);
      
          await expect(service.tokenBasedAuth('example bad token'))
              .rejects.toThrow('bad token exception');
      });
  });
  </code></pre>
  <p>I mock the database like this for normal test cases:</p>
  <pre class="lang-js prettyprint-override"><code>export const mockRepositoryV3User = () =&gt; ({
      metadata: {
          columns: [],
          relations: [],
      },
      findOne: async () =&gt;
          Promise.resolve({
              id: 3,
              email: 'email@example.com',
              first_name: 'david',
              last_name: 'david',
              last_login: '2019-07-15',
              date_joined: '2019-07-15',
          }),
  });
  </code></pre>