What is Unit Testing?
-
Unit testing means testing individual pieces of your code (functions, components) to ensure they work as expected.
-
Helps catch bugs early, improves code quality, and makes refactoring safer.
Popular JavaScript Testing Tools
| Tool | Purpose |
|---|---|
| Jest | Testing framework by Facebook, great for React |
| React Testing Library | Focuses on testing React components the way users interact with them |
| Mocha / Chai | General JS testing frameworks/assertion libraries |
Example: Testing a Simple Function with Jest
Run tests with:
Testing a React Component with React Testing Library
Benefits of Unit Testing
-
Catch errors early.
-
Prevent regressions when changing code.
-
Document expected behavior.
-
Boost confidence in code.
Summary
-
Unit tests focus on small, isolated pieces of code.
-
Jest is a popular testing framework for JavaScript.
-
React Testing Library lets you test React components from a user perspective.
-
Write tests for functions and UI interactions.
