Vibhor Verma
Vibhor Verma

Reputation: 173

Can't mock Paginate function in jest unit tests

I am trying to mock my service's findAll function. For this I either need to mock the repository function findAndCount myEntityRepository or the paginate function of of nestjs-typeorm-paginate node module. findAll function fetches list of records from table and is paginated using NodeModule nestjs-typeorm-paginate.

Try-1: Mocking myEntityRepository But it fails with error and traceback:

    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.<anonymous>.__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.<anonymous> (../node_modules/nestjs-typeorm-paginate/dist/paginate.js:22:15)
      at ../node_modules/nestjs-typeorm-paginate/dist/paginate.js:8:71

my.service.ts

import { IPaginationOptions, paginate, Pagination } from 'nestjs-typeorm-paginate'

export class MyService {
  constructor(@InjectRepository(MyEntity) private myEntityRepository: Repository<MyEntity>) { }

  async findAll(options: IPaginationOptions): Promise<Pagination<MyEntity>> {
    try {
      return await paginate<MyEntity>(this.myEntityRepository, options)
    } catch (error) {
      throw error
    }
  }
}

my.service.spec.ts

describe('MyService Basic GET findAll test cases', () => {
  let service: MyService
  let repositoryMock: MockType<Repository<MyEntity>>

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [MyService,
        {
          provide: getRepositoryToken(MyEntity), useFactory: repositoryMockFactory
        }
      ],
    }).compile()

    service = module.get<MyService>(MyService)
    repositoryMock = module.get(getRepositoryToken(MyEntity))

    const itemList = [{
        id: 1,
        my_field: 'a1',
    }, {
        id: 2,
        my_field: 'a2',
    }, ]

  it('should findAll() the MyEntity', async () => {
    expect((await service.findAll(options)).items.length).toBe(itemsList.length)
  })
})

const repositoryMockFactory: () => MockType<Repository<MyEntity>> = jest.fn(() => ({
  find: jest.fn(entity => entity),
  findAndCount: jest.fn(entity => entity),
}))

Try-2: Mocking paginate Then I tried mocking paginate method but still it returns error:

TypeError: Cannot redefine property: paginate at Function.defineProperty (<anonymous>)

my.service.spec.ts with Pagination mock changes)

import * as nestjsTypeormPaginate from 'nestjs-typeorm-paginate' // imported at top
  ....
  ....
  it('should findAll() the MyEntity', async () => {
    const queryDto: QueryMyEntityDto = { customerId: 1 }
    const options: IPaginationOptions = { page: 1, limit: 10 }
    let paginationMock = jest.spyOn(nestjsTypeormPaginate, 'paginate')
    paginationMock.mockImplementation((dto, options) => 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)
  })
  ...

Before writing this question: I tried following posts:

  1. Use `jest.spyOn` on an exported function from a Node module
  2. How to mock imported named function in Jest when module is unmocked
  3. https://github.com/nestjsx/nestjs-typeorm-paginate/issues/143

Upvotes: 5

Views: 4580

Answers (1)

moussamango
moussamango

Reputation: 96

I resolved this issue by mocking the external module through another way:

my.service.spec.ts

// At the top level of your unit test file, before the import of the service

// Declare your itemList
const itemList = ['item1', 'item2', 'item3', 'item4'];

// Mock the external module and the paginate function
jest.mock('nestjs-typeorm-paginate', () => ({
  paginate: jest.fn().mockResolvedValue({
      items: itemList.slice(0, 2),
      meta: {
        itemCount: 2,
        totalItems: 2,
        totalPages: 1,
        currentPage: 1,
      }
    }),
}));

// ... unit tests

This works for my situation. I got some ideas from https://stackoverflow.com/a/43090261.

Upvotes: 4

Related Questions