Skip to content

Latest commit

 

History

History
130 lines (96 loc) · 19.9 KB

testing.md

File metadata and controls

130 lines (96 loc) · 19.9 KB

Testing

Continuous integration

The root CI workflow runs for every pull request. It installs the pinned pnpm workspace, runs lint and type checks, builds both apps, and runs the API Jest suite and the frontend unit tests. Pull requests currently skip the browser tests. The workflow is meant to install the Playwright browsers and run pnpm test on pull requests, but its condition inputs.browser-tests != false is false when there is no input, as on a pull request, so those two steps are skipped. The same happens on a manual run of the workflow. This is a known bug that has not been fixed (noted 2026-11-30). CI checks formatting, vets, builds and tests apps/worker in the same job, in every run, the deploy workflows included (worker tests). Superseded: this said that CI did not build or check the worker. CI restores the local Turborepo cache (.turbo/cache), so lint, type-check and build results of an unchanged app are replayed. The test task is never cached: the suites depend on a database, and the web browser tests also run the API's code (2026-11-30). The four deploy workflows (api-production, api-production-dev, web-prodtest, web-production-dev) call the same workflow with browser-tests: false (since 2026-11-19). They lint, type-check and build, then run only the API Jest suite and the frontend unit tests, and skip the Playwright install and browser suite, which took about 25 minutes of a 27 minute deploy. Superseded, 2026-11-30: this section said that pull requests still run the full suite; see the condition bug above. The PR preview workflow web-prtests was removed on 2026-11-29 (security). The workflow uses the pull_request event, read-only repository permissions, and no repository secrets, so fork contributions can run the same checks safely. The GitHub ruleset for main requires the validate check for pull requests and requires one approval with review threads resolved. Tamal's account is the only bypass actor, so he can push directly to main.

apps/web has two kinds of automated tests. A Playwright E2E suite (apps/web/e2e/), added September 2026, replaced an earlier Cypress setup (cypress/e2e/smoke.spec.js, organizationSetup.spec.js) that existed but was never actually wired into CI. Vitest unit tests for pure functions were added in November 2026 (see "Unit tests (Vitest)" below). There is still no component or hook test runner for this app - see "What this doesn't cover" below.

This suite is deliberately full-stack: a real browser (via Playwright) drives a real, running apps/web dev server, which itself makes real HTTP requests to a real, running apps/api instance — nothing here is mocked, except email delivery (see "The password-reset outbox" below). apps/api has its own, separate Jest+Supertest suite for API-level coverage; see apps/api/docs/specs/testing.md for that side. Running pnpm test from the repo root runs both suites, via Turborepo, at the same time — that's what "test the API and the UI at once" means in this repo.

Where coverage lives — read this before adding a test

apps/web/e2e/ is organized feature-first, mirroring apps/web/src/features/<name>/ — the same convention docs/specs/README.md describes for the app itself. Each feature folder under e2e/ has its own README.md naming exactly what it covers, what it deliberately doesn't, and why:

Folder Feature Coverage doc
e2e/authentication/ Sign up, sign in, forgot/reset password, the route guard README
e2e/landing-home/ The marketing home page, hero CTA, footer newsletter README
e2e/onboarding-profile/ Individual editor navigation/save/exit and retired profile URLs; browser specs updated but not rerun for removal README
e2e/organizations/ The organization signup→draft→live setup wizard, responsive layout of the directory and public profile, and the directory's endless scroll README
e2e/events/ Public event directory filtering and responsive layout, organizer publishing, the attendee journey, online-event publishing, and editing and cancelling an event, leaving, capacity, the attendee list and the optional details (on 2026-11-23 the folder (then 50 tests) ran on Chromium, Firefox and WebKit, 13 of the 150 results failed (a real My Events label bug and three stale test expectations), and after the fixes every one passed). on 2026-11-29 the whole folder, including event-create-stepper.spec.ts, passed 273 of 273 on the three browsers README

e2e/smoke.spec.ts stays at the top level — it's cross-cutting infrastructure (backend reachability, basic nav) rather than any one feature's own behavior. e2e/env.ts and e2e/global-setup.ts are shared plumbing, not tests.

