Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,123 @@ jobs:
cache-dependency-path: client/yarn.lock
- run: yarn
- run: yarn test
env:
# entities.test.js renders the full admin app ~140 times in one
# process (every resource's List/Create/Edit page); the default
# heap limit is tight enough on CI runners that later tests in the
# same run degrade/fail from GC pressure even though they pass
# fine in isolation.
NODE_OPTIONS: --max-old-space-size=4096

e2e-tests:
name: Run browser E2E tests
runs-on: ubuntu-latest
needs: prepare
if: ${{ needs.prepare.outputs.client == 'true' || needs.prepare.outputs.server == 'true' }}
services:
mysql:
image: mysql:8.4
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mysql_database
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping -proot"
--health-interval=10s
--health-timeout=5s
--health-retries=10
env:
MYSQL_HOST: 127.0.0.1
MYSQL_PORT: 3306
MYSQL_USER: mysql_user
MYSQL_PASSWORD: mysql_password
MYSQL_DATABASE: mysql_database
JWT_SECRET: jwt_secret
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: actions/setup-node@v4
with:
node-version: '20'

# data.sql (the run-without-docker checkpoint dump) both creates the
# schema/seed data and grants mysql_user its privileges, but assumes
# the account itself already exists — the docker-based setup creates
# it via the image's own MYSQL_USER/MYSQL_PASSWORD env vars, which the
# plain `mysql:` service image doesn't expose the same way.
- name: Set up database
run: |
mysql -h127.0.0.1 -uroot -proot -e "
CREATE USER IF NOT EXISTS 'mysql_user'@'%' IDENTIFIED BY 'mysql_password';
GRANT ALL PRIVILEGES ON mysql_database.* TO 'mysql_user'@'%';
FLUSH PRIVILEGES;
"
mysql -h127.0.0.1 -uroot -proot mysql_database < db/data.sql

- name: Install server deps, run pending migrations
working-directory: ./server
run: |
yarn
NODE_ENV=development yarn typeorm:run

