Testing React code
There are several ways you can test React code:
- Unit testing: This involves writing small, isolated tests that focus on specific parts of your code, such as individual functions or components. These tests can help you ensure that your code is working as expected and catch any bugs early on.
- Enzyme: This is a library developed by Airbnb that makes it easier to test React components. It provides a set of functions that allow you to simulate actions on your components and make assertions about their behavior.
- Jest: This is a JavaScript testing framework that is often used with React. It includes a test runner, an assertion library, and a set of utility functions for working with React components.
- Testing Library: This is a collection of libraries that makes it easier to test React components in a way that is more closely aligned with how users interact with them.
To get started with testing your React code, you can choose a testing library or framework that you feel comfortable with and follow the documentation to set it up in your project. Then, you can start writing tests for your components and functions to ensure that they are working as expected.
Here is an example of a simple test using Jest:
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});This test is testing a function called sum that takes in two arguments and returns the sum of those arguments. The test function is a way to define a test case in Jest, and the expect function is used to make an assertion about the expected output of the function. In this case, we are using the toBe matcher to assert that the value returned by sum(1, 2) is equal to 3.
Get Mustafa Onur Çelik’s stories in your inbox
Join Medium for free to get updates from this writer.
If the function being tested behaves as expected, the test will pass. If the function returns an unexpected result, the test will fail.
Jest also provides other matchers that you can use to make assertions about the output of your code. For example, you can use toEqual to test for deep equality, toBeNull to test for null values, and toBeTruthy to test for truthy values.
For more information about Jest and how to use it to test your code, you can refer to the Jest documentation.
Here is an example of how you might use Enzyme to test a simple React component:
import React from 'react';
import { shallow } from 'enzyme';
import MyComponent from './MyComponent';
describe('MyComponent', () => {
it('renders a title', () => {
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('.title').text()).toEqual('Hello World');
});
});This test is testing a component called MyComponent that is expected to render a title element with the text "Hello World". The shallow function is used to create a shallow rendering of the component, which means that it only renders the component itself and not any of its children. The find method is used to find an element with the class title within the component, and the text method is used to retrieve the text content of that element. Finally, the toEqual matcher is used to assert that the text content is equal to "Hello World".
If the component behaves as expected, the test will pass. If the component does not render a title element with the correct text, or if the element is not found, the test will fail.
Enzyme provides a variety of other methods and functions that you can use to test your React components, such as mount for full DOM rendering, and simulate for simulating events. You can refer to the Enzyme documentation for more information.




