Setting up Jest
First, we need to set up a testing engine to be able to run tests. In this book, we will use Jest (https://facebook.github.io/jest/). Jest, like React, is developed by the Facebook open source team. Perform the following steps to set up Jest:
- The first step is to install Jest via npm:
npm install --save-dev jest- Since we want to use the new JavaScript syntax, we also need to install
babel-jestandregenerator-runtime(which is required for babel-jest):
npm install --save-dev babel-jest regenerator-runtime
Note
If you use npm 3 or 4, or Yarn, you do not need to explicitly install regenerator-runtime.
- Next, we create a
__tests__directory in the project root. This is where we will put our test files. - Then, we create a
__tests__/example.test.jsfile, with the following code:
test('example test', () =>
expect(1 + 1).toBe(2)
)- Finally, we define a
testscript in ourpackage.jsonfile:
"scripts": {
...other scripts...,
"test": "jest"
}Now that we have our test engine set up, simply...