Skip to content

Instantly share code, notes, and snippets.

@caye-telus
Last active June 30, 2022 12:27
Show Gist options
  • Save caye-telus/47237f7badacb38f13fb77bf210913b4 to your computer and use it in GitHub Desktop.
Save caye-telus/47237f7badacb38f13fb77bf210913b4 to your computer and use it in GitHub Desktop.
Troubleshooting while writing tests

Some notes

  1. Casa already has a working document for writing tests, please check it out if you haven't yet: Unit test cases guideline
  2. 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

Troubleshooting writing tests

1. Debugging basics

  • 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

2. What to do when component doesn’t render

  • 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')
    

3. What to do when component renders but data did not load

  • Again — are there any API calls in the component you need to await for?
  • What does console.log(data/state) say?

4. Checking if state changes work (local or redux)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment