Component tests in ERB use Jest and Enzyme. While Jest is popular for React testing, it is also capable of testing Node applications as well. Enzyme provides some utilities that make it easier to test React applications.
Writing Component Tests
By convention, all component test modules have the following filename suffix:
.spec.js. An example of test module filename would be
We start by writing a function that will setup the actions of our component and importing the necessary modules:
Now we describe a set of tests with
describe() and describe the test itself with
Tests can be run by running
Snapshot tests are probably one of the most powerful features of Jest. With them, you can easily capture the current state of an object and test against that state in future executions of the test. For more details on this, see Jest's Snapshot Testing docs.
After adding a new test to ERB, run
yarn test -u. This create a new snapshot if one does not exist already or it will update an existing one.
Suppose you are making change to React components and you want to see if components have broken while you are making changes. Running
yarn test -w will run tests all the tests that are affected by the changes made to the React components.