Before adding coverage for a feature, check whether it already has a folder here and read that folder's README.md first — it documents what's already covered, what's deliberately skipped and why (often because the underlying app code is unreachable/dead, not because testing it was skipped), and any test-data gotchas specific to that feature. A feature with no folder yet (dashboard, donations/payments) has no E2E coverage at all — see "What this doesn't cover".

Why Playwright, not Cypress

Cypress runs inside the browser, which makes cross-origin flows (this app's Google OAuth popup — see authentication.md) and multi-tab scenarios structurally awkward. Playwright drives the browser from outside via CDP, so cross-origin and multi-tab both work naturally, it runs Chromium/Firefox/WebKit from one API (all three run by default here — see playwright.config.ts's projects), and it has a native API-request context so the same test runner can also hit REST endpoints directly with no browser involved (e2e/smoke.spec.ts's "Backend reachability" tests, and every feature folder's API-seeding helpers, do exactly this).

The stack, and how it stays isolated from real local dev

A developer's normal pnpm dev runs the web app on port 3000 and the API on port 5050, against whatever development database is configured (see AGENTS.md / the memory note on local dev startup). The Playwright suite must never collide with, or read/write, that real session - so it uses entirely separate, dedicated infrastructure:

Local dev Playwright e2e
Web port 3000 3001
API port 5050 5051
Database karmacircle_dev karmacircle_e2e, emptied each run
Outgoing email an in-memory development outbox, never a real send an in-memory test outbox, never a real send
  • apps/web/e2e/env.ts is the single source of truth for the two e2e ports — imported by playwright.config.ts, global-setup.ts, and every spec file that needs to call the API directly.
  • apps/web/playwright.config.ts's webServer array starts both servers itself before any test runs, and tears them down after (locally, reuseExistingServer: true keeps them warm across repeated pnpm test runs in the same session) - a clean checkout needs a running local PostgreSQL server, and beyond that only pnpm install --frozen-lockfile and npx playwright install.
    • The API entry runs apps/api/tests/e2e/server.ts - a real, listening instance of the API backed by its own PostgreSQL database, distinct from how Jest drives the same createApp() in-process with nothing listening. See apps/api's own testing spec for that file's details, including the test-only POST /__test__/reset route it exposes. Since November 2026 that server is an outer Express app that mounts tests/e2e/test-routes.ts in front of createApp(). It adds one E2E-only route, POST /__test__/events/:uid/backdate (body { daysAgo }, a positive number, default 1), which moves an event into the past: startTime becomes daysAgo days before now, endTime two hours later, and startDate/endDate the matching UTC day starts. It returns 204, 400 for a bad daysAgo, or 404 for an unknown uid, and leaves registrations untouched. It exists because the API refuses to create a past event and refuses to join one that has started, so browser tests publish a future event, optionally join it, then backdate it. It exists only on the isolated E2E server (port 5051), never in the real app.
    • The web entry runs vite --port 3001 --strictPort with VITE_API_URL overridden to the e2e API's URL, so the dev server built for this run points at the isolated API, not local dev's 5050.
    • Both commands run via npx <bin> with an explicit cwd, not pnpm --filter/pnpm exec — pnpm itself isn't guaranteed to be on PATH in every environment (this repo's local dev has the same gap; pnpm is invoked via npx pnpm@<version> when it isn't installed globally). npx resolves each package's own local devDependency bin from its own node_modules/.bin regardless of what's on PATH.
  • apps/web/e2e/global-setup.ts runs once, after the servers are up and before any test, and POSTs the e2e API's /__test__/reset — guaranteeing the suite starts from an empty database every run.

The password-reset outbox

apps/api/src/config/mailer.ts never calls Resend unless NODE_ENV==="production". Development and test capture reset links in an in-memory outbox, readable through loopback-only GET /__dev__/last-reset-url?email=... locally or GET /__test__/last-reset-url?email=... in the isolated e2e server. This lets e2e/authentication/password-reset.spec.ts complete a real forgot→reset→sign-in-with-the-new-password round trip without a real mail provider. See apps/api's testing spec and that spec file's own comments for the details.

