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-jest and regenerator-runtime (which is required for babel-jest):
npm install --save-dev babel-jest regenerator-runtime
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.js file, with the following code:
test('example test', () =>
expect(1 + 1).toBe(2)
)
- Finally, we define a test...