Jest: Testing promise rejection error code

Created on 25 Sep 2017  Â·  7Comments  Â·  Source: facebook/jest

First, I love jest and you guys have made a great work here. In Jest 21.0 .toMatchObject() changed the way it handles Errors. I used to do this in Jest 20-:

const pub = parse(schema, { public: 25 });
await expect(pub).rejects.toMatchObject({ code: 'ERROR_CODE' });

But now I have to pass the same full Error and cannot pass just a part of it. Some times errors are easy and can be built in the spot, but some times they might have more properties. An alternative would be to be able to access the object (error) returned by rejects but as noted here the resolve/reject value cannot be accessed like this with Jest.

This was really useful for testing just the error code, is there an equivalent way of doing this in Jest 21 without writing too much boilerplate? My suggestion: to add a matcher for errors. Something like this:

  • .rejects.withErrorCode()

Or even more generic like this:

  • .rejects.withError({ code: 'ERROR_CODE' })

Or just leave the specific case when there is a plain object being passed to compare by properties instead of by the full thing:

  • .rejects.toMatchObject({ code: 'ERROR_CODE' })

Any of these situations would make IMHO testing for rejection errors much easier. I know that I can write my own extension, but I think testing for rejection error codes should be fairly commonplace. If I am missing something and there is an easy way of testing for rejection codes please let me know.

Most helpful comment

This works:

test('check code property', () => {
  const err = new Error();
  err.code = 404;

  return expect(Promise.reject(err)).rejects.toHaveProperty('code', 404);
});

All 7 comments

Reference: this was a small workaround for 20- as seen in here: https://github.com/facebook/jest/issues/3601

Now there is no workaround anymore.

I recommend changing your assertions to .toEqual(new Error(…)) instead of just testing for one property.

Those are no plain Errors and I do not have access to the original constructor in the client code, so .toEqual(new ???(…)) is just not possible.

The error instances are generated by sub-libraries that do not expose the constructor. The only assurance is that the error code is that one, and that is the reason for me to test it by one property.

Any recommendation in that situation? I am pretty sure this should be fairly common.

PS, haven't tested it yet but wouldn't the stack trace be different, and so the .toEqual() be always false? I'll test that out later on.

This works:

test('check code property', () => {
  const err = new Error();
  err.code = 404;

  return expect(Promise.reject(err)).rejects.toHaveProperty('code', 404);
});

@SimenB this example not works:

TypeError: Cannot read property 'toHaveProperty' of undefined

It works: https://repl.it/@SimenB/ComplexGainsboroGnudebugger

You either have a typo or you're on a really old version of Jest

Ops! my global jest version is 19, now updated and works fine! Thanks :)

Was this page helpful?
0 / 5 - 0 ratings