super-productivity/e2e/global-setup.ts
Johannes Millan 77f83c5687
test(e2e): harden failure signals and provider gates (#7753)
* test: harden e2e failure signals

Fail otherwise-passing E2E tests on browser runtime errors, keep Playwright retries disabled, preserve Docker E2E exit codes, and make plugin/WebDAV setup failures hard failures instead of logged or skipped conditions.

* test: harden provider e2e runners

Make WebDAV and SuperSync runner scripts require provider readiness, preserve cleanup and argument forwarding, and fail manual sync clients on uncaught page errors.

* ci: require providers for scheduled e2e

Set required-provider flags in scheduled WebDAV and SuperSync jobs, and remove the duplicate provider runner scripts while keeping local npm aliases inline.

* test: catch e2e teardown pageerrors and tighten fixture

- closeClient now asserts runtime errors AFTER context.close() so
  pageerrors emitted during teardown (Angular destroy hooks, late RxJS
  errors) are captured instead of dropped. Matches the pattern in
  guardContextCloseWithRuntimeErrorCheck.
- test.fixture.ts isolatedContext now spreads Playwright's merged
  contextOptions instead of destructuring 23 fields by hand. Future
  option additions propagate automatically; the page fixture uses the
  shared attachPageErrorCollector and only fails on pageerror (not
  console.error, which is too noisy). Guards against a configured 0
  timeout being treated as undefined.
- plugin-loading.spec.ts second test now hard-asserts that the plugin
  menu entry reappears after re-enable, matching the first test instead
  of silently logging when not visible.

* test(sync): stabilize ImmediateUploadService spec

Two complementary fixes for flaky failures observed under full-suite
random-order runs where the upload pipeline silently never fires:

- Pin navigator.onLine = true in beforeEach (restored in afterEach).
  isOnline() inside _canUpload reads navigator.onLine directly. The
  keyboard-layout spec replaces the whole navigator and the is-online
  spec spies on it; if order or restoration ever drifts, every "should
  fire upload" test fails trivially while the "should NOT" tests pass.
- Replace tick(2100) with tick(2000); flush(). The await chain inside
  withSession() (provider.isReady, withSession entry, uploadPendingOps,
  optional LWW re-upload) requires more microtask drain than tick's
  fixed-time window reliably provides under load. flush() drains the
  pipeline regardless.

* test(e2e): guard skipOnboarding init script against data: frames

The new page-error collector started failing plugin specs because
addInitScript runs in every frame — including the empty data:text/html
iframe that plugin-index swaps in on destroy — and localStorage access
in a data: URL throws SecurityError. Wrap the four setItem calls in
try/catch so the helper noops in storage-less frames.
2026-05-23 20:33:04 +02:00

94 lines
3.6 KiB
TypeScript

import { FullConfig } from '@playwright/test';
import { execSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { isServerHealthy } from './utils/supersync-helpers';
/**
* Warm up the dev server by fetching the app once before any tests start.
* This ensures Angular compilation is complete and cached, so all workers
* get instant responses instead of competing for the first compilation.
*/
const warmUpDevServer = async (baseURL: string): Promise<void> => {
console.log(`Warming up dev server at ${baseURL}...`);
const maxRetries = 3;
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
const response = await fetch(baseURL, { signal: AbortSignal.timeout(30000) });
if (response.ok) {
// Read the full body to ensure the server finishes compilation
await response.text();
console.log('Dev server warm-up complete');
return;
}
} catch {
if (attempt < maxRetries - 1) {
console.log(`Warm-up attempt ${attempt + 1} failed, retrying...`);
await new Promise((r) => setTimeout(r, 2000));
}
}
}
console.warn('Dev server warm-up failed — tests may be slow on first run');
};
const globalSetup = async (config: FullConfig): Promise<void> => {
// Set test environment variables
process.env.TZ = 'Europe/Berlin';
process.env.NODE_ENV = 'test';
console.log(`Running tests with ${config.workers} workers`);
// Build plugins before starting tests (skip if already built)
// Check both the dev path (src/assets/) and the CI pre-built path (.tmp/angular-dist/)
const pluginManifestPath = path.join(
process.cwd(),
'src/assets/bundled-plugins/api-test-plugin/manifest.json',
);
const ciBuildManifestPath = path.join(
process.cwd(),
'.tmp/angular-dist/browser/assets/bundled-plugins/api-test-plugin/manifest.json',
);
if (fs.existsSync(pluginManifestPath) || fs.existsSync(ciBuildManifestPath)) {
console.log('Plugins already built, skipping...');
} else {
console.log('Building bundled plugins...');
try {
execSync('npm run plugins:build', {
stdio: 'inherit',
cwd: process.cwd(),
});
console.log('Bundled plugins built successfully');
} catch (error) {
console.error('Failed to build plugins:', error);
throw error; // Fail fast if plugin build fails
}
}
// Warm up the dev server to pre-compile Angular bundles before workers start
const baseURL =
process.env.E2E_BASE_URL ||
config.projects[0]?.use?.baseURL ||
'http://localhost:4242';
await warmUpDevServer(baseURL);
// Check SuperSync server health ONCE here, before workers start.
// Without this, each worker checks independently on startup — with many workers
// running simultaneously, the concurrent health-check requests can overload the
// supersync server and cause false negatives, making workers skip all their tests.
// By storing the result in an env var set before workers are forked, every worker
// reads the cached result instantly instead of making HTTP requests.
const healthy = await isServerHealthy().catch(() => false);
process.env.SUPERSYNC_SERVER_HEALTHY = healthy ? 'true' : 'false';
if (healthy) {
console.log('SuperSync server healthy — supersync tests will run');
} else if (process.env.E2E_REQUIRE_SUPERSYNC === 'true') {
throw new Error(
'SuperSync server is required for this run but is not test-ready. ' +
'Use scripts/wait-for-supersync.sh before running required SuperSync E2E.',
);
} else {
console.log('SuperSync server not available — supersync tests will be skipped');
}
};
export default globalSetup;