How to test when text changes after user clicks #828
-
Hi, I am wondering if this is in the right path to testing my carousel. After the user clicks the next button, the text should change; however, I am having trouble testing that and being thrown errors. I've tried Promise with setTimeout, but I am getting
I've already tried using jest fakeTimers and realTimers, even extended the testTimeout in jest.config.js, but I am still getting the error about exceeding time and I'm not sure if I'm testing how my carousel changes text content properly. Here's what my test sort of looks like, for example:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You hit the test timeout because you await new Promise(() => setTimeout(() => undefined, 500)) // This Promise will never be resolved/rejected. The test should probably look something like this: // ...
await userEvent.click(nextAngleButton)
await waitFor(() => { // wait for this function to not throw an error
expect(serviceHeading).toHaveTextContent('service 2')
}) https://testing-library.com/docs/dom-testing-library/api-async/#waitfor |
Beta Was this translation helpful? Give feedback.
You hit the test timeout because you
await
aPromise
that is neversettled
.The test should probably look something like this:
https://testing-library.com/docs/dom-testing-library/api-async/#waitfor