Back to Blog
May 1, 2026
2 min readUpdated: May 12, 2026The Importance of Unit Testing
Do you have a question or doubt about something?
Scroll down to the bottom to ask your question, and I or anyone else will respond!
The Rule
Not everything needs unit tests.
| Code Type | Test Priority |
|---|---|
| Business logic | ✅ High |
| API endpoints | ✅ High |
| Auth/permissions | ✅ High |
| UI components | ❌ Low (use integration tests instead) |
| Third-party wrappers | ❌ Don't test (test that you call them correctly) |
| Configuration | ❌ Don't test |
One Good Test is Better Than 100 Bad Ones
Bad test (testing implementation, not behavior):
test('button click calls setState with true', () => {
// Tests internal state, breaks if you refactor
});
Good test (testing behavior):
test('clicking submit saves the user input', async () => {
render(<Form />);
await userEvent.type(screen.getByLabelText(/name/i), 'Ekuty');
await userEvent.click(screen.getByRole('button', { name: /submit/i }));
expect(screen.getByText(/saved/i)).toBeInTheDocument();
});
The 80/20 Rule for Testing
| Action | Impact |
|---|---|
| Test happy path | 80% of value |
| Test one edge case per component | 15% more |
| Test every possible error path | 5% more value, 300% more work |
Stop at 80% for most projects.
TypeScript as a Testing Tool
Proper types catch bugs that tests would otherwise need to catch:
if (user.email)is better than a test checking for missing emailstrictNullChecksprevents undefined is not an object- Discriminated unions prevent invalid states
Your Testing Setup (Recommended)
{
"scripts": {
"test": "vitest",
"test:ui": "vitest --ui",
"coverage": "vitest run --coverage"
}
}
// Basic component test with Vitest + Testing Library
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, it, expect } from 'vitest';
describe('ProductCard', () => {
it('displays product name and price', () => {
render(<ProductCard name="Coffee" price={15} />);
expect(screen.getByText('Coffee')).toBeInTheDocument();
expect(screen.getByText('15 PLN')).toBeInTheDocument();
});
});
Resources
| Tool | Use For |
|---|---|
| Vitest | Unit testing (faster than Jest) |
| React Testing Library | Testing React components |
| Playwright | End-to-end browser tests |
| Mock Service Worker | Mocking API requests |
Was this helpful?
Discussion
0Do you have a question or any doubt?
Ask here and I or anyone else will respond!
Loading comments...
2B
By 2BigDev
Full-Stack Engineer