sschottler / async-testing

demo repo for presentation on async testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Async Testing

This repo contains example tests to demonstrate how to test handling async UI updates in React components

TLDR

Video recordings of the presentations

Watch the videos below and follow along in the code:

Running the tests

  • git clone this repo
  • npm install once to setup node_modules
  • Add a .only to the test you want to run (test.only) so the console isn't cluttered by other tests
  • npm test
  • Start with promise.test, which demonstrates testing promises in isolation
  • The tests are designed to be self-documenting and read through top to bottom building on concepts demonstrated in earlier tests
  • Move on to async-react.test which demonstrates setting up assertions for UI updates in React components that happen async
  • Move on to act.test to learn more about why it's rarely necessary

Setting breakpoints in the tests

  • Open in VS Code and click the "run and debug" icon on the sidebar
  • Open test file you want to run and click green play icon for "Jest Current File" OR run "Jest CRA Tests" Debugging tests

Async/Await Syntax

This is a good tutorial to get more comfortable with async/await syntax for working with promises: https://javascript.info/async-await

Mastering the Event Loop, Microtasks, Macrotasks, etc.

If these terms are fuzzy to you, the good news is you don't really have to understand them if you use react testing library's async utils such as waitFor. They allow you to think about your test from a user's perspective (ie this data takes a little bit to appear after I click this button). If you're curious and would like to deepen your understanding, I have found these resources to be extremely helpful:

About

demo repo for presentation on async testing


Languages

Language:TypeScript 90.6%Language:HTML 6.1%Language:CSS 3.2%