I am building out tests in my React app that was built with create-react-app and I'm trying to understand what the difference is between screen.getByText()
and simply getByText()
. For instance, the documentation shows this kind of example:
import React from 'react';
import { render, screen } from '@testing-library/react';
import App from './App';
it('renders a title of "Dashboard"', () => {
render(<App />);
expect(screen.getByText('Dashboard')).toBeInTheDocument();
});
But I see that I can do this:
it('renders a title of "Dashboard"', () => {
const { getByText } = render(<App />);
expect(getByText('Dashboard')).toBeInTheDocument();
});
I haven't found what the difference is between using screen.getByText()
vs. simply using getByText()
like I've shown above.
What are the differences, and are there advantages in using one over the other in various scenarios?