What is the difference between using `react-testing-library` and `cypress`?

You’ve answered your question in the first line. If you want to test your React app end-to-end, connected to APIs and deployed somewhere, you’d use Cypress. react-testing-library‘s aimed at a lower level of your app, making sure your components work as expected. With Cypress, your app might be deployed on an environment behind CDNs, using … Read more

React testing library how to use waitFor

If you’re waiting for appearance, you can use it like this: it(‘increments counter after 0.5s’, async() => { const { getByTestId, getByText } = render(<TestAsync />); fireEvent.click(getByTestId(‘button-up’)); await waitFor(() => { expect(getByText(‘1’)).toBeInTheDocument(); }); }); Checking .toHaveTextContent(‘1’) is a bit “weird” when you use getByText(‘1’) to grab that element, so I replaced it with .toBeInTheDocument().

Unable to find an element with the text: “myText” error when using react-testing-library

getByText looks for the text inside a node. So in this example: <div class=”foo”>bar</div> the text is bar. getByAltText is the best way to find an image. Another way is to use getByTestId. What if you must find an element by class? In these cases, you can use container which is returned by render. container … Read more

How to test if a component is rendered with the right props when using react-testing-library?

This is the approach that Kent C. Dodds (the creator of RTL) shared with me after discussing it with him: import FetchNextPageButton from ‘FetchNextPageButton’ jest.mock(‘FetchNextPageButton’, () => { return jest.fn(() => null) }) // … in your test expect(FetchNextPageButton).toHaveBeenCalledWith(props, context)

How to test material ui autocomplete with react testing library

First of all, you need to make sure the options are not empty array, then do the following: const autocomplete = getByTestId(‘autocomplete’); const input = within(autocomplete).getByRole(‘textbox’) autocomplete.focus() // the value here can be any string you want, so you may also consider to // wrapper it as a function and pass in inputValue as parameter … Read more