> ## Documentation Index
> Fetch the complete documentation index at: https://system.muzemus.online/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing Strategy

> Overview of MUZE's testing approach across unit, integration, and E2E

## Testing pyramid

```mermaid theme={null}
graph TB
    E2E["Browser E2E<br/>(Playwright)<br/>~10 tests"]
    INT["Backend API E2E<br/>(Jest + Supertest)<br/>~50 tests"]
    UNIT["Unit Tests<br/>(Jest backend / Vitest frontend)"]

    E2E --> INT --> UNIT
```

## Test frameworks

| Layer                    | Framework                | Runner            | Config                                  |
| ------------------------ | ------------------------ | ----------------- | --------------------------------------- |
| Backend unit/integration | Jest (ts-jest)           | `pnpm test`       | `jest` config in `backend/package.json` |
| Backend API E2E          | Jest + Supertest         | `pnpm test:e2e`   | `backend/test/jest-e2e.json`            |
| Frontend unit            | Vitest + Testing Library | `vitest run`      | `frontend/vitest.config.ts`             |
| Frontend E2E             | Playwright               | `playwright test` | `frontend/playwright.config.ts`         |

## Test commands

### Backend

```bash theme={null}
cd backend
pnpm test              # Run all unit tests (Jest)
pnpm test:watch        # Run tests in watch mode
pnpm test:cov          # Run with coverage report
pnpm test:e2e          # Run API E2E tests (Supertest)
```

### Frontend

```bash theme={null}
cd frontend
pnpm test              # Run all unit tests
pnpm test:watch        # Run tests in watch mode
pnpm test:cov          # Run with coverage report
pnpm e2e               # Run Playwright E2E tests
pnpm e2e:ui            # Run E2E with Playwright UI
```

## What is tested

### Backend

| Category           | Coverage | Example                                               |
| ------------------ | -------- | ----------------------------------------------------- |
| Entitlement engine | High     | Criteria evaluation, phase calculation, budget checks |
| Guards             | Medium   | Permissions, scope, session validation                |
| Services           | Medium   | CRUD operations, business logic                       |
| Controllers        | Low      | Request/response handling                             |
| Filters            | Low      | Error mapping                                         |

### Frontend

| Category   | Coverage | Example                               |
| ---------- | -------- | ------------------------------------- |
| Components | Medium   | Form validation, table rendering      |
| Hooks      | Medium   | Custom hooks, React Query integration |
| Utilities  | High     | Pure functions, formatters            |
| Pages      | Low      | Full page rendering                   |
| E2E flows  | Low      | Critical user journeys                |

## Test data management

* **Backend:** Tests use isolated database transactions that roll back after each test
* **Frontend:** Tests use mocked API responses and in-memory state
* **E2E:** Tests run against a dedicated test database, seeded before each suite

## Coverage goals

| Metric                      | Current | Target |
| --------------------------- | ------- | ------ |
| Backend unit test coverage  | \~60%   | 80%    |
| Frontend unit test coverage | \~40%   | 70%    |
| E2E critical path coverage  | \~30%   | 60%    |

## CI integration

Tests run automatically on every push and pull request via GitHub Actions:

```yaml theme={null}
# .github/workflows/ci.yml (planned)
- name: Run backend tests
  run: cd backend && pnpm test

- name: Run frontend tests
  run: cd frontend && pnpm test

- name: Run E2E tests
  run: cd frontend && pnpm e2e
```

**Note:** CI/CD pipeline is a planned enhancement. Tests currently run locally only.
