nilesh suryavanshi
nilesh suryavanshi

Reputation: 408

NestJs unit testing on typeorm pagination getting error queryBuilder.take(...).skip is not a function

I am trying to mock createQueryBuilder to unit test pagination part, But i am getting below error

queryBuilder.take(...).skip is not a function

My Mock for createQueryBuilder

    createQueryBuilder: jest.fn(() => ({
      delete: jest.fn().mockReturnThis(),
      innerJoinAndSelect: jest.fn().mockReturnThis(),
      innerJoin: jest.fn().mockReturnThis(),
      from: jest.fn().mockReturnThis(),
      where: jest.fn().mockReturnThis(),
      execute: jest.fn().mockReturnThis(),
      getOne: jest.fn().mockReturnThis(),
      orderBy : jest.fn().mockReturnThis(),
      take : skip => ({
       skip: 5
      }), 
  }))

Unit Test for Find all

it("Find All", async () => {
    const pageData: PaginationDto = {
      page: 1,
      limit: 10,
      sortBy: "id",
      sortOrder: -1,
      relatinalData: relatinalData.no

    };
    expect(await service.getGroups(pageData)).toEqual({
      docs: [{
        groupName: "group123",
        description: "Group Description",
        roleId: 1
      },
      {
        groupName: "group123",
        description: "Group Description",
        roleId: 1
      }]
    });
});

Services

import { paginate } from 'nestjs-typeorm-paginate';

async getGroups(paginationDto: PaginationDto) {
        const pageOptions = await this.databaseService.preparePageData(paginationDto, 'groups');
        const queryBuilder = this.groupModel.createQueryBuilder('groups');
        const flag = paginationDto.relatinalData;
        if (relatinalData.yes === flag) {
            queryBuilder.leftJoinAndSelect("groups.role", "role");
        }
        queryBuilder.orderBy(pageOptions.order);
        pageOptions['route'] = environment.hostname + "groups";
        return await paginate(queryBuilder,pageOptions);
}

Pagination page option Prepare function

async preparePageData(paginationDto, modelName) {
    const sort = {};
    if (paginationDto.sortBy && paginationDto.sortOrder) {
        const sortValue = paginationDto.sortBy
        const sortOrder = paginationDto.sortOrder;
        if (paginationDto.relatinalData && relatinalData.yes === paginationDto.relatinalData) {
            sort[modelName + '.' + sortValue] = sortOrder;
        } else {
            sort[sortValue] = sortOrder;
        }
    }
    const options = {
        page: paginationDto.page ? Number(paginationDto.page) : constant.pageLimit,
        limit: paginationDto.limit ? Number(paginationDto.limit) : constant.limit,
        order: sort ? sort : constant.sort
    };
    return options;
}

Can any one please help me to mock nestjs-typeorm-paginate

Upvotes: 4

Views: 5237

Answers (2)

nilesh suryavanshi
nilesh suryavanshi

Reputation: 408

I am able to mock createQueryBuilder for nestjs-typeorm-paginate package.

createQueryBuilder Mock

createQueryBuilder: jest.fn(() => ({
    leftJoinAndSelect: jest.fn().mockReturnThis(),
    orderBy: jest.fn().mockReturnThis(),
    take: jest.fn().mockReturnThis(),
    skip: jest.fn().mockReturnThis(),
    getManyAndCount: jest.fn().mockResolvedValue([groupPageResponse]),
  }))

Upvotes: 5

Yevhenii
Yevhenii

Reputation: 1703

in your code skip is a parameter passed to function take. If you want skip to be a property of child object you have to enhance code in this way

createQueryBuilder: jest.fn(() => ({
      delete: jest.fn().mockReturnThis(),
      innerJoinAndSelect: jest.fn().mockReturnThis(),
      innerJoin: jest.fn().mockReturnThis(),
      from: jest.fn().mockReturnThis(),
      where: jest.fn().mockReturnThis(),
      execute: jest.fn().mockReturnThis(),
      getOne: jest.fn().mockReturnThis(),
      orderBy : jest.fn().mockReturnThis(),
      take : () => ({
         skip: (cnt) => ({
           skip: cnt
         }), 
      })
  }))

Upvotes: 3

Related Questions