#1441·chai

Checking multiple properties of the same error

Author: pallavJhaCreated Jan 11, 2022Updated Aug 2, 2025

Hello Folks!

I wanted to know if there is a way to check multiple properties of the same error object.

For example,

javascript
it('undefined params', function () {
    expect(() => {
      validateRequest(undefined, ["ID", "accessToken", "uri"])
    }).to
      .throw(ErrInvalidParams)
      .has.property("code", `E_VALIDATION`)
      .has.property("message", "params are empty")
  });

ErrInvalidParams extends the Error

This(^) test case results in the following error message:

javascript
AssertionError: expected 'E_VALIDATION' to have property 'message'
    at Context.<anonymous> (file:///home/test/request_test.js:74:12)
    at processImmediate (internal/timers.js:461:21)

But it works correctly if the second assertion is removed.

I was not able to find a way:

  • to assert on the multiple properties of an error object, or,
  • to access the error directly, without try/catch, and, set the assertions by myself, or,
  • to pass a callback function that will receive the error as an argument.