Unit Testing Best Practices In React

React application unit testing using Jest

Osanda Deshan Nimalarathna
Test Automation Master
5 min readMar 15, 2023

--

What is Unit Testing?

  • Unit testing is a testing method that tests an individual unit of software in isolation.
  • Unit testing for React Apps means testing an individual React Component.

“Unit testing is a great discipline, which can lead to 40% — 80% reductions in bug density.” — Eric Elliotte

The Test Pyramid

The test pyramid

Stick to the pyramid shape to come up with a healthy, fast, and maintainable test suite: Write lots of small and fast unit tests. Write some more coarse-grained tests and very few high-level tests that test your application from end to end. Watch out that you don’t end up with a test ice-cream cone that will be a nightmare to maintain and takes way too long to run.

Benefits of Unit Testing

  1. Process becomes Agile

The Agile Testing process is the main advantage of unit testing. When you add more features to the software, it might affect the older designs and you might need to make changes to the old design and code later. This can be expensive and require extra effort. But if you do unit testing, the whole process becomes much faster and easier.

Agile Workflow

2. Quality of code

Unit testing significantly improves the quality of the code. It helps developers to identify the smallest defects that can be present in the units before they go for the integration testing.

3. Facilitates changes

Refactoring the code or updating the system library becomes much easier when you test each component of the app individually.

4. Smooth debugging

The debugging process is very simplified by doing unit testing. If a certain test fails, then only the latest changes that have been made to the code need to be debugged.

5. Reduction in cost

When bugs are detected at an early stage, through unit testing, they can be fixed at almost no cost as compared to a later stage, let’s say during production, which can be really expensive.

6. Act as the best form of documentation

Unit tests tell you how the original authors intended their code to be used. Read the unit tests and you can learn how the authors consume their own produce.

Best Practices

  1. Use one test file per component

Separate different tests into different files to make them well separated, keep the files slim, and make them easily maintainable.

Separating different tests into different files

2. Organize your tests with describe and test/it

Use describe to create blocks that group several related tests together and use test or it to execute individual test cases.

Using describe as blocks and test/it as individual test cases

3. Setup and reset commons in beforeEach/afterEach hooks

Use beforeEach and afterEach hooks to set up common codes for test cases and reset all mocks after your tests.

Using beforeEach/afterEach hooks

4. Use the three A’s pattern

Arrange/Act/Assert (AAA) is a pattern for organizing unit tests. It breaks tests down into three clear and distinct steps:

  • Arrange: Perform the setup and initialization required for the test.
  • Act: Take action(s) required for the test.
  • Assert: Verify the outcome(s) of the test.
Using 3A’s pattern

5. Also test what should not happen

When writing tests, a common practice is to test what a function should do. But you should not only test the obvious but also test the edge cases and negative cases.

Testing the negative cases

6. Make your tests deterministic

Your tests should not depend on each other. It goes without saying, but for your tests to be deterministic, you want to ensure that your tests don’t depend on each other. Each test case should be independent of the other and should be able to pass on its own.

Using mocking to independent tests

7. Don’t duplicate implementation logic

You might copy the same flaws in the logic over to your test suite which means your test suite will pass, but not because it is working correctly. Rather, it contains the same bug as your function. Always test the outcomes, not the implementation.

Testing outcomes, not the implementation

8. Run your tests as part of your deployment

Run your test suite before each deployment to ensure you didn’t accidentally break any functionality. That way, you can ensure that no bugs are released into a test/production environment for parts of your application that are already covered by tests.

Deployment life-cycle

Happy Unit Testing !!!

--

--

Osanda Deshan Nimalarathna
Test Automation Master

Founder of MaxSoft | RPA Solution Architect | Open-source Contributor | Automation Framework Developer | Technical Specialist