jest-extended
jest-extended copied to clipboard
Create a new matcher to run an assertion cb against the thrown error
Feature Request
Description: I'm working on errors with a lot of custom fields on which I want to assert. I could use a plain try{}catch(e){}
to assert against e
but it breaks the arrange/act/assert pattern somehow IMO. Moreover, there is already a matcher that behaves similar to what I would want: toSatisfy
. So, related to #488 , it would be nice to run an assertion cb on the error thrown by a function.
Example:
function doThrow(){
throw new CustomError(
'This is the message',
{foo: 'bar'} // Some context
)
}
expect(() => doThrow()).____(err => {
expect(err.context).toEqual({foo: 'bar'})
})
Possible solution: Add a new matcher for this, something like toThrowErrorSatisfying
that would behave like toSatisfy
, or another name that would not require returning true
.
Again, I would be happy to help
Yes please! Either in jest-extended or Jest core Similar request: https://github.com/jestjs/jest/issues/14732