Unit Testing & Test Driven Development (TDD)

Last Modified
Last updated December 1, 2022
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.
  • What is the best way to organize tests alongside React components?