NestJs对typeorm分页进行单元测试,得到错误的queryBuilder.take(...).skip不是一个函数。

问题描述 投票:0回答:1

我试图模拟createQueryBuilder来单元测试分页部分,但我得到以下错误信息

queryBuilder.take(...).skip不是一个函数。

我的创建QueryBuilder的Mock

    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
      }), 
  }))

查找所有的单元测试

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
      }]
    });
});

服务项目

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);
}

分页选项 准备功能

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;
}

有谁能帮我模拟 nestjs-typeorm-paginate

unit-testing jestjs nestjs typeorm
1个回答
0
投票

在你的代码中 skip 是传递给函数的一个参数 take. 如果你想 skip 为了成为子对象的属性,你必须这样增强代码

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
         }), 
      })
  }))

0
投票

我可以模拟createQueryBuilder来实现 nestjs-typeorm-paginate 包。

创建QueryBuilder 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]),
  }))
© www.soinside.com 2019 - 2024. All rights reserved.