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


๐Ÿš€ 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()).