High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
Fix Playwright test scaffolding to work with the Docker dev env.
Kailash Nadhf82179bee22d
diff --git a/dev/app.Dockerfile b/dev/app.Dockerfileindex 0af6e5cb..891ad3c3 100644--- a/dev/app.Dockerfile+++ b/dev/app.Dockerfile@@ -2,7 +2,7 @@ FROM oven/bun:1.4.3 AS bunFROM golang:1.27.2-trixie-RUN apt-get update && apt-get install -y --no-install-recommends python3 \+RUN apt-get update && apt-get install -y --no-install-recommends python3 postgresql-client procps \&& rm -rf /var/lib/apt/lists/* \&& git config --system --add safe.directory /appdiff --git a/dev/docker-compose.yml b/dev/docker-compose.ymlindex 4c21333b..fb2d03c0 100644--- a/dev/docker-compose.yml+++ b/dev/docker-compose.yml@@ -35,6 +35,13 @@ services:init: trueenvironment:DEV_CONFIG: dev/config.toml+ LISTMONK_db__host: db+ LISTMONK_db__user: listmonk-dev+ LISTMONK_db__password: listmonk-dev+ LISTMONK_db__database: listmonk-dev+ MAILHOG_EXTERNAL: "1"+ MAILHOG_URL: http://mailhog:8025+ MAILHOG_SMTP_HOST: mailhogports:- "127.0.0.1:9000:9000"depends_on:diff --git a/docs/docs/content/developer-setup.md b/docs/docs/content/developer-setup.mdindex 989fb6a9..de421fbf 100644--- a/docs/docs/content/developer-setup.md+++ b/docs/docs/content/developer-setup.md@@ -27,5 +27,24 @@ The app is a Go backend that server-renders the admin UI (HTML templates with JSRun `make stop-dev-docker` to stop the container and `make rm-dev-docker` to completely remove it.+### Frontend tests++listmonk maintains a Playwright suite in `tests/` for end-to-end testing the admin UI. It runs in the Docker dev environment that bundles all the necessary dependencies (Postgres, Mailhog, Bug etc.).++#### Running++Follow the steps described above to run and enter the Docker dev environment.++Inside the dev container shell at `/app`:++```sh+make dist++cd tests+bun install+bun playwright install --with-deps chromium+bun run test+```+# Production buildRun `make dist` to build the SSR admin frontend and the Go binary, embedding the static assets into a single self-contained binary, `listmonk`.diff --git a/tests/README.md b/tests/README.mdindex 55c7f7c0..e33c0123 100644--- a/tests/README.md+++ b/tests/README.md@@ -1,20 +1,35 @@# listmonk E2E tests-Playwright UI E2E tests. Requires a built `./listmonk` binary in the repo root with a test Postgres instance (use docker-compose.yml) set in config.toml. Every spec wipes and reinstalls the DB schema (see `resetDB` in `helpers.js`).+Playwright tests for the server-rendered admin UI. See+[Frontend tests](../docs/docs/content/developer-setup.md#frontend-tests) for the+Docker dev workflow, local prerequisites, and configuration options.-`resetDB` also points listmonk's SMTP config at [MailHog](https://github.com/mailhog/MailHog) so mail is captured and testable via its API (`getMail`/`clearMail`). It needs `mailhog` and `psql` on `PATH`; DB credentials are read from the `LISTMONK_db__*` env (falling back to the config.toml defaults).+The suite **wipes and reinstalls the configured database schema** and stops+running listmonk processes. Use disposable data; in the Docker dev environment,+this is the `listmonk-dev` database.-## Setup+From the repository root on the host:```sh-cd tests-bun install-bun playwright install chromium+make build-dev-docker+make run-dev-docker```-## Run+Inside the dev container:```sh-bun run test # runs tests in headless mode-bun run test:ui # Shows the Playwright UI+make build-frontend+make build+cd tests+bun install+bun playwright install --with-deps chromium+bun run test```++Compose configures the database and existing MailHog service automatically.+Rebuild the backend after Go or SQL changes, and reinstall the browser and its+system dependencies when recreating the dev container.++Outside Docker, install the dependencies described in the developer guide.+From `tests/`, use `bun run test` for headless tests or `bun run test:ui` for the+interactive UI.diff --git a/tests/global-setup.js b/tests/global-setup.jsindex a3cf2ff8..39f9071d 100644--- a/tests/global-setup.js+++ b/tests/global-setup.js@@ -1,12 +1,13 @@import { chromium } from '@playwright/test';import { mkdirSync } from 'node:fs';import { login } from './helpers.js';+import { launchOptions } from './browser.js';// Login and create a persistent session for tests.export default async function globalSetup() {mkdirSync(new URL('.auth', import.meta.url).pathname, { recursive: true });- const browser = await chromium.launch();+ const browser = await chromium.launch(launchOptions);await login(await browser.newContext());await browser.close();}diff --git a/tests/helpers.js b/tests/helpers.jsindex ae4ea204..ef3bb111 100644--- a/tests/helpers.js+++ b/tests/helpers.js@@ -5,12 +5,14 @@ import { expect } from '@playwright/test';const BASE_URL = process.env.LISTMONK_URL || 'http://localhost:9000';const MAILHOG_URL = process.env.MAILHOG_URL || 'http://localhost:8025';const ROOT = '..';+const CONFIG = process.env.LISTMONK_CONFIG || process.env.DEV_CONFIG || 'config.toml';const AUTH_FILE = new URL('.auth/admin.json', import.meta.url).pathname;const ENV = { ...process.env, LISTMONK_ADMIN_USER: 'admin', LISTMONK_ADMIN_PASSWORD: 'listmonk' };// A single SMTP server pointing at MailHog so tests capture (and can assert on) mail.const MAILHOG_SMTP = JSON.stringify([{- enabled: true, host: 'localhost', port: 1025, auth_protocol: 'none',+ enabled: true, host: process.env.MAILHOG_SMTP_HOST || 'localhost',+ port: Number(process.env.MAILHOG_SMTP_PORT || 1025), auth_protocol: 'none',username: '', password: '', hello_hostname: '', max_conns: 10,idle_timeout: '15s', wait_timeout: '5s', max_msg_retries: 2, msg_retry_delay: '10ms',tls_type: 'none', tls_skip_verify: false, email_headers: [], from_addresses: [],@@ -60,13 +62,20 @@ export async function getMail() {// Run a SQL statement against the listmonk DB. Credentials come from the// LISTMONK_db__* env, falling back to config.toml defaults.function psql(sql) {- execFileSync('psql', [+ const args = ['-h', process.env.LISTMONK_db__host || 'localhost','-p', process.env.LISTMONK_db__port || '5432','-U', process.env.LISTMONK_db__user || 'listmonk','-d', process.env.LISTMONK_db__database || 'listmonk',- '-c', sql,- ], { env: { ...process.env, PGPASSWORD: process.env.LISTMONK_db__password || 'listmonk' }, stdio: 'ignore' });+ '-v', 'ON_ERROR_STOP=1', '-c', sql,+ ];+ const env = { ...process.env, PGPASSWORD: process.env.LISTMONK_db__password || 'listmonk' };+ const options = { env, stdio: ['ignore', 'ignore', 'inherit'] };+ if (process.env.PSQL_CONTAINER) {+ execFileSync('docker', ['exec', '-e', 'PGPASSWORD', process.env.PSQL_CONTAINER, 'psql', ...args], options);+ } else {+ execFileSync('psql', args, options);+ }}// Point listmonk's SMTP config at MailHog on the freshly installed DB.@@ -92,12 +101,12 @@ function configureBounces() {// reinstalled under a running server, so the process is killed and relaunched.export async function resetDB(browser, { bounces = false } = {}) {try { execSync('pkill -9 listmonk', { stdio: 'ignore' }); } catch { /* none running */ }- execSync('./listmonk --install --yes', { cwd: ROOT, env: ENV, stdio: 'ignore' });+ execFileSync('./listmonk', ['--config', CONFIG, '--install', '--yes'], { cwd: ROOT, env: ENV, stdio: 'inherit' });configureSMTP();if (bounces) {configureBounces();}- spawn('./listmonk', ['--static-dir', 'static'], {+ spawn('./listmonk', ['--config', CONFIG, '--static-dir', 'static'], {cwd: ROOT, env: ENV, detached: true, stdio: 'ignore',}).unref();@@ -124,9 +133,9 @@ export async function resetDBBlank() {delete env.LISTMONK_ADMIN_USER;delete env.LISTMONK_ADMIN_PASSWORD;- execSync('./listmonk --install --yes', { cwd: ROOT, env, stdio: 'ignore' });+ execFileSync('./listmonk', ['--config', CONFIG, '--install', '--yes'], { cwd: ROOT, env, stdio: 'inherit' });configureSMTP();- spawn('./listmonk', ['--static-dir', 'static'], {+ spawn('./listmonk', ['--config', CONFIG, '--static-dir', 'static'], {cwd: ROOT, env, detached: true, stdio: 'ignore',}).unref();diff --git a/tests/playwright.config.js b/tests/playwright.config.jsindex 5298e079..cf6f9c21 100644--- a/tests/playwright.config.js+++ b/tests/playwright.config.js@@ -1,4 +1,5 @@import { defineConfig, devices } from '@playwright/test';+import { launchOptions } from './browser.js';// listmonk root address (see ../config.toml).const BASE_URL = process.env.LISTMONK_URL || 'http://localhost:9000';@@ -17,15 +18,17 @@ export default defineConfig({webServer: [// Run mailhog local SMTP test server.- {- command: 'pkill -9 mailhog; mailhog',+ ...(process.env.MAILHOG_EXTERNAL === '1' ? [] : [{+ command: 'pkill -9 -- "${MAILHOG:-mailhog}"; "${MAILHOG:-mailhog}"',url: MAILHOG_URL,reuseExistingServer: false,- },+ }]),// Create a fresh listmonk DB instance for every test run.{- command: 'pkill -9 listmonk; ./listmonk --install --yes && ./listmonk --static-dir static',+ command: 'pkill -9 listmonk; ./listmonk --config "${LISTMONK_CONFIG:-${DEV_CONFIG:-config.toml}}" --install --yes && ./listmonk --config "${LISTMONK_CONFIG:-${DEV_CONFIG:-config.toml}}" --static-dir static',cwd: '..',+ stdout: 'pipe',+ stderr: 'pipe',url: `${BASE_URL}/health`,reuseExistingServer: false,env: { LISTMONK_ADMIN_USER: 'admin', LISTMONK_ADMIN_PASSWORD: 'listmonk' },@@ -33,6 +36,7 @@ export default defineConfig({],use: {+ launchOptions,baseURL: BASE_URL,storageState: './.auth/admin.json',trace: 'retain-on-failure',