'mocking getting failed in nestjs

'I have below method in nestjs.

  async findAll(queryCertificateDto: QueryCertificateDto): Promise<Certificate[]> {
        const { requestStatus, startDate, endDate } = queryCertificateDto;

        const query = this.certificateRepository.createQueryBuilder('certificate');

        if (requestStatus) {
            query.andWhere('certificate.requestStatus=:requestStatus', {
                requestStatus,
            });
        }

        if (startDate && endDate) {
            query.andWhere('CAST(certificate.createdAt as Date) BETWEEN :startDate AND :endDate', {
                startDate,
                endDate,
            });
        } else if (startDate) {
            query.andWhere('CAST(certificate.startDate as Date)=:startDate', { startDate });
        } else if (endDate) {
            query.andWhere('CAST(certificate.endDate as Date)=:endDate', { endDate });
        }

        const certificates = await query.getMany();
        return certificates;
    }

and I am writing below test case

 let service: CertificatesService;
    let repository: Repository<Certificate>;

    beforeEach(async () => {
        const module = await Test.createTestingModule({
            providers: [
                CertificatesService,
                {
                    provide: getRepositoryToken(Certificate),
                    useValue: {
                        find: jest.fn().mockReturnValue([new Certificate()]),
                        findOne: jest.fn().mockReturnValue(new Certificate()),
                        save: jest.fn().mockReturnValue(new Certificate()),
                        softRemove: jest.fn().mockReturnValue(new Certificate()),
                    },
                },
            ],
        }).compile();

        service = module.get<CertificatesService>(CertificatesService);
        repository = module.get<Repository<Certificate>>(getRepositoryToken(Certificate));
    });

test case:-

it('should find all certificate', async () => {
    const queryCertificateDto = new QueryCertificateDto();
    const certificate = await service.findAll(queryCertificateDto);
    expect(repository.find).toHaveBeenCalledTimes(1);
    expect(repository.find).toHaveBeenCalledWith({ where: queryCertificateDto });
    expect(certificate).toEqual(expect.arrayContaining([new Certificate()]));
});

it is getting failed with below error.

enter image description here

so I changed it like below. then I added at top

createQueryBuilder: jest.fn().mockReturnValue([new Certificate()]),

and wrote below test case.

it('should find all certificate', async () => {
    const queryCertificateDto = new QueryCertificateDto();
    const certificate = await service.findAll(queryCertificateDto);
    expect(repository.createQueryBuilder).toHaveBeenCalledTimes(1);
    expect(repository.createQueryBuilder).toHaveBeenCalledWith({ where: queryCertificateDto });
    expect(certificate).toEqual(expect.arrayContaining([new Certificate()]));
});

now it is giving error on getMany()

enter image description here

what additional thing I need to add in my test case?



Sources

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

Source: Stack Overflow

Solution Source