DEV Community

Cover image for What I've Learned About Testing React Apps - Unit Tests

What I've Learned About Testing React Apps - Unit Tests

Tomasz Łakomy on November 01, 2019

Before we start - I'm working on https://cloudash.dev, a brand new way of monitoring serverless apps 🚀. Check it our if you're tired of switching b...
Collapse
 
philsinsight profile image
Mr. Boateng

Great post, really good take on the benefits and the purpose of testing!

You mentioned that if a prop name changes that you don't want your test to break, I think I may have misunderstood this but in order to render the component you would need to specify props so wouldn't a name change always break your tests?

Collapse
 
thatzacdavis profile image
Zachary Davis

I agree, I think it depends on the prop being changed, but I've definitely had tests break after prop name changes because I was specifically using that prop.

Collapse
 
almostconverge profile image
Peter Ellis

I would add two things to the reason we're unit testing, which may tip you towards getting that 100% coverage after all.

  1. Unit tests indicate brittle code. Ever wrote a unit test that was a pain to set up? That's a pretty good clue that your code is doing too much and/or is poorly organised. This is your chance to change that, and rejig your code until it is easy to unit test.
  2. Unit tests are part of the documentation. Imagine you've landed on an alien codebase and while trawling through the code, you come across something odd. Is this a bug or is it a feature? The official documentation is silent about the matter but maybe they just forgot to write it down? Well, head over to the unit tests then...oh, there aren't any. Now you've got a problem.

So to sum it up, unit tests are about a lot more than just double-checking X really does Y. That is not to say you should chase 100% coverage at all costs or by any means, of course you shouldn't. We all know that will end up with tests that mean nothing. But you better be clear about why you leave out bits and "that bit's difficult to test" is usually not a good excuse.

Collapse
 
gumballhead profile image
Andrew Kirkegaard • Edited

Though I do agree that react component tests should just test behavior, I would say that's not a unit test. Unit tests should test logic and have no dependencies. You should be testing logic directly and not through side effects of rendering a component.

Also, tests are a feedback tool. If they're brittle and break, that's a good thing -- you got some valuable feedback about the impact of the change you made to the system.

Collapse
 
sebalinares profile image
Sebastian Linares L.

Nice one, I want to start testing a LOT more than what I'm doing now - which is nothing, nada. Now I know what not to start with, enzyme. I'll try the testing library you recommend, thanks.

Collapse
 
ankeetmaini profile image
Ankeet Maini

Excellent post, will send it to my team first thing Monday morning :)

Collapse
 
tlakomy profile image
Tomasz Łakomy

Thank you! That means a lot to me 🥳