From 850f53045a40330867f6e21f9dbfa5388374812e Mon Sep 17 00:00:00 2001 From: Ronja Date: Sat, 17 Oct 2026 10:36:37 +0200 Subject: [PATCH] chore: add npm run check and a pull request template npm run check runs the lint, the type checks, the build and all the tests, and fails when a test is skipped or DATABASE_URL is missing, so a green result always means that the search and the API were tested. --- .github/pull_request_template.md | 18 ++++++++ README.md | 2 + package.json | 1 + scripts/check.mjs | 70 ++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 .github/pull_request_template.md create mode 100644 scripts/check.mjs diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..650d10a --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,18 @@ +## Summary + + + +## What's in it + +- + +## Testing + + + +## Checklist + +- [ ] The base branch is `dev` +- [ ] `npm run check` passes, with the database tests included and none skipped +- [ ] The README or `docs/` are updated if the behaviour changed +- [ ] `docs/AI_USAGE.md` has an entry if AI was used diff --git a/README.md b/README.md index 7ada51f..2ebbe60 100644 --- a/README.md +++ b/README.md @@ -337,6 +337,8 @@ git checkout -b my-feature git push -u origin my-feature ``` +**Before you open a pull request**, run `npm run check`. It runs the lint, the type check of the frontend and the backend, the build and all the tests, and it **fails when a test is skipped**. A normal `npm test` also passes when the database tests are skipped (without `DATABASE_URL`), so a green run can hide that the search and the API were never tested. `npm run check` therefore needs the migrated and seeded database from "Database (local setup)". Pull requests go to `dev`; the template in `.github/pull_request_template.md` has the checklist. + Before a pull request can be merged: 1. **A member of IT2810-H26-T29 must approve it.** You cannot approve your own pull diff --git a/package.json b/package.json index 3999596..812fecf 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "lint": "eslint .", "format": "prettier --write .", "test": "vitest", + "check": "node scripts/check.mjs", "preview": "vite preview", "db:generate": "npm run prisma:generate -w backend", "db:migrate": "npm run prisma:migrate -w backend", diff --git a/scripts/check.mjs b/scripts/check.mjs new file mode 100644 index 0000000..bd6b597 --- /dev/null +++ b/scripts/check.mjs @@ -0,0 +1,70 @@ +// `npm run check`: everything a pull request must pass, in one command. +// +// lint, type check of the frontend and the backend, build, and all the tests. +// +// A normal `npm test` also passes when the database tests are skipped (they are +// skipped without DATABASE_URL), so a green run can hide that the search and +// the API were never tested. This script fails in that case: it needs the +// database from backend/.env (see "Database (local setup)" in the README) and +// fails when any test is skipped. + +import { execSync } from 'node:child_process' +import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +const run = (label, command) => { + console.log(`\n=== ${label}`) + try { + execSync(command, { stdio: 'inherit' }) + } catch { + console.error(`\ncheck failed at: ${label}`) + process.exit(1) + } +} + +// The database tests need DATABASE_URL, from the environment or backend/.env. +const hasDatabaseUrl = + Boolean(process.env.DATABASE_URL) || + (existsSync('backend/.env') && + /^\s*DATABASE_URL\s*=\s*\S+/m.test(readFileSync('backend/.env', 'utf8'))) +if (!hasDatabaseUrl) { + console.error( + 'check failed: DATABASE_URL is not set, so the database tests would be skipped.\n' + + 'Set up the database first: see "Database (local setup)" in the README, then run\n' + + '`npm run db:migrate` and `npm run db:seed`.', + ) + process.exit(1) +} + +run('Prisma client', 'npm run db:generate') +if (existsSync('backend/codegen.ts')) { + run('GraphQL types', 'npm run codegen') +} +run('Lint', 'npm run lint') +run('Type check (backend)', 'npm run typecheck -w backend') +run('Type check and build (frontend)', 'npm run build') + +const dir = mkdtempSync(join(tmpdir(), 'check-')) +const report = join(dir, 'vitest.json') +try { + run( + 'Tests', + `npx vitest run --reporter=default --reporter=json --outputFile.json="${report}"`, + ) + const { numTotalTests, numPassedTests, numPendingTests, numTodoTests } = + JSON.parse(readFileSync(report, 'utf8')) + const skipped = numPendingTests + (numTodoTests ?? 0) + console.log( + `\n${numPassedTests} of ${numTotalTests} tests passed, ${skipped} skipped.`, + ) + if (skipped > 0) { + console.error( + `check failed: ${skipped} tests were skipped. The database tests need the migrated and seeded database in backend/.env (npm run db:migrate, npm run db:seed).`, + ) + process.exit(1) + } +} finally { + rmSync(dir, { recursive: true, force: true }) +} +console.log('\ncheck passed.')