'mock typeorm findAndCount option's nested querybuilder

as you know typeorm find options, and specifically findAndCount, has a where option which takes a query builder as you can see in the docs. it's really easy to user and create your custom query with it's query builder but the testing of it and mocking it, that is something i couldn't figure out how to do it. so the this is how i used it :

const options: FindManyOptions<UserEntity> = {
      join: {
        alias: 'User',
        innerJoinAndSelect: {
          userLogin: 'User.userLogin',
          payment: 'User.payment',
        },
      },
      where: (queryBuilder: SelectQueryBuilder<UserEntity>) => {
        queryBuilder.andWhere(`${queryBuilder.alias}.isManual = 1`);
        if (!filters) {
          return;
        }
        if (filters.id) {
          queryBuilder.andWhere(`${queryBuilder.alias}.id = :id`, { id: filters.id });
        }
        if (filters.userId) {
          queryBuilder.andWhere(`${queryBuilder.alias}.userId = :userId`, { userId: filters.userId });
        }
      },
      skip: paginationDto.skip,
      take: paginationDto.size,
    };

      options.order = { createdAt: 'DESC' };
    const [users, total] = await this._transactionOnlineRepository.findAndCount(options);

and this is how i tried to mock it :

    Const fakeQueryBuilder= {
join: jest.fn().mockReturnThis(),
where: (p)=> p.whereFactory(<SelectQueryBuilder<UserEntity>>{
 andWhere: jest.fn().mockReturnThis();
})}
const fakeUserRepo = {
findAndCount : jest.fn(()=>fakeQueryBuilder)
};

do you have any idea how to mock this or how to check if my filters are applied?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source