无法在玩笑单元测试中模拟 Paginate 函数

Vib*_*rma 5 unit-testing typescript jestjs typeorm nestjs

我正在尝试模拟我的服务的 findAll 功能。为此,我需要模拟存储库函数findAndCountmyEntityRepository 或paginateNestjs-typeorm-paginate 节点模块的函数。findAll函数从表中获取记录列表,并使用 NodeModule Nestjs-typeorm-paginate进行分页。

Try-1:模拟 myEntityRepository 但它失败并出现错误和回溯:

    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

Run Code Online (Sandbox Code Playgroud)

我的服务.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
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我的服务规格

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

Run Code Online (Sandbox Code Playgroud)

Try-2:模拟分页然后我尝试模拟分页方法,但仍然返回错误:

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

my.service.spec.ts 与分页模拟更改)

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)
  })
  ...
Run Code Online (Sandbox Code Playgroud)

在写这个问题之前:我尝试了以下帖子:

  1. 在 Node 模块导出的函数上使用 `jest.spyOn`
  2. 当模块未模拟时,如何在 Jest 中模拟导入的命名函数
  3. https://github.com/nestjsx/nestjs-typeorm-paginate/issues/143

小智 4

我通过另一种方式模拟外部模块解决了这个问题:

我的服务规格

// 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

Run Code Online (Sandbox Code Playgroud)

这适合我的情况。我从/sf/answers/3016318301/得到了一些想法。