title: Can't mock Paginate function in jest unit tests
body: <p>I am trying to mock my service's findAll function. For this I either need to mock the repository function <code>findAndCount</code> myEntityRepository or the <code>paginate</code> function of  of nestjs-typeorm-paginate node module. <code>findAll</code> function fetches list of records from table and is paginated using NodeModule <a href="https://www.npmjs.com/package/nestjs-typeorm-paginate" rel="nofollow noreferrer">nestjs-typeorm-paginate</a>.</p>
  <p><strong>Try-1:</strong> Mocking myEntityRepository
  But it fails with error and traceback:</p>
  <pre><code>    TypeError: queryBuilder.limit is not a function
  
        at ../node_modules/nestjs-typeorm-paginate/dist/paginate.js:119:28
        at ../node_modules/nestjs-typeorm-paginate/dist/paginate.js:8:71
        at Object.&lt;anonymous&gt;.__awaiter (../node_modules/nestjs-typeorm-paginate/dist/paginate.js:4:12)
        at paginateQueryBuilder (../node_modules/nestjs-typeorm-paginate/dist/paginate.js:115:12)
        at Object.&lt;anonymous&gt; (../node_modules/nestjs-typeorm-paginate/dist/paginate.js:22:15)
        at ../node_modules/nestjs-typeorm-paginate/dist/paginate.js:8:71
  
  </code></pre>
  <p>my.service.ts</p>
  <pre><code>import { IPaginationOptions, paginate, Pagination } from 'nestjs-typeorm-paginate'
  
  export class MyService {
    constructor(@InjectRepository(MyEntity) private myEntityRepository: Repository&lt;MyEntity&gt;) { }
  
    async findAll(options: IPaginationOptions): Promise&lt;Pagination&lt;MyEntity&gt;&gt; {
      try {
        return await paginate&lt;MyEntity&gt;(this.myEntityRepository, options)
      } catch (error) {
        throw error
      }
    }
  }
  </code></pre>
  <p>my.service.spec.ts</p>
  <pre><code>describe('MyService Basic GET findAll test cases', () =&gt; {
    let service: MyService
    let repositoryMock: MockType&lt;Repository&lt;MyEntity&gt;&gt;
  
    beforeEach(async () =&gt; {
      const module: TestingModule = await Test.createTestingModule({
        providers: [MyService,
          {
            provide: getRepositoryToken(MyEntity), useFactory: repositoryMockFactory
          }
        ],
      }).compile()
  
      service = module.get&lt;MyService&gt;(MyService)
      repositoryMock = module.get(getRepositoryToken(MyEntity))
  
      const itemList = [{
          id: 1,
          my_field: 'a1',
      }, {
          id: 2,
          my_field: 'a2',
      }, ]
  
    it('should findAll() the MyEntity', async () =&gt; {
      expect((await service.findAll(options)).items.length).toBe(itemsList.length)
    })
  })
  
  const repositoryMockFactory: () =&gt; MockType&lt;Repository&lt;MyEntity&gt;&gt; = jest.fn(() =&gt; ({
    find: jest.fn(entity =&gt; entity),
    findAndCount: jest.fn(entity =&gt; entity),
  }))
  
  </code></pre>
  <p><strong>Try-2:</strong> Mocking paginate
  Then I tried mocking paginate method but still it returns error:</p>
  <p><code>    TypeError: Cannot redefine property: paginate at Function.defineProperty (&lt;anonymous&gt;)</code></p>
  <p>my.service.spec.ts with Pagination mock changes)</p>
  <pre><code>import * as nestjsTypeormPaginate from 'nestjs-typeorm-paginate' // imported at top
    ....
    ....
    it('should findAll() the MyEntity', async () =&gt; {
      const queryDto: QueryMyEntityDto = { customerId: 1 }
      const options: IPaginationOptions = { page: 1, limit: 10 }
      let paginationMock = jest.spyOn(nestjsTypeormPaginate, 'paginate')
      paginationMock.mockImplementation((dto, options) =&gt; Promise.resolve({
        items: itemList.slice(0, 2),
        meta: {
          itemCount: 2,
          totalItems: 2,
          totalPages: 1,
          currentPage: 1,
        }
      }))
      repositoryMock.find.mockReturnValue(itemList)
      expect((await service.findAll(options)).items.length).toBe(itemsList.length)
    })
    ...
  </code></pre>
  <p>Before writing this question:
  I tried following posts:</p>
  <ol>
  <li><a href="https://stackoverflow.com/questions/57619182/use-jest-spyon-on-an-exported-function-from-a-node-module">Use `jest.spyOn` on an exported function from a Node module</a></li>
  <li><a href="https://stackoverflow.com/questions/39755439/how-to-mock-imported-named-function-in-jest-when-module-is-unmocked">How to mock imported named function in Jest when module is unmocked</a></li>
  <li><a href="https://github.com/nestjsx/nestjs-typeorm-paginate/issues/143" rel="nofollow noreferrer">https://github.com/nestjsx/nestjs-typeorm-paginate/issues/143</a></li>
  </ol>