DEVELOPMENT,TECHNOLOGY

A guide for unit testing React using Jest and Enzyme

06 Oct 2021

Want to avoid legacy codes? Any codes that are not tested are classified as legacy codes. To avoid legacy codes, what can you do? The solution is quite simple- use test-driven development (TDD)!

There are many tools for testing JavaScript and React.JS framework, we are narrowing down our discussion to unit testing React using Jest and Enzyme.

What is unit testing and why is it significant?

Unit testing is an integral part of the software development process for assuring product stability. Through unit testing, you can be sure that all the components of your product are tested and ready to be live.

Why is unit testing significant in the software development process? It can:

  • Improve the quality of code
  • Quickly identify the future breaks in an always changing software requirements
  • Bugs can be found at an early stage, and hence the cost of fixing bugs can be reduced

How is Jest similar to Enzyme?

Jest and Enzyme are resilient tools for testing React User Interfaces. These help in making sure that your React app has a user interface that is complying to the set requirements. Jest and Enzyme are developed and installed using Node Pack Manager. It has a large supporting community for finding your way out during any stagnation point during the app development process.

How is Jest different from Enzyme?

The purpose served by Jest and Enzyme is slightly different from each other. 

Jest is a complete feature-rich test framework. Jest can run test scripts on all your tests and the whole assertion library. Jest works well not only with React, but also with other frameworks. A JavaScript-based web application will use NodeJS to build the server, Jest allows you to run tests for your whole application with one test library.

Enzyme instead of serving a full testing library, it acts as the library that makes testing React components even easier. Enzyme can be integrated with different test libraries including Jest. 

If you are developing a React app, it will make more sense to use bothe Jest and Enzyme together for automated UI testing.

Unit testing React.JS apps using Jest and Enzyme: A step-by-step approach

Jest is a JavaScript testing framework centered on simplicity, that works on ultramodern framework and using pure JavaScript. Enzyme is used for testing React.JS components, and is a very easy-to-use and intuitive library.

Here is the step-by-step procedure for testing React app using Jest and Enzyme:

Installation

  1. Start installing all the necessary libraries
  2. Open the application and console using yarn or npm for installing Jest, Enzyme, and other required plugins
  3. Apps created using create-react-app does not need to install Jest because it is already inbuilt
  4. If there is no pre-installed Jest, you can use yarn add jest command
  5. Finally, open the application code and set up the testing environment

Fixing test file

  1. Open the setupTest.js file and configure the adapter to use Jest in the test environment properly
  2. Once it is ready and saved, start considering test cases

Setting up test cases

  1. Understand what is happening inside while testing an app and be sure of the cases that are very crucial for us to test. Make sure that all the components in the app are rendering
  2. Verify whether the props passed through the components are correct
  3. To check the logic, make sure that while clicking the buttons, the values change on both accounts
  4. Finally test snapshots 

Now we have four more main groups to test and are briefed below

Components rendering check

  1. Test whether your components in the first group are rendered correctly and group those using describe
  2. Open the App.test.js file and pull all the tests. Bigger applications need to be put into different files and if the bigger applications have two components, it is ideal to create a test file for each of them separately.
  3. Using shallow, make sure that components are rendering without children. If there is an additional element rendered in the component, define that element and use the .contain () method to see if it is present.
  4. Using the userBalance object can helpb with moicks for the props that are used for the next step.

Passing props testing

  1. Now pass the props to the components
  2. Create another group with describe ()
  3. Inside the group, set tests to check if the props are accepted, displayed correctly and notification props are passed as well
  4. Make sure the props are passed to child components successfully
  5. Test the logic of your application right now

Logic testing

  1. Logic can be complicated as an important functionality like changing account values  on button click event is involved
  2. Add another describe () group in the App.test.js
  3. Use .simulate () method to simulate the click event on the selected button
  4. Run tests to check functionality after a click event

Snapshots

  1. Use additional plugin that was installed at the beginning enzyme-to-json
  2. Define cases for App component, AccountBalance and Notification component
  3. If there is an update in the UI, and the snapshot tests fail, you can use u to update the snapshots
  4. Firstly done snapshots, new folders in your app called  __snapshots__ where it will be saved

Testing

  1. Run the tests and test your application
  2. Open the terminal and run the command yarn test or npm test
  3. Make sure the tests are running and you will have the list of tests that passed and can see the tests to understand how the tests to see how it looks when it fails

Conclusion

With robust test frameworks like  Jets and Enzyme, your React App becomes more stable and enhances the quality of your code. It helps developers in fixing bugs faster. For complicated applications, these test frameworks will ensure that their codes work the way that it was expected, and QA engineers can reduce their efforts for  UI regressions.

The betterment of your software is ensured by using tools like Jest and Enzyme. When the projects get more complex, right tools like Jest and Enzyme can help you with managing that complexity.

smile

Thanks For Reading!

Webdura Technologies

Webdura Technologies

Webdura technologies is a full spectrum technology company in India with over 10 years of experience in developing technological solutions using ​JavaScript (ES6+), React JS, React Native, Redux, Rematch, Vue JS, Graph QL, Apollo, Meteor JS, Node JS, Gatsby JS, PHP, Wordpress, MySQL, Mongo DB and other latest tools. Webdura technologies have joined hands with many international and national giants to put forth cutting edge applications in this past decade.

Comments

POST YOUR COMMENTS

Sign up for our newsletter the monthly updates

How about a lil' game of fill in the blanks?

We love working alongside ambitious brands and people