sandeep singh
sandeep singh

Reputation: 107

Write jest unit testcases for exception handling getting issue on while run

This is my controller class:

        @Put()
            async updateUser(@BodyToClass() user: UpsertUserDto): Promise<UpsertUserDto> {
                try {
                    return await this.userService.updateUser(user);
                } catch (e) {
                    throw new HttpException(e.message, HttpStatus.INTERNAL_SERVER_ERROR);
                }
            }

This is my Usercontroller.spec test class, I am writing the testcases for exception handling for negative cases.

While running getting error on this line:

        ".then(() => done.fail('Client controller should return INTERNAL_SERVER_ERROR 500 '))

Here is my failure message from Jest:

at Env.fail (../node_modules/jest-jasmine2/build/jasmine/Env.js:722:61) at user/user.controller.spec.ts:51:33 at Object. (user/user.controller.spec.ts:50:15)

I am not sure where I am doing mistake or whether there is other way to handle the exception in nestjs.

       it('should throw INTERNAL_SERVER_ERROR if user not update', async (done) => {
          const expectedResult = undefined;
          const testuser =  new  UpsertUserDto();
          testuser.id = '123';
          jest.spyOn(userservice, 'updateUser').mockResolvedValue(expectedResult);
          await usercontroller.updateUser(testuser)
           .then(() => done.fail('Client controller should return INTERNAL_SERVER_ERROR 500 '))
           .catch((error) => {
             expect(error.status).toBe(500);
             expect(error.message).toMatchObject({error: ' INTERNAL_SERVER_ERROR', statusCode: 500});
             done();
            });

    });

Upvotes: 1

Views: 4009

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70061

You're using mockResolvedValue in your jest.spyOn which means that in a .then chain, the .catch will never be executed. Looking at your code, all that's happening it your UserService method returns undefined and as there is no logic around it, the controller then returns that undefined meaning there's no error to be caught. If you are trying to test an error, you should be using mockRejectedValue instead. Also, it's usually not the best practice to mix using async/await with using a done callback, as it can lead to some weird situations.

And lastly, Jest has a built in way to check for errors from a function. Your test could look something like this instead:

it('should throw INTERNAL_SERVER_ERROR if user not update', async () => {
  const testuser =  new  UpsertUserDto();
  testuser.id = '123';
  jest.spyOn(userservice, 'updateUser').mockRejectedValue(new Error('There was an error'));
  await expect(usercontroller.updateUser(testuser)).rejects.toThrow(HttpException);
  await expect(usercontroller.updateUser(testuser)).rejects.toThrow('There was an error');
});

I suggest you spend some time reading Jest's documentation and possibly looking at some examples to get your feet on the ground with testing.

Upvotes: 4

Related Questions