This is React's recommended testing library, and comes installed as default in the React Example App. It is quite simple to use and does share some of the JEST syntax.
import { render, screen } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
/* App.test.js */
import React from 'react';
import { render, screen } from '@testing-library/react';
import App from './App';
test('Example App Test', () => {
render(<App />);
// TESTS HERE *
})
test('Test if name input exists', () => {
render(<App />);
const nameInput = screen.getByLabelText('Name');
expect(nameInput).toBeInTheDocument();
expect(nameInput.type).toBe('text');
})
<aside> 💡 All of the syntax is pretty straight forward. If anything, do not hesitate to consult the API or search for any topic in particular.
</aside>
screen.getbyText( [regex] || /[string]/i ) // /i === !caseSensitive;
screen.getbyTestId( [string] ) // element.data-testid === [string];
expect( [screenElement] ).toBeInTheDocument();
expect( [screenElements] ).toHaveLength( [number] );
screen.getByRole('button') //type button && only 1;
screen.getAllByRole('button') //type button && all;
expect( [screenElement] ).toHaveProperty('type', 'text') || expect(it.type).toBe('text');