jest-extended icon indicating copy to clipboard operation
jest-extended copied to clipboard

Create a new matcher to run an assertion cb against the thrown error

Open GerkinDev opened this issue 1 year ago • 1 comments

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

GerkinDev avatar Aug 10 '22 15:08 GerkinDev

Yes please! Either in jest-extended or Jest core Similar request: https://github.com/jestjs/jest/issues/14732

maxime-dupuis avatar Nov 30 '23 20:11 maxime-dupuis