Home > other >  Jest test case is failing in nestjs
Jest test case is failing in nestjs

Time:02-11

I have below method in nestjs in service class.

async findOne(id: string): Promise<Certificate> {
    const result = await this.certificateRepository.findOne({ id });
    if (!result) {
        throw new NotFoundException(`Certificate with ID does not exist`);
    }
    return result;
}

I have writtern below test case for the same.

it('should find one certificate', async () => {
        const id = '1';
        const certificate = await service.findOne(id);
        expect(repository.findOne).toHaveBeenCalledTimes(1);
        expect(repository.findOne).toHaveBeenCalledWith(id);
        expect(certificate).toBeInstanceOf(Certificate);
    });

I am getting below error on the same.

enter image description here

I am not sure why it is returning object instead of value. if I cant modify the method can I modify this test case to retunr object?

WHat changes I should do in my testcase?

CodePudding user response:

Update the assertion to be the following:

expect(repository.findOne).toHaveBeenCalledWith({ id: "1" });

This will match what how the code uses id to call certificateRepository.findOne.

  • Related