Skip to content

Errors thrown in event handlers are not propagating to tests #624

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

Closed
armingjazi opened this issue Mar 27, 2020 · 5 comments
Closed

Errors thrown in event handlers are not propagating to tests #624

armingjazi opened this issue Mar 27, 2020 · 5 comments

Comments

@armingjazi
Copy link

Hey folks,

I'm not sure if this is an issue for this library, react or even jest-dom, so I'm starting here so where it leads:

for the following component

const ClickThrowsComponent = () => {
  const handleOnSectionClick = () => {
    throw new Error('some error')
  };
  return (
    <div data-testid='some-id'
      onClick={handleOnSectionClick}
    />
  );
};

the following test does not pass

test('should throw', () => {
  const { getByTestId } = render(<ClickThrowsComponent />);
  expect(()=>fireEvent.click(getByTestId('some-id'))).toThrow();
});

somehow the error is being swallowed on the way, I have tried almost all combinations with wrapping in act with async/awaits and promises to no avail

any help would be appreciated.

Reproduction repro

@eps1lon
Copy link
Member

eps1lon commented Mar 27, 2020

Thanks for the detailed report.

This is unfortunately not possible since it's inherent to how it works in the DOM.

dispatching an event never throws if a function that handled that error throws. In other terms

Exceptions thrown by event handlers are reported as uncaught exceptions; the event handlers run on a nested callstack: they block the caller until they complete, but exceptions do not propagate to the caller.

-- https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent

but exceptions do not propagate to the caller

This bit being the important part.

Maybe you can find a solution to your underlying problem on StackOverflow.

@eps1lon eps1lon closed this as completed Mar 27, 2020
@charklewis
Copy link

@eps1lon did you manage to find a solution in how to test this?

@eps1lon
Copy link
Member

eps1lon commented Apr 25, 2021

@eps1lon did you manage to find a solution in how to test this?

@charklewis I didn't look for one. You probably want to ask the author of the issue instead.

@armingjazi
Copy link
Author

@charklewis my solution was not to throw at all. As standard suggests the error is not propagated to the caller, which means the exception should be caught in the callback handler and be dealt with there directly (e.g. with window alert if the message is client relevant)

@charklewis
Copy link

Ah ok no worries, makes sense. I have done the same! Thanks for the prompt reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants