Testing ReactJS app with Jest and Enzyme tutorial (2024)

  • Tags:
  • Javascript

Table of contents:

Intro to testing frontend applications

Writing a good quality web application requires testing, to avoid unexpected crash of one component when something changes in the other one. It’s necessary to understand the logic and plan test cases at the beginning to test the application well.

In this article, I’d like to go deeper into development concepts like Test-Driven Development (TDD), and explain to you why testing the app is essential. Besides that, I’ll go through the pros and cons of testing, and I’ll describe three types of tests usesd in testing web apps.

In the end, I’ll go to the practical part, where I’m going to test a simple React.js application using Jest and Enzyme step by step. The code of the application you can find on our Github.

Here’s the video tutorial, where I’m testing the application, so if you are more watching person than reading, join me on Youtube.

Let’s start!

Why are tests important?

Tests in the application are reviewing the written code if it executes correctly and brings the required results.

Testing application when we are coding is really valuable and can bring a lot of benefits for the development process, as for the future application maintenance.

The biggest benefit of a testing application is preventing regression. Thanks to tests, we can easily catch if the new code doesn’t bring old bugs again. Regression slows down the development a lot, and if there’s a way to prevent it, it should be done.

Testing the application also provides quick feedback about the code we’ve created, and instead of using multiple console.log and manual testing, we can find out what works and what doesn’t.

Besides that, testing helps us make sure that complex components and logic work well in different situations, so there won’t be an unexpected surprise when we try to interact with certain features.

Testing helps developers to create less buggy applications and allows us to find and fix bugs early. It also simplifies adding new features and reduce the cost of building the application.

Pros and cons of testing

Even if unit testing is essential, it has some pros and cons, which developers should be aware of.

The pros of the tests are:

  • possibility to find errors early and fix them early
  • well written tests provide kind of documentation, which helps new developers to understand what’s going on in the application
  • it reduces the time of manual testing
  • helps to maintain and improve the application easily and with fewer errors

The cons of tests are:

  • writing tests is time-consuming
  • more code needs to be done
  • badly written tests can skip important errors

What is Test-Driven Development?

Test-Driven Development is a methodology that assumes that tests are written before the code, and the task is to create the code that will pass them.

The process of coding with Test Driven Development happens with six steps flow:1. Write tests2. Run all tests, new and existing ones. In this step, new tests should fail because there’s no existing code yet.3. Write the minimum amount of code to pass the tests.4. Run the tests again to check if it passed.5. Refactor the code if necessary.6. Repeat.

TDD is a good solution for developing a good quality application, but as it has pros, it also has some cons.

Let’s see what the pros of TDD are:

  • writing small tests during the development forces the code to be modular
  • TDD powers good architecture and modularization
  • it helps with easier maintenance
  • it helps to clarify the requirements of the project from the start and helps to avoid misunderstanding
  • it provides high coverage of tests in the application

The bad sides of TDD are:

  • it can be difficult to write
  • it can slow down the development, because of writing additional code
  • it is difficult to apply to exist or legacy code
  • tests need to be refactored sometimes

Besides a pure TDD, there’s one more solution that can be considered, especially in frontend. In the case of test-driven development, we are testing the implementation, and tests can easily fail after small changes.

But if we’d test the application’s behavior, then small changes in the implementation won’t make us change the tests. This kind of approach is called Behavior Driven Development, and it’s a solution worth thinking of in case of frontend projects that will grow.

Types of tests

When testing the application, we can divide tests into three types:

  • Unit tests - this type of test are focused on individual components, functions, modules called units. Unit tests isolate the particular units and tests it separately to make sure each part of the application is tested and works as expected. In this kind of test, we are not testing the integration of each unit.

  • Component tests - this type of test are focused on testing a single component as an individual part of the application.

  • Snapshot tests - the snapshot tests are used to make sure that the UI isn’t changing unexpectedly. The frameworks create a snapshot from the component, and then compares it with the current state, checking for the changes.

Now, let’s go and try to test the ReactJS application with Jest and Enzyme.

How to test the ReactJS app step by step with Jest and Enzyme?

This is a practical part of this article, where I’d like to go step by step through testing my existing ReactJS application.

If you’d like to join and do it with me, you can find the code on our Github.

For the testing, I’m going to use Jest and Enzyme. Jest is a Javascript testing framework focused on simplicity. It works with most modern frontend frameworks and with pure Javascript.

Enzyme is a library for testing ReactJS components, and it’s very easy and intuitive to use.

1. Installation

Let’s start by installing all the necessary libraries. Open the application and console using yarn or npm to install Jest, Enzyme, and some additional plugins.

If you created your app with create-react-app you don’t have to install Jest, it’s already there.

yarn add enzyme enzyme-adapter-react-16 react-test-rendereryarn add enzyme-to-json

If you don’t have Jest in your project yet you can install it with the following command:

yarn add jest

When it’s ready, we can open the application code and start setting up the testing environment.

2. Setting test file

Please open the setupTest.js file, where we will need to configure the adapter to use Enzyme in the Jest environment properly.

import { configure } from "enzyme";import Adapter from "enzyme-adapter-react-16";configure({ adapter: new Adapter() });

