Back to Blog
May 1, 2026
2 min readUpdated: May 12, 2026

The 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 Importance of Unit Testing

The Rule

Not everything needs unit tests.

Code TypeTest 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 /&gt;);
  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

ActionImpact
Test happy path80% of value
Test one edge case per component15% more
Test every possible error path5% 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 email
  • strictNullChecks prevents 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', () =&gt; {
  it('displays product name and price', () =&gt; {
    render(<ProductCard name="Coffee" price={15} /&gt;);
    expect(screen.getByText('Coffee')).toBeInTheDocument();
    expect(screen.getByText('15 PLN')).toBeInTheDocument();
  });
});

Resources

ToolUse For
VitestUnit testing (faster than Jest)
React Testing LibraryTesting React components
PlaywrightEnd-to-end browser tests
Mock Service WorkerMocking API requests

Was this helpful?

Discussion

0

Do you have a question or any doubt?

Ask here and I or anyone else will respond!

Loading comments...
2B

By 2BigDev

Full-Stack Engineer