-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Mastering React Test-Driven Development
By :

Now that you’ve written a couple of tests, let’s step away from the keyboard and discuss what you’ve seen so far.
Your first test looks like the following example:
it("renders the customer first name", () => { const customer = { firstName: "Ashley" }; render(<Appointment customer={customer} />); expect(document.body.textContent).toContain("Ashley"); });
This is concise and clearly readable.
A good test has the following three distinct sections:
This is so well understood that it is called the Arrange, Act, Assert (AAA) pattern, and all of the tests in this book follow this pattern.
A great test is not just good but is also the following:
In the remainder of this section, we’ll discuss the TDD cycle, which you’ve already used, and also how to set up your development environment for easy TDD.
TDD, at its heart, is the red, green, refactor cycle that we’ve just seen.
Figure 1.1 – The TDD cycle
The steps of the TDD cycle are:
That’s all there is to it. You’ve already seen this cycle in action in the preceding two sections, and we’ll continue to use it throughout the rest of the book.
Think about the effort you’ve put into this book so far. What actions have you been doing the most? They are the following:
src/Appointment.js
and test/Appointment.test.js
npm test
and analyzing the outputMake sure you can perform these actions quickly.
For a start, you should use split-screen functionality in your editor. If you aren’t already, take this opportunity to learn how to do it. Load your production module on one side and the corresponding unit test file on the other.
Here’s a picture of our setup; we use nvim
and tmux
:
Figure 1.2 – A typical TDD setup running tmux and vim in a terminal
You can see that we also have a little test window at the bottom for showing test output.
Jest can also watch your files and auto-run tests when they change. To enable this, change the test
command in package.json
to jest --watchAll
. This reruns all of your tests when it detects any changes.
Watching files for changes
Jest’s watch mode has an option to run only the tests in files that have changed, but since your React app will be composed of many different files, each of which are interconnected, it’s better to run everything as breakages can happen in many modules.