- What is
RTL? - What is
RTLgood 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
Jestvs when should you useRTL? - When is a
Jestsnapshot helpful?
Here are links to lessons that should be completed before this lesson:
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?
Participants will be able to:
- Create a testing structure with
Jest+RTL - Create assertion functions
- Generate, display and watch tests
- Become familiar with
Jestsnapshot testing
- Write test assertions using
Jest+RTL - Recognize when to use
RTL - The basic difference between unit and integration testing
Note: How to use these links is described in the Lesson section.
- Read these 2 "React Testing Library: Getting Started" pages. (5 min)
- Work through this freeCodeCamp tutorial (~30 min): React Testing Library – Tutorial with JavaScript Code Examples
- 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.):
-
RTL Tutorial #11 - Integration Tests
You'll be using previous methods a lot, while these next ones are likely necessary only a couple times per application.
-
RTL Tutorial #13 - Mocking Requests - You should not be testing outside apps. Apps should be testing themselves, and this includes your own backend. You don't want to be testing things you have not control over.
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): 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?
-
For non-react JS functions, use plain
Jestinstead. -
If you only want to test the logic of just one CSS rule changing, like just the
fontSizechanging if there's a prop calledtextSize, useReact Testing Library. -
If you want to test behavior, like whether text changes on hover, you should use
React Testing Library.
- For an overview of how this framework compares to others, see the General Testing Framework Comparison Chart.