Set Up the Test Framework

Let’s add some tests to the word counter project. We’ll start by installing a test framework that runs our tests and reports the test results. We’ll use Jest[15] because it offers an excellent out-of-the-box experience, but you can apply the techniques you’ll learn to other test frameworks.

Navigate to the word counter project directory and install Jest as a development dependency:

 $ ​​npm​​ ​​i​​ ​​--save-dev​​ ​​jest

Since your code uses JSX, you also need to transpile the code when you run the tests. A Jest plugin called babel-jest ensures Jest transpiles the test code with the Babel configuration it finds in the package root. Install the babel-jest plugin as a development dependency:

 $ ​​npm​​ ​​i​​ ​​--save-dev ...

Get React for Real now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.