Testing React Components Best Practices

Tomasz Bak
Mar 3, 2017 · 3 min read

by React Developers @ Selleo

Image for post
Image for post

The most popular approach to testing React components is to use either Mocha+Chai+Enzyme or Jest+Enzyme. In this article, we will describe our React components testing practices with Jest+Enzyme which are also applicable to Mocha+Chai.

If you are new to testing React components you should read also:

Tests organization

Image for post
Image for post

Minimal component test confirms component rendered

or Jest snapshot:

The later generates __snapshots__/MainSection.spec.js.snap file.

Changes in snapshots are confirmed locally via ‘u’ in the jest cli and committed to the git repository, so PR reviewer can see them. You can read more on Snapshot Testing

At the moment we limit usage of snapshots to component rendering and complex json (i.e. chart configurations).

Ok, I test renders — what else should I test?

Tests are automated quality assurance and documentation for developers. The larger the project and team is the more detailed tests you need.

Try to think of future you getting back to this component or refactoring it — what would your expectations from tests be?

  • Isolated — all interactions with external services should are mocked
  • Specific — if change small functionality you would like to get specific test failure message
  • They describe what the system does not how so that you can easily refactor

Let’s go through some practices that we find helpful in achieving those goals.

Explicit setup() instead of beforeEach()

Testing behaviour

You can see how setup() makes writing those tests really fast!

Use helper functions

It reads much batter and is easier to maintain.

Summary

  • put tests close to implementation in __tests__ subfolder
  • always start with simple component rendering test aka smoke testing then test behaviour
  • think of future you getting back to this component or refactoring it
  • use explicit setup() and return common shortcut variables with it
  • use helper functions that generates tests

We hope you found this article helpful. You can find working example code at my fork of redux todomvc code at https://github.com/tb/redux/tree/react-testing/examples/todomvc/src/components/__tests__

selleo

Experienced Ruby On Rails, Elixir, Node.js,

Tomasz Bak

Written by

✅ Design Successful Product at https://selleo.com ✅ Build Engineering Team at https://www.softkraft.co 🚀

selleo

selleo

Experienced Ruby On Rails, Elixir, Node.js, Ember.js, React & React Native developers ready to join your team or build your next project.

Tomasz Bak

Written by

✅ Design Successful Product at https://selleo.com ✅ Build Engineering Team at https://www.softkraft.co 🚀

selleo

selleo

Experienced Ruby On Rails, Elixir, Node.js, Ember.js, React & React Native developers ready to join your team or build your next project.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store