Minh Ngo
Minh Ngo

Reputation: 289

How can I test error handing without try-- catch

I am writing code that validates texts. This is how I test it. But I don't want to use try--catch in unit testing. Please give me a better way to do it.

it('Testing if hook errors on invalid description.', async () => {
      try {
        workRequest.requestor = 'John';
        workRequest.description = 1;
        result = await app.service('dummy').create(workRequest);
      } catch (error) {
        assert.equal(error.errors.description, 'Description is must be a string');
      }
    });

Upvotes: 2

Views: 82

Answers (1)

Naor Tedgi
Naor Tedgi

Reputation: 5699

what you looking for is somthing like this should.throws almost all tests framework supports this API

for example :

shouldjs

https://shouldjs.github.io/#should-throws

mocha

https://mochajs.org/#bdd

and also

https://nodejs.org/api/assert.html#assert_assert_throws_fn_error_message

 it('Testing if hook errors on invalid description.', async () => {
        assert.throws(  () =>  {
            workRequest.requestor = 'John';
            workRequest.description = 1;
            result = await app.service('dummy').create(workRequest);
        },
  err 
);

Upvotes: 3

Related Questions