title: Jest expected mock function to have been called
body: <p>I am testing the following service:</p>
  
  <pre><code>import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
  import { InjectRepository } from '@nestjs/typeorm';
  import { extend } from 'lodash';
  import { Repository } from 'typeorm';
  
  import { DriverDTO } from './driver.dto';
  import { DriverEntity } from './driver.entity';
  
  @Injectable()
  export class DriverService {
    private logger = new Logger('DriverService');
    constructor(
      @InjectRepository(DriverEntity)
      private driverRepository: Repository&lt;DriverEntity&gt;,
    ) { }
  
    async create(clientId: string, data: DriverDTO): Promise&lt;Partial&lt;DriverEntity&gt;&gt; {
      let driver = await this.driverRepository.findOne({ where: { clientId, driverId: data.driverId } });
      this.logger.log(driver);
      if (driver) {
        throw new HttpException('Driver already exists', HttpStatus.CONFLICT);
      }
      driver = this.driverRepository.create(extend({ clientId }, data));
      await this.driverRepository.save(driver);
      return { driverId: driver.driverId, createdAt: driver.createdAt };
    }
  }
  </code></pre>
  
  <p>The service.spec.ts is as follows:</p>
  
  <pre><code>import { Test, TestingModule } from '@nestjs/testing';
  
  import { DriverService } from './driver.service';
  import { MockType } from '../mock/mock.type';
  import { plainToClass } from 'class-transformer';
  import { repositoryMockFactory } from '../mock/repositoryMock.factory';
  import { getRepositoryToken } from '@nestjs/typeorm';
  import { DriverEntity } from './driver.entity';
  import { Repository } from 'typeorm';
  import { Pagination } from '../pagination/pagination';
  import { HttpException, HttpStatus } from '@nestjs/common';
  import { DriverDTO } from './driver.dto';
  
  describe('DriverService', () =&gt; {
    let service: DriverService;
    let driverRepositoryMock: MockType&lt;Repository&lt;DriverEntity&gt;&gt;;
  
    beforeEach(async () =&gt; {
      const module: TestingModule = await Test.createTestingModule({
        providers: [
          DriverService,
          { provide: getRepositoryToken(DriverEntity), useFactory: repositoryMockFactory },
        ],
      }).compile();
  
      service = module.get&lt;DriverService&gt;(DriverService);
      driverRepositoryMock = module.get(getRepositoryToken(DriverEntity));
    });
  
    it('should be defined', () =&gt; {
      expect(service).toBeDefined();
      expect(driverRepositoryMock).toBeDefined();
    });
  
    describe('DriverService.create()', () =&gt; {
      const driverDto: DriverDTO = {
        driverId: 'one',
      };
  
      const driver: DriverEntity = plainToClass(DriverEntity, {
        id: 1,
        clientId: 'one',
        driverId: 'one',
        createdAt: '2019-02-11T04:11:00.766Z',
        updatedAt: '2019-02-11T04:11:00.766Z',
      });
  
      it('should create driver if it does not already exist', () =&gt; {
        const response: Partial&lt;DriverEntity&gt; = plainToClass(DriverEntity, {
          driverId: driver.driverId,
          createdAt: driver.createdAt,
        });
  
        driverRepositoryMock.findOne.mockReturnValue(undefined);
        driverRepositoryMock.create.mockReturnValue(driver);
        expect(service.create('one', driverDto)).resolves.toEqual(response);
        expect(driverRepositoryMock.findOne).toHaveBeenCalledTimes(1);
        expect(driverRepositoryMock.findOne).toHaveBeenCalledWith({ where: { clientId: 'one', driverId: driverDto.driverId } });
        expect(driverRepositoryMock.create).toHaveBeenCalledTimes(1);
        expect(driverRepositoryMock.create).toHaveBeenCalledWith({
          clientId: driver.clientId,
          driverId: driver.driverId,
        });
        expect(driverRepositoryMock.save).toHaveBeenCalledWith(driver);
      });
    });
  });
  </code></pre>
  
  <p>But I keep getting the following error:</p>
  
  <pre><code>● DriverService › DriverService.create() › should create driver if it does not already exist
  
      expect(jest.fn()).toHaveBeenCalledTimes(1)
  
      Expected mock function to have been called one time, but it was called zero times.
  
        237 |       expect(driverRepositoryMock.findOne).toHaveBeenCalledTimes(1);
        238 |       expect(driverRepositoryMock.findOne).toHaveBeenCalledWith({ where: { clientId: 'one', driverId: driverDto.driverId } });
      &gt; 239 |       expect(driverRepositoryMock.create).toHaveBeenCalledTimes(1);
            |                                           ^
        240 |       expect(driverRepositoryMock.create).toHaveBeenCalledWith({
        241 |         clientId: driver.clientId,
        242 |         driverId: driver.driverId,
  
        at Object.it (driver/driver.service.spec.ts:239:43)
  
  Test Suites: 1 failed, 1 total
  Tests:       1 failed, 8 passed, 9 total
  Snapshots:   0 total
  Time:        1.762s, estimated 6s
  Ran all test suites matching /src\/driver\/driver.service.spec.ts/i.
  </code></pre>
  
  <p>I mocked the repository like this:</p>
  
  <pre><code>import { Repository, Entity } from 'typeorm';
  import { MockType } from './mock.type';
  
  // @ts-ignore
  export const repositoryMockFactory: () =&gt; MockType&lt;Repository&lt;any&gt;&gt; = jest.fn(() =&gt; ({
      findOne: jest.fn(entity =&gt; entity),
      findAndCount: jest.fn(entity =&gt; entity),
      create: jest.fn(entity =&gt; entity),
      save: jest.fn(entity =&gt; entity),
  }));
  </code></pre>
  
  <p>I can't seem to find the cause. Any help will be appreciated.</p>