Skip to content

Latest commit

 

History

History
109 lines (73 loc) · 5.68 KB

react-testing-jest-and-RTL.md

File metadata and controls

109 lines (73 loc) · 5.68 KB

Testing React Components with Jest and React Testing Library

Week 8 Keywords and Questions

  • What is RTL?
  • What is RTL good for?
  • What is the difference between Unit and Integration testing?
  • What is an “assertion” in testing?
  • What edge cases should I always keep in mind?
  • When should you use Jest vs when should you use RTL?
  • When is a Jest snapshot helpful?

Prerequisites

Here are links to lessons that should be completed before this lesson:

Motivation

Learn a commonly used React testing tool.

Jest is designed to test JS and React code. React Testing Library (RTL) is a great tool to use with Jest to test that your component is rendering and behaving as expected from the perspective of the user, as opposed to the perspective of the developer. The idea is that the most critical things that you don't want to break are the experience of the end user - for example, do you want a test for a button to fail when its internal function name changes or gets refactored? It's probably more important that the component renders at all, that it renders with the right styles, and it behaves correctly when clicked.

Which companies use Jest + RTL testing?

Objectives

Participants will be able to:

  • Create a testing structure with Jest + RTL
  • Create assertion functions
  • Generate, display and watch tests
  • Become familiar with Jest snapshot testing

Specific Things to Learn

  • write test assertions using Jest + RTL
  • recognize when to use RTL
  • the basic difference between unit and integration testing

Materials

Note: How to use these links is described in the Lesson section.

Lesson

React Testing Library

  1. Read these 2 "React Testing Library: Getting Started" pages. (5 min)

  2. Work through this freeCodeCamp tutorial (~30 min): React Testing Library – Tutorial with JavaScript Code Examples

  3. Follow along with this video series by The Net Ninja (~ 90 min. Each one is 5-15 min. Feel free to take a break at some point between videos.):

    You'll be using previous methods a lot, while these next ones are likely necessary only a couple times per application.

Jest Snapshots

Follow just these 2 sections about snapshot testing. Use inside any of your practice apps from this outline or the Jest outline (10 min):

Read this, but no need to try it now. This may come in handy later. (2 min): https://kentcdodds.com/blog/effective-snapshot-testing#snapshot-diff

When should I use Jest snapshots?

  • Most of the time you want to test CSS in your component.
  • To test the overall structure of your component, and how major variations differ. For example, if you have a component that can switch between a light and a dark theme, you probably want 1 snapshot per theme.
  • Breakpoint differences! For example, when mobile layout is different then desktop layout, you should have 2 snapshots.

When should I not use Jest snapshots?