- Casa already has a working document for writing tests, please check it out if you haven't yet: Unit test cases guideline
- Since the how-to's of writing tests are covered there, this one will focus on the things/errors you might run into, and what to do with them. We'll use examples from Samarpan codebase
- We have
screen.debug()
,screen.logTestingPlaygroundURL()
- Notice that sometimes the logs get cut off/are truncated — use
DEBUG_PRINT_LIMIT=10000 npm run unit /path here/
to display the whole log - You can keep
console.log
in the components themselves — they will log when tests run
- What does
screen.debug()
say? - Are there any API calls in the component you need to await for?
- Mock the API calls, then
await waitFor(() => expect(spy).toHaveBeenCalled())
- Or, you can find an element that only shows up once the APIs are done, like:
const successNotification = await screen.findByTestId('notification-success') expect(successNotification).toBeInTheDocument() expect(successNotification).toHaveTextContent('API has loaded')
- Mock the API calls, then
- Again — are there any API calls in the component you need to await for?
- What does
console.log(data/state)
say?