WhoBuilt

Testing & Error Handling

Jul 3, 2020· 33 min· 38K views·Watch on YouTube

Lee Robinson, a developer at Vercel (then Zeit), explains testing and error handling for Next.js in the Learn Next.js course.

Choose tests by value

Unit tests check small pieces of code, integration tests check how components and data work together, and end-to-end tests simulate browser behavior. Robinson recommends tests that resemble real user actions and prioritizing high-value flows, such as login or payment, over pursuing full unit-test coverage without considering the effort.

Use Jest and React Testing Library

Robinson describes Jest as the test runner and React Testing Library as a way to query rendered DOM in a user-oriented way. He demonstrates setting up a shared render helper with Chakra providers, then testing a bar card's text, image, and deal-count badge. He advises using snapshots cautiously, since large snapshots can be hard to review and easy to accept without inspection.

Test a sign-in flow

The sign-in example checks a critical path by entering credentials, submitting the form, and verifying the result. The tests mock the authentication hook and router, then check successful sign-in and navigation, an API error displayed to the user, and validation messages when fields are blank. Robinson recommends limiting mocks while testing user-visible behavior.

Monitor errors and customize the error page

Robinson recommends exception monitoring such as Sentry to catch errors in production and lower environments, noting that Next.js apps need to account for both server-side and client-side errors. He shows how a custom `_error` page can present different experiences for errors such as 404s and 500s, and suggests checking Next.js's Sentry plugin and examples for setup.

Takeaways

  • Prioritize tests for high-value user flows, such as sign-in and payment.
  • Query elements by visible text, labels, roles, or alt text before resorting to test IDs.
  • Use snapshots selectively and keep them small enough for reviewers to understand.
  • Mock external dependencies at a deliberate boundary, then verify both outcomes and visible errors.
  • Add exception monitoring and tailor error pages to help users recover or navigate away.

Mentioned in this talk

Search inside this talk

What was claimed 5

Related talks

Testing & Error Handling · WhoBuilt