Chapter 8. Test Your React Application with Jest

By now, you must have created a number of React components. Some of them are quite straightforward, but some are sophisticated enough. Having built both, you might have gained a certain confidence, which makes you believe that no matter how complex the user interface is you can build it with React, without any major pitfalls. This is a good confidence to have. After all that's why we're investing time in learning React. However, there is a trap that many confident React developers fall into; the act of not writing unit tests.

What is a unit test? As the name suggests, it's a test for a single unit of your application. A single unit in your application is often a function, which suggests that writing unit tests means writing tests for your functions.

Why write unit tests?

You might be wondering why you should write unit tests? Let me tell you a story from my personal experience. I had a release of a new website that I built recently. A few days later, my colleague who was using the website sent me an e-mail with a few files that the website would reject. I closely examined the files, and the requirement of having the IDs matched in both of them was met. However, the files were still rejected, and the error message said that the IDs didn't match. Can you guess what the problem was?

I wrote a function that will check whether the IDs from the two files match. The function checked both the value and the type of an ID, so even if the values were the same and the types were different, it would return no match. Turns out, that was exactly the case with the files from my colleague. The important question is how could I prevent this from happening? The answer is a number of unit tests for my function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.248.149