You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Intro to React Testing: Provide background context for UI testing (#30146)
* Write section discussing value behind UI testing
Lesson currently missing background context - just dives straight into
how to write the tests.
* Mention "rendering" with jsdom
Helpful context as to what's actually being rendered during our tests and how
* Point out comparison with i-regex instead of with string
Copy file name to clipboardExpand all lines: react/react_testing/introduction_to_react_testing.md
+12-2Lines changed: 12 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,19 @@ We've learned how to test our vanilla JavaScript applications in a previous sect
6
6
7
7
This section contains a general overview of topics that you will learn in this lesson.
8
8
9
+
- Why UI testing is valuable.
9
10
- How to set up a React testing environment.
10
11
- How to test UI elements.
11
12
- Understanding snapshot tests.
12
13
14
+
### UI testing
15
+
16
+
By now, you'll have had some experience already with TDD and the idea of testing in general. You may remember in the Battleship project, you will have written tests limited to the underlying Battleship game, but without involving the DOM at all. You may have made non-UI changes, caused tests to fail, then resolved whatever was needed so you had tests that were relevant and passed.
17
+
18
+
Despite this, you may have run into bugs in the UI because your tests only involved the underlying logic. You may have a working Battleship game, but that does not mean the UI actually displays what you intend nor lets users interact with the game as intended. And let's be honest, every time you make a change to any part of your code (whether related to UI or not), you're not going to remember to recheck every related UI aspect to make sure everything still works as you expect. i.e., this is no different to before, we're just involving the UI now.
19
+
20
+
UI tests give us more confidence that our websites contain the intended contents and behave as we want, and notify us when something no longer satisfies our requirements. Perhaps you decide to change the exact structure of a state, such as changing from a plain object to an array, but now a list does not contain the text you intend. Or maybe you add a condition somewhere in a component and now suddenly, only the last three drop targets for your drag and drop cards are no longer valid targets. As your websites get more complex, the value of good tests (both UI and non-UI) will only increase.
21
+
13
22
### Setting up a React testing environment
14
23
15
24
Follow along [Robin Wieruch's guide on setting up Vitest with RTL](https://www.robinwieruch.de/vitest-react-testing-library/). Once you've completed the setup, let's meet back here.
@@ -30,7 +39,7 @@ Now that we have everything we need, let's briefly go over what some of those pa
30
39
31
40
### Our first query
32
41
33
-
First, we'll render the component using `render`. The API will return an object and we'll use destructuring syntax to obtain a subset of the methods required. You can read all about what `render`can do in [the React Testing Library API docs about render](https://testing-library.com/docs/react-testing-library/api/#render).
42
+
First, we'll render the component using `render` (you can read all about what the function can do in [the React Testing Library API docs about `render`](https://testing-library.com/docs/react-testing-library/api/#render)). We say "render", but you won't see any screen. Back in the RTL setup article just now, one of the packages installed is `jsdom`, which simulates the DOM (including events) in memory without actually laying anything out visually like in a browser, and that allows us to parse its contents for our tests. This is different to and much less complex than other libraries that actually run a real browser environment for more complex end-to-end tests - that's out of scope here.
34
43
35
44
```jsx
36
45
// App.jsx
@@ -50,10 +59,10 @@ import App from "./App";
50
59
describe("App component", () => {
51
60
it("renders correct heading", () => {
52
61
render(<App />);
62
+
// using regex with the i flag allows simpler case-insensitive comparison
53
63
expect(screen.getByRole("heading").textContent).toMatch(/our first test/i);
54
64
});
55
65
});
56
-
57
66
```
58
67
59
68
<divclass="lesson-note"markdown="1">
@@ -185,6 +194,7 @@ Even though some articles use Jest and the Enzyme testing library, the concepts
185
194
186
195
The following questions are an opportunity to reflect on key topics in this lesson. If you can't answer a question, click on it to review the material, but keep in mind you are not expected to memorize or master this knowledge.
187
196
197
+
-[Why might you want to test your UI?](#ui-testing)
188
198
-[What packages are required for React testing?](#setting-up-a-react-testing-environment)
189
199
-[What is the significance of the user-event package?](#user-event)
190
200
-[What does the `render` method do?](https://testing-library.com/docs/react-testing-library/api/#render)
0 commit comments