Skip to content
UNASPACE

Testing

This content is for Frontend. Switch to the latest version for up-to-date documentation.

Testing is the practice of verifying that your code works as expected. In frontend development, this ranges from testing individual functions to simulating full user interactions in a real browser.

  • Catch bugs early: Find issues before users do.
  • Refactor with confidence: Change code without fear of breaking existing features.
  • Document behavior: Tests serve as living documentation of how your code should work.
  • Team collaboration: Tests protect against regressions when multiple developers work on the same codebase.
Diagram
TypeWhat it testsSpeedExample
UnitA single function or component in isolation.FastDoes formatDate() return the correct string?
IntegrationMultiple units working together.MediumDoes clicking a button update the list component?
End-to-End (E2E)The full application in a real browser.SlowCan a user sign up, log in, and create a post?

The modern standard for JavaScript/TypeScript testing. Built on top of Vite, it’s fast and compatible with the Jest API.

  • Fast: Runs tests in parallel with native ES module support.
  • Vite-Powered: Shares your Vite config — same transforms, aliases, and plugins.
  • Jest-Compatible: If you know Jest, you know Vitest.
math.js
export function add(a, b) {
return a + b;
}
// math.test.js
import { describe, it, expect } from 'vitest';
import { add } from './math.js';
describe('add', () => {
it('adds two numbers', () => {
expect(add(2, 3)).toBe(5);
});
it('handles negative numbers', () => {
expect(add(-1, 1)).toBe(0);
});
});
Terminal window
npx vitest

The long-standing standard for JavaScript testing, created by Meta. Still very widely used, especially in legacy and React projects.

  • Mature: Huge ecosystem of plugins and integrations.
  • Snapshot Testing: Serialize component output and compare against saved snapshots.
  • Mocking: Built-in support for mocking functions, modules, and timers.

A family of libraries for testing UI components from the user’s perspective. Instead of testing implementation details, you test what users see and do.

  • Framework-Agnostic: Available for React, Vue, Svelte, Angular, and more.
  • User-Centric Queries: Select elements by role, label, or text — not by CSS class or ID.
// React + Testing Library
import { render, screen, fireEvent } from '@testing-library/react';
import Counter from './Counter';
test('increments the counter', () => {
render(<Counter />);
const button = screen.getByRole('button', { name: /increment/i });
fireEvent.click(button);
expect(screen.getByText('Count: 1')).toBeInTheDocument();
});

E2E tests run your application in a real (or headless) browser and simulate actual user behavior.

A modern E2E testing framework by Microsoft. Tests run across Chromium, Firefox, and WebKit.

  • Cross-Browser: One test suite runs on all major browsers.
  • Auto-Wait: Automatically waits for elements to be ready before interacting.
  • Codegen: Record your actions in a browser and generate test code automatically.
example.spec.js
import { test, expect } from '@playwright/test';
test('homepage has correct title', async ({ page }) => {
await page.goto('https://example.com');
await expect(page).toHaveTitle(/Example/);
});
test('user can navigate to about page', async ({ page }) => {
await page.goto('https://example.com');
await page.click('text=About');
await expect(page).toHaveURL(/about/);
});

A popular E2E testing tool with an interactive test runner that shows your tests executing in real-time.

  • Time Travel: See snapshots of your app at each step of the test.
  • Real-Time Reloads: Tests re-run automatically when you save.
  • Developer-Friendly: Great debugging experience with detailed error messages.
ToolTypeBest For
VitestUnit / IntegrationNew Vite-based projects, fast iteration
JestUnit / IntegrationEstablished projects, React ecosystem
Testing LibraryComponent TestingUser-centric UI testing across frameworks
PlaywrightE2ECross-browser, CI/CD pipelines
CypressE2EInteractive development-time testing
Built with passion by Ngineer Lab