How to make testing a habit
Use feedback loops
My favourite part of testing in React is the feedback loop I get from running Jest in watch mode. By passing Jest the
--watch flag (so
yarn test --watch), I'm able to have jest only run tests for files that I change, and in doing so, I know in the moment when I've broken something.
You might have a huge React app with hundreds of tests - in that case you'll want to tell your test runner to only watch the file you're modifying.
Run all of your tests as part of CI
This assumes your team's project uses CI (which it should), but running all of your tests - both on the branch you're working on, and the main branch helps ensure your code works as you expect before deploying.
You might be wondering how this helps build a testing habit - the trick is, you won't be able to deliver code if your tests aren't passing, so it forces you to run tests as you code.
Next, let's look at coverage in Chapter 6: What's a good level of code coverage?
Want to become a better React developer?
I send a single email every two weeks with an article like this one, to help you be a better React developer.
Lots of developers like them, and I'd love to hear what you think as well. You can always unsubscribe.
I'll even send you a free PDF copy of the book you're reading, The Beginner's Guide to React Testing, to start with.