What is Jest and Enzyme and what are they useful for?
Hi, first let’s learn about Jest that is a testing framework that was created by Facebook and is already integrated in “create-react-app” tool (that help us to create a react app).
Jest is is a competition from other testing libraries like Mocha , Jasmin, etc. but Jest is integrated by default in “create-react-app”.
Using Jest is a way to test React application and the way to create a test with Jest is:
Using the it method that receive the first parameter that is a text that has the description (example: ‘did not sell’) of what we are going to test and another function as second parameter where we going to insert the test code and this will be executed.
The “Assertion” word it is widely used in testing and is a way to verify that something has been accomplished, so for testing assertions we use the “expect” word that is a function.
So in the next example we hope that by invoking the method zeroEarning() return 0.
Example:
it('did not sell', () => {
expect(zeroEarning()).toBe(0);
});
Ok, second let’s learn about Enzyme that is a JavaScript Testing utility for React that makes it easier to test your React Components’ output. You can also manipulate, traverse, and in some ways simulate runtime given the output.
Enzyme was created by Airbnb so do not be confused if you see want to find a relation between Airbnb and Enzyme, they just created the library and is open source.
And this utility is not included in “create-react-app” so is necessary add this utility into our package.json file like:
npm install --save-dev enzyme
Ok, in the next post I will explain you more about the Jest methods and how to use enzyme to create an instance of the component that we want to test.
By Cristina Rojas.