web-components

Testing

This doc covers how we test web-components, and what we test with: stories, component tests, and unit tests.

Summary

Running tests

Command What it does
yarn test Runs all Jest tests in watch mode
yarn test-coverage Runs the tests with coverage and writes the report to docs/coverage
yarn start Starts Storybook for visual checks

Tests use Jest (through react-scripts), React Testing Library, @testing-library/user-event, and the @testing-library/jest-dom matchers loaded in src/setupTests.js. Put test files next to the code they test, named *.test.ts or *.test.tsx.

Stories

Stories are where we check how a component looks. They live in src/stories/.

Add a story for each new component, with enough args or variants to show its main states.

Component tests

Component tests check what a user sees and does: interactions, keyboard and focus behavior, form and modal behavior, loading and error states, and accessible roles and names.

Examples:

Unit tests

Examples:

If a component has non-trivial logic (math, state machines, parsing), consider moving it into a plain function and unit testing that. It’s usually easier than testing it through the rendered component.