Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
SimonSiefke opened this issue Nov 8, 2021 · 5 comments · Fixed by #13606
Closed

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

SimonSiefke opened this issue Nov 8, 2021 · 5 comments · Fixed by #13606

Comments

@SimonSiefke
Copy link
Contributor

🚀 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.

@slorber
Copy link
Contributor

slorber commented Oct 26, 2022

It would also be great to support asserting all the chained error-cause messages at once, maybe with toThrowErrorMatchingInlineSnapshot?

I wrote this temporarily (facebook/docusaurus#8234) because I'm not sure how to assert this with Jest in a better way:

  it('docs with invalid id', async () => {
    const error = await testUtils.getProcessDocFileError("some/fake/path");

    expect(error.message).toMatchInlineSnapshot(
      `"Can't process doc metadata for doc at path path=some/fake/path in version name=current"`,
    );
    expect(error.cause).toBeDefined();
    expect(error.cause!.message).toMatchInlineSnapshot(
      `"Document id "Hello/world" cannot include slash."`,
    );
  });

@SimenB
Copy link
Member

SimenB commented Oct 26, 2022

PR very much welcome for this! 🙂

Some discussion in #13260 as well

@ibuibu
Copy link
Contributor

ibuibu commented Nov 11, 2022

I will try this ✋

@SimenB
Copy link
Member

SimenB commented Feb 15, 2023

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants