Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
How to catch such an error with jest and testing-library/react

Simply saying I want to assert clicking on this button without onClick prop will result in an error(I know this is a silly case but this is just a rough simplification). But unfortunately I cannot catch this error, no matter how I tried.

The pseudocode looks like this:

Element.js:

const Element = ({ onClick }) => (
<button onClick={() => onClick('test')}>Test</button>)
);

Element.test.js

it('triggers on click callback', () => {
    const wrapper = render(<Element />);

    expect(userEvent.click(wrapper.getByTestId('test'))).toThrowError();
  });

I receive 3 console.error statements: "console.error Error: Uncaught [TypeError: onClick is not a function]"

and a

   expect(received).toThrowError()
    Received function did not throw

This doesn't make much sense to me. Please help me find a solution or at least clarify what is happening

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!