Testing Guide - hokumcangus/taste-of-aloha GitHub Wiki
๐งช Testing Strategy & Guide
This guide covers the full testing suite for the Taste of Aloha application, including Backend API tests and Frontend component tests.
๐ ๏ธ The Tech Stack
- Backend: Jest + Supertest for API testing.
- Frontend: Vitest + React Testing Library for component testing.
๐ Running Tests
From the Root Directory
# Run everything once
npm run test
# Run both suites with coverage reports
npm run test:coverage
Scope | Command | Description
-- | -- | --
Backend | npm --workspace apps/backend run test | Standard Jest run
Backend | npm --workspace apps/backend run test:watch | Re-runs on save
Frontend | npm --workspace apps/web run test | Standard Vitest run
Frontend | npm --workspace apps/web run test:ui | Interactive Vitest UI
๐ Current Coverage
Backend (API)
We use Supertest to simulate HTTP requests without actually starting the server on a port.
โ
GET all menu items / GET by ID
โ
POST/PUT/DELETE operations
โ
Error handling (404, 500, DB Connection failures)
Frontend (UI)
We use React Testing Library to test user-visible behavior (like clicking "Add to Cart") rather than implementation details.
โ
Redux state changes (Pending/Fulfilled)
โ
Loading, Empty, and Error UI states
โ
API Mocking via Vitest
๐ก Best Practices
1. Mock the Database
Never hit your real database in a unit test. Use jest.mock to simulate Prisma responses.
JavaScript
jest.mock('../src/config/database');
const db = require('../src/config/database');
test('should return data', async () => {
db.query.mockResolvedValue({ rows: [{ id: 1, name: 'Test' }] });
// ... expect response to match mock
});
2. Test User Behavior
On the frontend, don't test the internal state of a component. Test what the user sees.
Bad: expect(component.state.isLoading).toBe(true)
Good: expect(screen.getByText('Loading...')).toBeInTheDocument()
๐ Troubleshooting
Module not found: Clear the Jest cache: cd apps/backend && npx jest --clearCache.
Timeouts: If an API test fails, ensure you are clearing mocks: beforeEach(() => jest.clearAllMocks()).