When that’s ready and saved, the next step will be to think about test cases.

3. Preparing test cases

When we start testing any application, we need to have an idea of what we want to test inside it. That’s why, in this step, we are going to think of cases that will be important for us to test.

In the case of our ReactJS application, we have a very simple functionality. The component is rendered, then there’s a button that is changing the values in the boxes. When the balance box is 1000 or less, we display a notification, by changing the class.

So, first of all, let’s test is all the components are rendering.

Next, let’s check if the props passed through the components are correct.

We can then check the logic, so if the button is clicked, the value changes on both accounts.

Finally, we can test snapshots.

Now, we have four main groups of tests that we would like to test.

4. Test if components are rendering

Let’s start from the first group, where we will test if our components are rendered correctly. We are going to group it using describe.

Let’s open the App.test.js file where we are going to put all our tests. Because the application is not large, we are not going to put it into different files. But in case of bigger applications than two components, it’s really good to create a test file for each of them separately.

import React from 'react';import App from './App';import AccountBalance from './AccountBalance.jsx';import Notification from './Notification.jsx';import { shallow, mount } from 'enzyme';import toJson from "enzyme-to-json";const userBalance = { balance: 1100, savingBalance: 103,}describe("rendering components", () => { it("renders App component without crashing", () => { shallow(<App />); }); it("renders App component header without crashing", () => { const wrapper = shallow(<App />); const header = (<h1 className="has-text-centered title is-1">Welcome in the personal finance app!</h1>); expect(wrapper.contains(header)).toEqual(true); }); it("renders Notification component without crashing", () => { shallow(<Notification />); }); it("renders button", () => { const wrapper = mount(<AccountBalance accounts={userBalance} />); const label = wrapper.find("#balance-button").text(); expect(label).toEqual("Send 100$"); });});

As you can see in the code, we are first using shallow, which is responsible for rendering the component without the children. If we need to see if there is any additional element rendered in the component we can check it by defining the element and using .contain() method to see if it’s present.

Also, I’ve already created a userBalance object, which is a mock for the props, which are going to be used in the next step.

5. Test passing props

Now, we can go to the next test case, which is passing props to the components. Let’s create another group with describe(). Inside the group, I’m going to set three tests, checking if the props are accepted, if they display correctly and if notification props are passed as well.

describe("passing props", () => { const accountWrapper = mount(<AccountBalance accounts={userBalance} />); const notificationWrapper = mount(<Notification balance={userBalance.balance} />); it("accepts user account props", () => { expect(accountWrapper.props().accounts).toEqual(userBalance); }); it("contains savingBalance value", () => { const value = accountWrapper.find(".savings").text(); const expectedValue = userBalance.savingBalance + "$"; expect(value).toEqual(expectedValue); }); it("notification accepts props", () => { expect(notificationWrapper.props().balance).toEqual(userBalance.balance); });});

So, now we can be sure that our props are passed to the child component successfully. Let’s test the logic of our application right now.

6. Test logic

The next step in our testing is to check if logic works properly. The logic is not very complicated here, because the most important functionality is changing accounts values on button click event.

In the App.test.js let’s add another describe() group.

describe("logic", () => { const wrapper = mount(<AccountBalance accounts={userBalance} />); const notificationWrapper = mount(<Notification balance={userBalance.balance} />); wrapper.find("#balance-button").simulate("click"); it("button click - update savings", () => { const savingsValue = wrapper.find(".savings").text(); const expectedValue = userBalance.savingBalance + 100 + '$'; expect(savingsValue).toEqual(expectedValue); }); it("button click - update balance", () => { const balanceValue = wrapper.find(".balance").text(); const expectedBalanceValue = userBalance.balance - 100 + '$'; expect(balanceValue).toEqual(expectedBalanceValue); });});

At first, I defined AccountBalance component wrapper and Notification component wrapper; then I used .simulate() method to simulate the click event on the selected button. Next, we have three tests that are checking the functionality after a click event.

7. Snapshots

The final step of testing our simple application are snapshots. For that, we will use an additional plugin that was installed at the beginning of this tutorial, enzyme-to-json. In this group, I’m going to define three cases as well, one for the App component, one for AccountBalance, and one for Notification component.

describe("snapshots", () => { it("App snapshot", () => { const tree = shallow(<App/>); expect(toJson(tree)).toMatchSnapshot(); }); it("Accounts snapshots", () => { const accountBalanceTree = shallow(<AccountBalance accounts={userBalance} />); expect(toJson(accountBalanceTree)).toMatchSnapshot(); }); it("Notification snapshot", () => { const notificationTree = shallow(<Notification />); expect(toJson(notificationTree)).toMatchSnapshot(); });});

In case there will be some update in the UI, and the snapshot test will fail, you can use u to update the snapshots.

Also, after snapshot tests are done for the first time, you will see the new folder in your app called __snapshots__ where the snapshots will be saved.

8. Testing

Now, it’s time to really test our application and run the tests. Let’s open the terminal, and run the following command:

yarn testornpm test

Then you should see the tests are running, and you’ll see the lists of your tests, and you’ll see if they passed.You can play with the tests to see how it looks when it fails.