Why no reset between spec files

Playwright runs spec files in parallel across workers by default (fullyParallel: true). A mid-suite database reset would race whichever other file's tests are running concurrently, so there isn't one. Instead, every spec generates its own unique data — almost always a Date.now()-based suffix on emails/names (see any feature folder's uniqueEmail() helper) — the same isolation pattern the Cypress specs this suite replaced already used, kept because it's still the right approach under Playwright's parallelism.

Unit tests (Vitest)

Added November 2026 for pure functions (no DOM, no components). Vitest ^5.1.0 is an apps/web devDependency; the config is apps/web/vitest.config.mjs, deliberately separate from vite.config.mjs (no PWA, SVGR, React or Tailwind plugins) and from playwright.config.ts. It includes only src/**/*.test.ts (tests sit next to the helper they test) and excludes e2e/**, so neither runner can pick up the other's files. The environment is node, and the config pins TZ=UTC and LC_ALL/LANG=en_US.UTF-8 so date and currency assertions are deterministic on every machine and in CI. Its path aliases mirror vite.config.mjs and tsconfig.json; keep all three in sync when an alias is added.

  • pnpm test:unit (vitest run) - run once. pnpm test:unit:watch - watch mode.
  • The test script is now vitest run && playwright test, so pnpm test from apps/web and root pnpm test (Turborepo) run the unit tests before Playwright. CI currently runs test:unit on its own, without Playwright (see Continuous integration).
  • Last run, 2026-11-23: 17 test files passed, 244 tests passed and no todo (the formatDuration and fundedPercent bad-input cases are now real tests), adding calendarLinks.test.ts (6) and removing getFormattedDate.test.ts with the unused code it covered. Earlier run, 2026-11-16: 13 test files passed, 196 tests passed and 2 todo (198 total), adding eventMode.test.ts, eventTags.test.ts, eventTimezone.test.ts, richText.test.ts, components/RichText.test.ts and constants/eventTypes.test.ts to the files below (RichText.test.ts renders with react-dom/server, so it needs no DOM). Before those, 7 files and 121 tests. The files are formatEventFacts.test.ts (including formatSpotsLeft), toDisplayEvent.test.ts (which also covers the cancelled mapping and the capacity counts), eventToFormState.test.ts, buildEventChanges.test.ts and eventDetailFields.test.ts under src/features/events/utils/, and src/utils/formatMoney.test.ts.
  • Added 2026-11-26: validateEvent.test.ts, joinLinkNote.test.ts and myEvents.test.ts under src/features/events/utils/, and src/features/authentication/utils/returnPath.test.ts.
  • Not covered: components and hooks. Superseded: convertToBase64 was listed here; the helper has been deleted.

Running it

From apps/web, or from the repo root with --filter karmacircle-frontend:

  • pnpm test - runs the Vitest unit tests, then Playwright headless on all configured browser projects.
  • pnpm test:ui — Playwright's interactive UI mode (time-travel through a run, pick a single test).
  • pnpm test:headed — headed (visible) browser windows.
  • pnpm test:report — reopen the last run's HTML report.
  • CAPTURE_TOUR=1 pnpm test — also saves numbered screenshots of the organization-setup journey into test-results/tour/.
  • npx playwright test e2e/<folder>/ — run just one feature's spec files.

From the repo root, plain pnpm test runs this suite and apps/api's Jest suite together (Turborepo runs each workspace's own test script; neither depends on the other, so they run concurrently).

CI does not currently run the Playwright suite (see Continuous integration), so run it locally. No Playwright run is recorded since the move to PostgreSQL.

Known sharp edges (found while building this suite)

Real, current app behavior the suite had to be written against — not aspirational — plus a handful of genuine bugs and stale-doc corrections the suite surfaced along the way (see each feature folder's own README.md and known-issues.md for the full writeups):

  • Playwright's getByRole respects real ARIA semantics; Cypress's cy.contains("button", …) didn't. The signup flow's account-type switch is a role="tab" pair (a deliberate design choice — see Auth.tsx), not role="button", even though it's a <button> element.
  • Playwright is strict about ambiguous matches; Cypress silently took the first one. getByText(...) throws if more than one element matches — several assertions need .first(), { exact: true }, or a role-scoped locator where Cypress's cy.contains() would have quietly picked an element and moved on. Watch for this especially with short/common words ("Live" collides with the setup page's rotating testimonial quote; "Profile" collides with the account-menu's own "Your Profile" link).
  • Client (Redux/localStorage) state only changes through the app's own actions. Seeding an account directly against the e2e API sets up a database record but touches no client state. A test that needs isLoggedIn true has to go through the real signup/signin UI at least once.
  • OrganizationSetup.tsx's Continue button reports as unavailable via aria-disabled, not the native disabled attribute, whenever a required question is blank — toBeDisabled/toBeEnabled treat the two the same (both resolve through Playwright's own getAriaDisabled), but only aria-disabled leaves the element clickable, so a press while blocked still reaches the onSubmit guard and shows the red setup-required-note message rather than doing nothing. A genuinely in-flight save (saving) still uses the native attribute. Exercising the clickable-while-blocked path needs .click({ force: true }) — Playwright's own actionability check refuses a plain .click() on anything aria-disabled.
  • A live organization never sees the setup questions - OrganizationSetup.tsx renders the profile editor once the record is live, and organization-setup.spec.ts asserts that. Superseded: this entry said back() from the first question was a silent no-op for a live organization.
  • Saving the second setup step switches the page to the editor. An organization goes live once about/reach's required fields are saved, and the optional presence step (the support toggle) is then never shown, so a test should expect the "Edit organization" heading rather than a redirect to the public profile. Superseded: this entry said the wizard kept going through a presence step of logo, socials, address, leadership and sponsorship questions.
  • /organizations' search field can render a live-suggestions dropdown (DirectoryToolbar's Combobox) with an option matching the same text as a result card — a plain getByText(name, { exact: true }) after typing into the search field is a strict-mode violation between the two; scope to the card's own h2 (getByRole("heading", { level: 2, name })) instead.
  • The retired /user/:userName route remains covered as a not-found case. The active individual editor is /user/setup; the E2E coverage documents both the removed route and the current editor flow in e2e/onboarding-profile/README.md.
  • Use pressSequentially, not fill, for autocomplete-style fields (the organization setup city field). The suggestion dropdown is driven by real keystroke events; fill() sets the value in one shot and never triggers it. Plain text fields (name, description, email, etc.) work fine with fill().

What this doesn't cover

  • No component or hook-level tests exist for apps/web. Vitest covers pure helpers only (see "Unit tests (Vitest)"); components and hooks have no existing pattern to follow, and would need a DOM environment (jsdom or happy-dom) first.
  • Feature folders with no E2E coverage at all yet: dashboard, donate-shop-trending (payments). The products module was removed on 2026-12-02. The Events feature has directory, publishing, attendee-journey, edit and cancel coverage (see the Events README). See each feature's own summary spec (docs/specs/<feature>.md) before writing tests for one of these - several have the same "less is actually reachable than the docs used to claim" texture the authentication/onboarding-profile work above surfaced.
  • Google OAuth sign-in isn't covered anywhere — it needs a real (or mocked) Google consent screen, which this suite doesn't attempt.
  • Animation/motion is not asserted on anywhere in this suite (see landing-home/README.md). Responsive layout is checked in places, by specs that set their own viewport size (for example events-responsive.spec.ts, organizations-responsive.spec.ts and navbar-menus.spec.ts); the three configured projects are desktop browsers, and this repo has no visual-snapshot tooling. Superseded, 2026-11-30: this said responsive breakpoints were asserted on nowhere.

Keeping this file honest

If you add a new feature folder under e2e/, add it to the table above (and give the folder its own README.md, matching the existing ones' shape: a file table, test-data notes, what's not covered and why, a "keeping this honest" note). If you change the dedicated ports, the reset strategy, the outbox, or the browser projects, update this file and apps/api/docs/specs/testing.md together — they describe two ends of one integration. If you fix one of the "known sharp edges" items above, remove it from here (and from wherever else it's documented) in the same change.