Cheat Sheet PDF
Cheat Sheet PDF
Cheat Sheet PDF
github.com/kentcdodds/react-testing-library
import {render} from ‘react-testing-library’ getBy Element or Error test('movie title appears', async () => {
// Element is initially not present...
getAllBy Element[] or Error expect(() => getByText(‘aladdin’)).toThrow()
// See render result for more details…
const result = render(<MyComponent />)
queryBy Element or null // Wait for appearance
await wait(() => {
queryAllBy Element[] or [] expect(getByText(‘aladdin’)).toBeTruthy()
})
search the DOM
findBy Promise<Element> or Error
// Wait for appearance and return the result
const {getByText} = render(<span>hello</span>) findAllBy Promise<Element[]> or Error await waitForElement(() => getByText(‘aladdin’))
})
// Pretty print the DOM tree of your render …queries Queries for the baseElement
debug() text matches
❌
✅
const {getByText} = render(<div>Hello World</div>) cleanup the DOM
wait for something
getByText('Goodbye World’) // import ‘react-testing-library/cleanup-after-each’