Here’s my result:

Conclusion

In this article, I went through the benefits of testing web applications, and I pointed out some pros and cons of testing. Besides that, I also covered what’s Test-Driven Development and why it’s good and its disadvantages. I also went through three types of tests that are present on frontend application testing.

After that, I went to a practical task, where we have an application in ReactJS to test. I’ve installed all the necessary plugins and libraries; after that, we’ve planned the test cases and went through the tests.

I hope you’ll find this article helpful, especially if you are a beginner with testing. This tutorial will help you understand what testing is, what benefits it brings, and how to go through testing your application.

Thank you for reading,
Anna

Related readings 📖

14 most popular Angular interview questions in 2020

How to select right tech stack for your next web application?

How to build company website with Bulma CSS

Anna Danilec

Anna Danilec is graduated in Business Management.She was getting solid experience for years in front-end and back-end development by building software for known IT companies.Her IT expertise is React.JS, Angular, React Native, Flutter, Node.JS, and Nest.JS.In the last years, she was doing business for Duomly, focusing on IT as well as Online Marketing, Design, and Content Creation like Blogging and Youtube. After hours Anna manages her true crime youtube channel, loves sport, sun, and mediterrean cuisine.

More about Anna Danilec

If you liked it share and comment!

Testing ReactJS app with Jest and Enzyme tutorial (2024)

FAQs

How do you write test cases using Jest and Enzyme for react? ›

How to perform Unit testing of React Apps using JEST?
  1. Step 1: Create a new react app.
  2. Step 2: Create a component.
  3. Step 3: Write a unit test for the react component.
  4. Note: In order to let jest know about this test file, it's important to use the extension . test. js.
  5. Step 4: Run the test.
  6. Conclusion.
Apr 29, 2022

Can we use Jest and Enzyme together? ›

Many people choose to use Jest and Enzyme together to test their React web applications. They use Jest as a test runner and assertion library, then use Enzyme to build the tests for their UI. This results in slimmer, cleaner testing code that's also easier to debug when a test breaks.

Is react Enzyme testing Jest? ›

Both Jest and Enzyme are meant to test the react applications. Jest can be used with any other Javascript framework, but Enzyme is meant to run on react only. Jest can be used without Enzyme, and snapshots can be created and tested perfectly fine. But the Enzyme adds additional functionality to it.

Can I use Enzyme and react testing library together? ›

To ensure a successful migration, we recommend doing it incrementally by running the two test libraries side by side in the same application, porting your Enzyme tests to React Testing Library one by one.

Should I use Enzyme or React testing library? ›

Enzyme works best with class-based components.

React Testing Library will perform better in functional-based components. The official React documentation recommends using it whenever possible.

Is Enzyme testing dead? ›

If your project is using React+Enzyme and you dream about upgrading React version to 18 (and enjoying cool features like server-side and concurrent rendering) then I have bad news for you — Enzyme is dead and there would be no official adapters released compatible with future versions of React (you can still find some ...

Can one enzyme do multiple jobs? ›

Each type of enzyme only has one job. For example, the enzyme sucrase breaks down a sugar called sucrose. Lactase breaks down lactose, a kind of sugar found in milk products.

Why use RTL over enzyme? ›

RTL is replacing the already existing Enzyme and makes testing easier. RTL code is short, simple and it is easier to read, while Enzyme tests require more lines of code.

Why not to use jest? ›

Jest doesn't support “root hooks“ to setup the environment for all test suits. A type of hooks before (or after) every test in every file. Instead, you are forced to use some crappy jest-environment-node module with an awkward configuration process. Jest cannot nest the test suits inside each other.

Is Jest for frontend or backend? ›

Jest is a JavaScript-based testing framework that lets you test both front-end and back-end applications. Jest is great for validation because it comes bundled with tools that make writing tests more manageable.

Is React testing library better than Jest? ›

React Testing Library is not an alternative to Jest. Each performs a clear task, and you need them both to test your components. Jest is a test runner that finds tests, runs the tests, and determines whether the tests passed or failed. Additionally, Jest offers functions for test suites, test cases, and assertions.

What is the difference between Jest test and Jest it? ›

The difference between test and it in Jest

Basically, it is an alias for test , so they are functionally the same. So, what makes it different? This alias is created to make your tests more readable from a test output point of view. It can really help make your tests more readable from a readability point of view.

How do you write a test for a function in Jest? ›

To create a test case in Jest we use the test() function. It takes a test name string and handler function as the first two arguments. The test() function can also be called under the alias - it() .

How do you write a test case for if condition in Jest? ›

So you could write a test like this: describe('extractInfo', () => { test('test 1', () => { //inputs const Info = ''; const description = ''; const jestinformation = ''; //test const result = extractInfo(Info); //expect expect(result). toEqual('hello jest'); }); });

How do you write test cases for API testing in Jest? ›

In Jest, these are done using four different functions:
  1. beforeAll – called once before all tests.
  2. beforeEach – called before each of these tests (before every test function).
  3. afterEach – called after each of these tests (after every test function).
  4. afterAll – called once after all tests.

References

Top Articles
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5733

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.