. Many students and junior developers are familiar with backend tests in Java or Spring but have little experience testing the frontend.
โCan't I just open the browser and click around to see if it works?โ
Checking the UI yourself is useful. As the project grows, however, you can't click every button after each change.
In this series, we'll learn how to make
React
components more robust and how tests can make development more enjoyable.
Frontend code changes often. Designs evolve, and components get split up.
Without tests, you may find yourself refreshing the browser after every change and wondering whether something broke.
With tests, you can change the structure freely and check that the tests still pass.
When you use someone else's component, you may need to ask which props it expects. Tests can answer that question by showing exactly how the component behaves in different situations.
Opening the browser, signing in, navigating to a page, and clicking a button takes longer than it sounds.
With TDD, a single command can check hundreds of behaviors in seconds. That also speeds up development.
What should we cover? Testing everything is impractical. The central principle of
React
testing is to
focus on behavior from the user's perspective
.
What to test
What happens when the user clicks a button.
How a form value changes when the user types.
Whether the screen shows an error message when something goes wrong.
What to leave out
Whether a component's internal state variable is called isVisible or show.
Whether the
React
framework itself works. The
React
team tests useState.
Whether CSS styling has been applied. Use visual testing tools to check appearance.
Suggested illustration: [An iceberg. Above the surface are the screens and actions users can see, which are in scope for testing. Below the surface are internal variables and implementation details, which are out of scope.]