Skip to content

[Feature]: Support for Error.cause in jest.Matchers #12053

@SimonSiefke

Description

@SimonSiefke

🚀 Feature Proposal

Error.cause has been freshly added to the JavaScript language (https://tc39.es/ecma262/#sec-error-message) and makes chaining errors easier than ever.

Motivation

Error.cause is a very useful feature. It would be very useful if it would be possible to test Error.cause with jest

Related: #11935

Example

toThrowError could accept cause as an additional property:

test('getJson - error', async () => {
  const mockError = new SyntaxError('Unexpected token')
  Ajax.getJson = jest.fn(() => {
    throw mockError
  })
  await expect(Ajax.getJson('https://example.com')).rejects.toThrowError({
    message: 'Failed to fetch "https://example.com"',
    cause: mockError, // support this
  })
})

Pitch

It is a core feature of the javascript language and would be very useful.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions