vendredi 7 février 2020

How to test throw new HttpException in unit test Nest.js

I'm trying to test a service in my Nest.js app. In some of my methods of this service I have some cases where I throw new HttpException depending of the context.

I'm trying to write unit tests of these methods and I don't know how to test cases where I throw HttpException.

I've tried this code:

it('Shound return error for a non existing id provided', async () => {
      await expect(service.getUser('false Id')).rejects.toThrow(new HttpException('This user does not exist', 404));
});

But I received:

Expected the function to throw an error matching:
      [Error: This user does not exist]
    Instead, it threw:
      Error: This user does not exist

Does anyone already encounter this use case ?

Aucun commentaire:

Enregistrer un commentaire