Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Jest test case is fainiling in nestjs

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading