diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 11c837c..36b63d5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,6 +9,10 @@ on: - opened - synchronize +env: + # Test database on the self-hosted runner + DATABASE_URL: postgresql://ci:ci@localhost:5432/ci + jobs: lint-format-and-test: runs-on: [self-hosted] @@ -21,14 +25,20 @@ jobs: - name: Install dependencies run: pnpm install --frozen-lockfile - - name: Write env file - run: > - printf 'DATABASE_URL="postgresql://ci:ci@localhost:5432/ci"\n' > - packages/db/.env + - name: Write env files + run: | + printf 'DATABASE_URL="%s"\n' "$DATABASE_URL" > apps/backend/.env + cp apps/backend/.env packages/db/.env - name: Generate Prisma client run: pnpm --filter @t09-project-2/db run db:generate + - name: Apply database migrations + run: pnpm --filter @t09-project-2/db run db:deploy + + - name: Seed test database + run: pnpm --filter @t09-project-2/db run db:seed + - name: Check formatting run: pnpm check diff --git a/README.md b/README.md index fd03ca1..d4d10a0 100644 --- a/README.md +++ b/README.md @@ -81,8 +81,10 @@ Frontend: http://localhost:3000 ยท GraphQL API: http://localhost:3001 | `pnpm build` | Build all workspaces that define a build | | `pnpm typecheck` | Typecheck all workspaces | | `pnpm lint` / `pnpm format` / `pnpm check` | Lint, auto-fix format, and check formatting | -| `pnpm test` | Run the frontend component tests once | -| `pnpm test:watch` | Re-run tests while developing | +| `pnpm test` | Run frontend and backend tests | +| `pnpm test:frontend` | Run only the frontend component tests once | +| `pnpm test:frontend:watch` | Re-run frontend tests while developing | +| `pnpm test:backend` | Run only the backend tests once | | `pnpm db:setup` | Provision PostgreSQL natively on Ubuntu (see below) | | `pnpm db:generate` | Regenerate the Prisma client and Pothos types | | `pnpm db:push` | Push the Prisma schema to the database (prototyping) | @@ -116,16 +118,18 @@ pnpm check # Runs the prettier formatter in check mode, so will not write to any The frontend uses [Vitest](https://vitest.dev/) with [Testing Library](https://testing-library.com/docs/react-testing-library/intro/) and jsdom. Component tests live in `apps/frontend/src/components/__tests__`, and shared fixtures and providers are in `apps/frontend/src/test`. The tests cover rendering, user interaction, form validation, routing and the map wrappers without needing a running backend. ```bash -pnpm test # Run all component tests once -pnpm test:watch # Re-run tests while developing +pnpm test:frontend # Run all component tests once +pnpm test:frontend:watch # Re-run tests while developing ``` The backend API has Playwright end-to-end tests that send requests to the running GraphQL API. They require PostgreSQL, the backend `.env`, and the sample data created by `pnpm db:seed` (which replaces existing database rows). The test command starts the backend automatically; set `API_BASE_URL` to use an already-running API instead. ```bash -pnpm test:e2e:backend +pnpm test:backend ``` +`pnpm test` runs the frontend component tests and the backend end-to-end tests together. CI applies the migrations and seeds the test database before running it. + ### Deployment The deployment of both the backend and frontend is automated using the github action found in `./.github/workflows/deploy-to-vm.yaml` diff --git a/apps/backend/package.json b/apps/backend/package.json index c44c870..a955de4 100644 --- a/apps/backend/package.json +++ b/apps/backend/package.json @@ -5,7 +5,7 @@ "scripts": { "dev": "dotenv -e .env -- tsx watch src/index.ts", "start": "dotenv -e .env -- tsx src/index.ts", - "test:e2e": "dotenv -e .env -- playwright test", + "test": "dotenv -e .env -- playwright test", "typecheck": "tsc --noEmit" }, "dependencies": { diff --git a/apps/backend/playwright.config.ts b/apps/backend/playwright.config.ts index ae30a41..d618eb7 100644 --- a/apps/backend/playwright.config.ts +++ b/apps/backend/playwright.config.ts @@ -21,7 +21,7 @@ export default defineConfig({ command: 'pnpm run start', cwd: fileURLToPath(new URL('.', import.meta.url)), url: `${apiBaseURL}/?query=%7B__typename%7D`, - reuseExistingServer: !process.env.CI, + reuseExistingServer: true, timeout: 120_000, }, } diff --git a/apps/frontend/src/components/__tests__/Map.test.tsx b/apps/frontend/src/components/__tests__/Map.test.tsx index e05d54c..015db64 100644 --- a/apps/frontend/src/components/__tests__/Map.test.tsx +++ b/apps/frontend/src/components/__tests__/Map.test.tsx @@ -28,7 +28,7 @@ interface CircleMarkerProps { } interface PopupProps { - children: ReactNode + children?: ReactNode } vi.mock('react-leaflet', () => ({ diff --git a/package.json b/package.json index 9b0bf2d..aa1347d 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,10 @@ "build": "pnpm --recursive run build", "typecheck": "pnpm --recursive run typecheck", "lint": "eslint .", + "test": "pnpm --parallel --recursive run test", "test:frontend": "pnpm --filter @t09-project-2/frontend run test", - "test:backend": "pnpm --filter @t09-project-2/backend run test:e2e", - "test:watch": "pnpm --filter @t09-project-2/frontend run test:watch", + "test:frontend:watch": "pnpm --filter @t09-project-2/frontend run test:watch", + "test:backend": "pnpm --filter @t09-project-2/backend run test", "format": "prettier --write . && eslint . --fix", "check": "prettier --check .", "generate-routes": "pnpm --filter @t09-project-2/frontend run generate-routes",