# Backgrounded within this step, with the wait loop right after it in
# the same step: the workflow can't move on to the next step until
# this one is confirmed answering requests — a later step starting
# before this one is truly ready would just fail on connection-refused
# instead of waiting for it.
- name: Start server
working-directory: ./server
run: |
NODE_ENV=development PORT=3001 yarn start:dev > /tmp/server.log 2>&1 &
ready=false
for i in $(seq 1 60); do
# Any HTTP response (even a 400 from an empty body) means the
# server is up; curl's own exit code only tells us the TCP
# connection was refused or not. GitHub Actions runs this script
# under `bash -e`, and `code=$(curl ...)` is a plain assignment
# (not exempt the way `curl ... && break` is) — so on the very
# first, expected-to-fail attempt curl's connection-refused exit
# code aborted the whole step before the retry loop ran at all.
# `|| true` neutralizes that; emptiness is still checked below.
code=$(curl -s -o /dev/null -w "%{http_code}" -X POST http://localhost:3001/auth/register -H "Content-Type: application/json" -d '{}' || true)
if [ -n "$code" ] && [ "$code" != "000" ]; then ready=true; break; fi
sleep 1
done
if [ "$ready" != "true" ]; then
echo "Server did not become ready:"; cat /tmp/server.log; exit 1
fi

- name: Install client deps, start client
working-directory: ./client
run: |
yarn
yarn start > /tmp/client.log 2>&1 &
for i in $(seq 1 60); do
curl -sf -o /dev/null http://localhost:3000 && break
sleep 1
done
curl -sf -o /dev/null http://localhost:3000 || (echo "Client did not become ready:"; cat /tmp/client.log; exit 1)

- name: Install E2E deps
working-directory: ./e2e
run: |
yarn
# playwright.config.ts prefers a pre-installed Chromium at
# $PLAYWRIGHT_CHROMIUM_PATH (present in some dev sandboxes) but
# falls back to Playwright's own managed browser when that path
# doesn't exist — which is always the case on this runner.
npx playwright install --with-deps chromium

- name: Run E2E tests
working-directory: ./e2e
run: yarn test

- uses: actions/upload-artifact@v4
if: failure()
with:
name: playwright-report
path: e2e/test-results/
retention-days: 7

3 changes: 3 additions & 0 deletions e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
test-results/
playwright-report/
112 changes: 112 additions & 0 deletions e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Browser E2E (Playwright)

Real-browser tests, driving the actual client + server + a real database — not
mocks, not jsdom. Deliberately narrow: three flows chosen for what only a real
browser + server + database can catch, not for breadth (the jsdom-based
`entities.test.js` smoke tests already cover every resource page cheaply —
see "What this catches that jsdom smoke tests don't" below). Wired into CI
as the `e2e-tests` job in `.github/workflows/run-tests.yml`, which stands up
a real MySQL service container, runs migrations, boots server + client, then
runs this suite against them.

## Why Playwright, and which browser it launches

Some dev sandboxes this pilot runs in ship a pre-installed Chromium under
`$PLAYWRIGHT_BROWSERS_PATH` (`/opt/pw-browsers`), so there's no download step
there — `playwright.config.ts` points `launchOptions.executablePath` at
`/opt/pw-browsers/chromium` (a stable symlink to the current versioned
build) when that path exists, which avoids both a network fetch that may
not be reachable in a sandbox and a redundant download of a browser that's
already there. Override with `PLAYWRIGHT_CHROMIUM_PATH` if your environment
installs it elsewhere.

CI (and most other environments) doesn't have that path — there,
`executablePath` is left unset and Playwright falls back to its own managed
browser, installed by the `Install E2E deps` step's `npx playwright install
--with-deps chromium`. Run that same command locally first if your
environment has neither a pre-installed Chromium nor one Playwright has
already downloaded.

## Prerequisites: a live stack

Playwright needs the client and server actually running against a real
database — see the `run-without-docker` skill (`multi-repo-codespace/.github/skills/`)
for the full walkthrough if you don't have Docker. Short version:

```bash
# 1. MySQL, once
apt-get install -y mysql-server-8.0 && service mysql start
mysql -u root -e "
CREATE DATABASE mysql_database CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
CREATE USER 'mysql_user'@'localhost' IDENTIFIED BY 'mysql_password';
GRANT ALL PRIVILEGES ON mysql_database.* TO 'mysql_user'@'localhost';
GRANT SYSTEM_USER ON *.* TO 'mysql_user'@'localhost';
FLUSH PRIVILEGES;
"

# 2. Load the schema checkpoint + remaining migrations (fast path — see
# run-without-docker for the full-replay alternative)
mysql -u root mysql_database < db/data.sql
cd server && NODE_ENV=development yarn typeorm:run

# 3. Start server and client — NODE_ENV=development is required, not optional:
# server/shared/utils/bootstrap.util.ts only allows the browser's
# localhost origin through CORS when NODE_ENV === 'development'. Without
# it every request from the client to the API fails as a silent CORS
# error in the browser (curl/supertest calls don't hit this — they don't
# send an Origin header — so this is easy to miss when smoke-testing the
# API directly).
NODE_ENV=development MYSQL_HOST=localhost MYSQL_PORT=3306 \
MYSQL_USER=mysql_user MYSQL_PASSWORD=mysql_password MYSQL_DATABASE=mysql_database \
JWT_SECRET=jwt_secret PORT=3001 yarn start:dev # server, separate shell

cd client && yarn start # client, separate shell — picks 3000, falls back if taken
```

Default API URL resolution in the client (`constantsProvider.js`) is
`http://<hostname>:<client-port + 1>`, so client on 3000 + server on 3001
just works with no extra client-side config.

## Running

```bash
cd e2e
yarn install
yarn test # against http://localhost:3000
E2E_BASE_URL=... E2E_API_URL=... yarn test # against a different stack
```

## What's covered

All three specs share `tests/helpers.ts`'s `registerTestUser()` — registers a
fresh user via the API (same pattern as `server/test/*-crud.e2e-spec.ts`, not
seeded DB fixtures — the checkpoint dump's password hashes aren't real bcrypt
hashes of any known password) — and `loginThroughUi()`, which logs that user
in through the actual UI form.

- `tests/login-dashboard.spec.ts` — confirms the dashboard loads after login
(sidebar renders, no leftover login form, no error page).
- `tests/crud-roundtrip.spec.ts` — creates, edits, and deletes a `klass_type`
through the real UI, checking after each step that the change actually
round-tripped through the server and database (not just that the request
didn't throw). `klass_type` is used because, as a non-admin user, it only
needs two plain fields (no reference-picker dropdown to drive).
- `tests/export-download.spec.ts` — downloads a real Excel export and checks
the file is non-empty. Exporting is gated on the user's `isPaid` flag, which
nothing in the app itself can set for a fresh registration, so
`tests/helpers.ts`'s `markUserAsPaid()` reaches directly into the database —
setup for the flow under test, same reasoning as `registerTestUser()`
reaching around the UI via the API.

## What this catches that jsdom smoke tests don't

Real browser rendering, real CORS/cookie/auth wiring end to end, an actual
multi-page flow (form submit → redirect → authenticated data fetch →
render), a create/edit/delete that really persists through the server and
database instead of a mocked `dataProvider`, and a real server-generated
file (the export test) — none of which a jsdom test with a mocked
`dataProvider` can exercise, since there's no real server or file on the
other end of the call. `entities.test.js`'s smoke tests are cheaper and
catch a different, much larger class of bug (any resource crashing on
mount) across every resource in the app — see that file's comments for the
trade-off between the two.
11 changes: 11 additions & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "react-admin-nestjs-e2e",
"private": true,
"scripts": {
"test": "playwright test"
},
"devDependencies": {
"@playwright/test": "^1.48.0",
"mysql2": "^3.11.0"
}
}
27 changes: 27 additions & 0 deletions e2e/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { defineConfig } from '@playwright/test';
import { existsSync } from 'fs';

// The sandboxed dev environment this pilot was first built in ships a
// pre-installed Chromium under $PLAYWRIGHT_BROWSERS_PATH (/opt/pw-browsers)
// with a stable `chromium` symlink to the current versioned build — using
// it avoids a redundant download there. GitHub Actions runners (and most
// other environments) don't have that path at all, so this only pins
// executablePath when the path is actually present; otherwise it's left
// undefined and Playwright falls back to its own managed browser (installed
// via `npx playwright install --with-deps chromium` — see the CI workflow
// and this package's README).
const sandboxChromium = process.env.PLAYWRIGHT_CHROMIUM_PATH || '/opt/pw-browsers/chromium';

export default defineConfig({
testDir: './tests',
timeout: 30000,
retries: 0,
use: {
baseURL: process.env.E2E_BASE_URL || 'http://localhost:3000',
launchOptions: {
...(existsSync(sandboxChromium) ? { executablePath: sandboxChromium } : {}),
args: ['--no-sandbox'],
},
screenshot: 'only-on-failure',
},
});
54 changes: 54 additions & 0 deletions e2e/tests/crud-roundtrip.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { test, expect } from '@playwright/test';
import { registerTestUser, loginThroughUi } from './helpers';

// Exercises a real dataProvider <-> nestjs-crud round trip through the
// actual UI: create, appears in the list, edit, change is visible, delete,
// gone from the list. jsdom's entities.test.js smoke tests (mocked
// dataProvider) prove pages don't crash on mount; they can't prove a create
// actually persists, an edit actually updates, or a delete actually removes
// a row — that needs a real server and a real database.
//
// klass_type is used because as a non-admin user (a fresh registration,
// same as the login test) it only requires two fields — key (number) and
// name (text) — with no reference picker to drive; every other field on
// this app's entities that was checked either needed a reference-select
// dropdown or is admin-only.
test('create, edit and delete a klass type through the real UI and API', async ({ page }) => {
const { username, password } = await registerTestUser();
await loginThroughUi(page, username, password);

const uniqueSuffix = Date.now();
const originalName = `E2E Klass ${uniqueSuffix}`;
const updatedName = `E2E Klass ${uniqueSuffix} (updated)`;

// --- Create ---
await page.goto('/klass_type/create');
await page.locator('input[name="key"]').fill('1');
await page.locator('input[name="name"]').fill(originalName);
await page.locator('button[type="submit"]').first().click();

// A successful create redirects to the list, which is the read half of
// the round trip: the row has to come back from a real GET, not just be
// assumed present because the POST returned 2xx.
await expect(page).toHaveURL(/\/klass_type$/, { timeout: 10000 });
await expect(page.getByText(originalName, { exact: true })).toBeVisible({ timeout: 10000 });

// --- Edit ---
await page.getByText(originalName, { exact: true }).click();
await expect(page).toHaveURL(/\/klass_type\/\d+$/, { timeout: 10000 });
const nameInput = page.locator('input[name="name"]');
await nameInput.fill(updatedName);
await page.locator('button[type="submit"]').first().click();

await expect(page).toHaveURL(/\/klass_type$/, { timeout: 10000 });
await expect(page.getByText(updatedName, { exact: true })).toBeVisible({ timeout: 10000 });
// The pre-edit name should be gone, not just the new one added alongside it.
await expect(page.getByText(originalName, { exact: true })).toHaveCount(0);

// --- Delete ---
await page.locator('input[type="checkbox"]').nth(1).check();
await page.getByRole('button', { name: 'מחק' }).click();
await page.getByRole('button', { name: 'אשר' }).click();

await expect(page.getByText(updatedName, { exact: true })).toHaveCount(0, { timeout: 10000 });
});
48 changes: 48 additions & 0 deletions e2e/tests/export-download.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { test, expect } from '@playwright/test';
import fs from 'fs';
import { registerTestUser, loginThroughUi, markUserAsPaid } from './helpers';

// Downloads a real Excel export of a real list. jsdom cannot exercise this
// at all: the server builds actual .xlsx bytes (see server/shared's
// BaseReportGenerator family) and the client turns a base64 JSON payload
// into a real browser download via file-saver — there is no DOM to render,
// so a mocked-dataProvider unit test has nothing to assert against here.
// This is the file-download counterpart to crud-roundtrip's persistence
// check: it proves the export pipeline produces real, non-empty content,
// not just that clicking the button doesn't throw.
test('exports a real, non-empty Excel file for a resource', async ({ page }) => {
const { username, password } = await registerTestUser();

// Exporting is gated on the user's isPaid flag (see server/shared's
// base-entity.util validateUserHasPaid). There is no self-service way to
// become paid through the app itself — billing happens outside it — so
// this reaches directly into the DB, same as registerTestUser reaches
// around the UI via the API: setup for the flow under test, not the flow
// itself.
await markUserAsPaid(username);

await loginThroughUi(page, username, password);

// The export button is disabled when the list is empty, so give it one
// row to actually export.
await page.goto('/klass_type/create');
await page.locator('input[name="key"]').fill('1');
await page.locator('input[name="name"]').fill(`Export Test ${Date.now()}`);
await page.locator('button[type="submit"]').first().click();
await expect(page).toHaveURL(/\/klass_type$/, { timeout: 10000 });

await page.getByRole('button', { name: 'ייצא' }).click();

const downloadPromise = page.waitForEvent('download', { timeout: 10000 });
await page.getByRole('menuitem', { name: 'אקסל' }).click();
const download = await downloadPromise;

expect(download.suggestedFilename()).toMatch(/\.xlsx$/);

const path = await download.path();
expect(path).toBeTruthy();
const { size } = fs.statSync(path!);
// A real xlsx (it's a zip container) is at minimum a few hundred bytes;
// an empty/corrupt response would be at or near 0.
expect(size).toBeGreaterThan(200);
});
Loading
Loading