Test Driven Development Is Hard
I've been practicing test driven development for over 3 years now and I still find it challenging. Almost anyone can write a half decent unit test after the fact, but only the best developers know how to make great decisions throughout a project test-first.
Testing Overview - React
Jest Testing Examples
Most of the time you'd just check to see if a function was called with a specific Object. There are some cases where you may only care that one prop of a default Object has been altered.
Best practices for testing React Apps with JEST: - Avoid unnecessary tests: Consider a test where you expect some element to be defined and then expect it to have some property on it. Now the latter one becomes unnecessary, because what is the point if the element was not defined at all. - Don’t test implementation details: If your test does something that your user doesn’t, chances are that you are testing implementation details. For example, you may be exposing a private function just to test your component. This is a code smell — don’t do it. A refactor can easily break your test. - Push business logic into pure functions rather than UI components: Consider, having a Shopping Cart UI component that should not compute the cart total. This should be pushed to a pure function because it is easier to test.
Complete Guide to Unit Tests with React
Have you ever gotten yourself into a situation where a small change in the project was worrying because you didn't know what could be broken? Well, I guess everybody passed through this at least once at some point. But what we didn't know back there, is the existence of our best friend, Unit Tests.
- What is the best way to organize tests alongside React components?
- How do you effectively add tests to existing code?