This document describes the comprehensive testing setup for the Tappmesa project.
The project uses Vitest as the testing framework along with React Testing Library for component testing.
vitest- Modern, fast testing framework@testing-library/react- Simple and complete testing utilities for React@testing-library/jest-dom- Custom DOM element matchers@testing-library/user-event- Advanced user event simulationjsdom- DOM environment for Node.js
src/test/
├── setup.js # Global test configuration
├── utils.js # Test utilities and mocks
├── utils/
│ ├── tenantUtils.test.js # Tenant context utility tests
│ └── cartUtils.test.js # Cart context utility tests
└── context/
├── TenantContext.test.jsx # TenantContext integration tests
└── CartContext.test.jsx # CartContext integration tests
# Run tests in watch mode (development)
npm test
# Run all tests once
npm run test:run
# Run tests with UI interface
npm run test:ui
# Run tests with coverage report
npm run test:coverage
# Run tests in watch mode (explicit)
npm run test:watchTenant Utilities (tenantUtils.test.js)
- Subdomain extraction from various URL formats
- Table code parsing and validation
- App type detection (landing, admin, tenant, table)
- Support for local development, production, and custom domains
Cart Utilities (cartUtils.test.js)
- Item total calculations
- Tax calculations (19% Chilean IVA)
- Price formatting (Chilean pesos)
- Cart item creation and management
- Quantity and notes updates
TenantContext (TenantContext.test.jsx)
- Provider initialization and state management
- Subdomain and table code detection
- Supabase integration mocking
- Error handling (tenant not found)
- Branding application (colors, title)
- Hook usage outside provider error handling
CartContext (CartContext.test.jsx)
- Cart state management (items, totals, taxes)
- Item addition, update, and removal
- LocalStorage persistence
- Temperature-based item differentiation
- Cart UI state (open/closed)
import { createMockSupabase, mockTenant, mockProduct } from './test/utils'
// Create mock Supabase client
const mockSupabase = createMockSupabase()
// Use predefined mock data
const tenant = mockTenant // Sample tenant data
const product = mockProduct // Sample product dataimport { mockLocation } from './test/utils'
// Mock different environments
mockLocation({ hostname: 'cafe-central.tappmesa.local' })
mockLocation({ hostname: 'localhost', search: '?cafe=test-cafe' })
mockLocation({ pathname: '/ABCD1234/menu' })The setup.js file provides:
- Global DOM environment setup
- Window.location mocking
- LocalStorage mocking
- Supabase environment variables
- Document property mocking
# Run specific test file
npm run test:run src/test/utils/cartUtils.test.js
# Run tests with specific pattern
npm run test:run -- --grep "should calculate"
# Run tests in specific directory
npm run test:run src/test/context/-
Supabase Mock Issues
// Ensure proper mock setup vi.mock('../../lib/supabase', () => ({ supabase: createMockSupabase() }))
-
LocalStorage Mocking
// Reset localStorage between tests beforeEach(() => { localStorage.getItem.mockClear() localStorage.setItem.mockClear() })
-
Async Operations
// Always await async operations await waitFor(() => { expect(screen.getByText('Expected Text')).toBeInTheDocument() })
Tests automatically use these environment variables:
VITE_SUPABASE_URL=http://localhost:54321VITE_SUPABASE_ANON_KEY=test-anon-key
- Utility Functions: 100% coverage
- Context Providers: 90%+ coverage
- Critical Business Logic: 100% coverage
- Error Handling: 100% coverage
Tests run automatically on:
- Pull requests
- Main branch pushes
- Pre-commit hooks (when configured)
- Unit tests:
*.test.js - Component tests:
*.test.jsx - Integration tests:
*.test.jsx
describe('Component/Function Name', () => {
beforeEach(() => {
// Setup code
})
describe('specific functionality', () => {
it('should do something specific', () => {
// Test implementation
})
})
})-
Use descriptive test names
it('should calculate correct total including 19% tax')
-
Test both success and error cases
it('should handle invalid input gracefully')
-
Mock external dependencies
vi.mock('./external-dependency')
-
Clean up after each test
afterEach(() => { vi.clearAllMocks() })
- Add E2E tests with Playwright/Cypress
- Implement visual regression testing
- Add performance testing
- Set up automatic coverage reporting
- Add component integration tests
For questions about testing or to suggest improvements, please open an issue or submit a pull request.