| name | test-runner |
| description | Test Runner |
test-runner
Write and run tests across languages and frameworks.
Framework Selection
| Language | Unit Tests | Integration | E2E |
|---|
| TypeScript/JS | Vitest (preferred), Jest | Supertest | Playwright |
| Python | pytest | pytest + httpx | Playwright |
| Swift | XCTest | XCTest | XCUITest |
Quick Start by Framework
Vitest (TypeScript / JavaScript)
npm install -D vitest @testing-library/react @testing-library/jest-dom
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
setupFiles: './tests/setup.ts',
},
})
npx vitest
npx vitest run
npx vitest --coverage
Jest
npm install -D jest @types/jest ts-jest
npx jest
npx jest --watch
npx jest --coverage
npx jest path/to/test
pytest (Python)
uv pip install pytest pytest-cov pytest-asyncio httpx
pytest
pytest -v
pytest -x
pytest --cov=app
pytest tests/test_api.py -k "test_login"
pytest --tb=short
XCTest (Swift)
swift test
swift test --filter MyTests
swift test --parallel
Playwright (E2E)
npm install -D @playwright/test
npx playwright install
npx playwright test
npx playwright test --headed
npx playwright test --debug
npx playwright test --project=chromium
npx playwright show-report
TDD Workflow
- Red โ Write a failing test that describes the desired behavior.
- Green โ Write the minimum code to make the test pass.
- Refactor โ Clean up the code while keeping tests green.
โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโโ
โ Write โโโโโโถโ Write โโโโโโถโ Refactor โโโโ
โ Test โ โ Code โ โ Code โ โ
โ (Red) โ โ (Green) โ โ โ โ
โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโโ โ
โฒ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Test Patterns
Arrange-Act-Assert
test('calculates total with tax', () => {
const cart = new Cart([{ price: 100, qty: 2 }]);
const total = cart.totalWithTax(0.08);
expect(total).toBe(216);
});
Testing Async Code
test('fetches user data', async () => {
const user = await getUser('123');
expect(user.name).toBe('Colt');
});
Mocking
import { vi } from 'vitest';
const mockFetch = vi.fn().mockResolvedValue({
json: () => Promise.resolve({ id: 1, name: 'Test' }),
});
vi.stubGlobal('fetch', mockFetch);
Testing API Endpoints (Python)
import pytest
from httpx import AsyncClient
from app.main import app
@pytest.mark.asyncio
async def test_get_users():
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.get("/users")
assert response.status_code == 200
assert isinstance(response.json(), list)
Testing React Components
import { render, screen, fireEvent } from '@testing-library/react';
import { Button } from './Button';
test('calls onClick when clicked', () => {
const handleClick = vi.fn();
render(<Button onClick={handleClick}>Click me</Button>);
fireEvent.click(screen.getByText('Click me'));
expect(handleClick).toHaveBeenCalledOnce();
});
Coverage Commands
npx vitest --coverage
npx jest --coverage
pytest --cov=app --cov-report=html
pytest --cov=app --cov-report=term
pytest --cov=app --cov-fail-under=80
open coverage/index.html
open htmlcov/index.html
What to Test
Always test:
- Public API / exported functions
- Edge cases: empty input, null, boundary values
- Error handling: invalid input, network failures
- Business logic: calculations, state transitions
Don't bother testing:
- Private implementation details
- Framework internals (React rendering, Express routing)
- Trivial getters/setters
- Third-party library behavior