Writing great tests
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:
- Arrange: Sets up test dependencies
- Act: Executes production code under test
- Assert: Checks that expectations are met
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:
- Short
- Descriptive
- Independent of other tests
- Has no side effects
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.
Red, green, refactor
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:
- Write a failing test: Write a short test that describes some functionality you want. Execute your test and watch it fail. If it doesn’t fail, then it’s an unnecessary test; delete it and write another.
- Make it pass: Make the test green by writing the simplest production code that will work. Don’t worry about finding a neat code structure; you can tidy it up later.
- Refactor your code: Stop, slow down, and resist the urge to move on to the next feature. Work hard to make your code—both production and test code—as clean as it can be.
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.
Streamlining your testing process
Think about the effort you’ve put into this book so far. What actions have you been doing the most? They are the following:
- Switching between
src/Appointment.js
andtest/Appointment.test.js
- Running
npm test
and analyzing the output
Make 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.