Files
Mario Heiderich 5e23a03688 chore(test): migrate from Karma to Playwright for browser tests
Karma has been deprecated since April 2023 and has been blocking
Dependabot updates across the dev-dependency tree. This migration
replaces it with Playwright while preserving all existing test logic.

Changes:
- Replace Karma + karma-* plugin ecosystem with @playwright/test
- Add tiny zero-dep static file server for the Playwright webServer
- Add HTML runner pages for dist/purify.js and dist/purify.min.js that
  load QUnit, jQuery, DOMPurify, test-suite.js, and an ES module entry
- Add Playwright spec that drives each runner, waits for QUnit.done,
  and surfaces per-assertion failure detail on error
- Drop rollup-plugin-includepaths (was only aliasing 'purify' for Karma)
- Drop minimist (was only used by the old custom-launchers config)
- Update build-and-test.yml to install Playwright browsers and upload
  playwright-report/ artifact on failure

Unchanged:
- test/test-suite.js (2398 lines of QUnit assertions)
- test/bootstrap-test-suite.js
- test/fixtures/expect.mjs
- test/config/setup.js
- test/jsdom-node-runner.js, test/jsdom-node.js
- test/fuzz/**
- rollup.config.js, src/**, scripts/**

Browser coverage: chromium, firefox, and webkit run locally and on
non-BrowserStack CI (3 rendering engines: Blink, Gecko, WebKit).
The 28-browser BrowserStack matrix is temporarily disabled and will
be restored in a follow-up PR via browserstack-node-sdk.

Test counts verified identical pre/post migration:
911 QUnit tests × 2 suites, all passing.
2026-04-16 12:46:30 +02:00

92 lines
3.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Playwright test: loads each HTML runner, waits for QUnit to finish,
* surfaces failure details, and asserts zero failures.
*
* The actual assertion logic lives in /test/test-suite.js (unchanged from the
* Karma era). This file only orchestrates the browser and reports results.
*/
'use strict';
const { test, expect } = require('@playwright/test');
const suites = [
{ label: 'purify.js', path: '/test/browser/index.html' },
{ label: 'purify.min.js', path: '/test/browser/index.min.html' },
];
for (const suite of suites) {
test(`${suite.label} — QUnit suite passes`, async ({ page }) => {
const pageErrors = [];
page.on('pageerror', (err) => {
pageErrors.push(err.stack || err.message);
});
const consoleErrors = [];
page.on('console', (msg) => {
if (msg.type() === 'error') consoleErrors.push(msg.text());
});
await page.goto(suite.path);
/* Wait for QUnit to finish (entry.js flips window.__qunitDone__).
* Playwright's waitForFunction polls; 100 ms is a reasonable cadence for
* a test suite that takes ~5–30 s in practice. */
await page.waitForFunction(() => window.__qunitDone__ === true, null, {
timeout: 90_000,
polling: 100,
});
const { result, failures } = await page.evaluate(() => ({
result: window.__qunitResult__,
failures: window.__qunitFailures__,
}));
/* If tests failed, print per-assertion detail to the Playwright output so
* CI logs surface the root cause rather than just "N tests failed". */
if (failures.length > 0) {
const lines = ['', `${failures.length} QUnit test(s) reported failures:`];
for (const f of failures) {
lines.push(` ${f.module} > ${f.name} (${f.failed}/${f.total})`);
for (const a of f.assertions) {
lines.push(` - ${a.message || '(no message)'}`);
lines.push(` expected: ${JSON.stringify(a.expected)}`);
lines.push(` actual: ${JSON.stringify(a.actual)}`);
}
}
// eslint-disable-next-line no-console
console.error(lines.join('\n'));
}
expect(
pageErrors,
`uncaught page error(s):\n${pageErrors.join('\n---\n')}`
).toEqual([]);
expect(
result.failed,
`${result.failed} of ${result.total} QUnit assertions failed`
).toBe(0);
/* Sanity check — if for some reason tests didn't actually run, fail loudly
* rather than pass silently. */
expect(result.total, 'QUnit reported zero total tests').toBeGreaterThan(0);
/* Record a light-touch summary line in the pass case. */
// eslint-disable-next-line no-console
console.log(
` ${suite.label}: ${result.passed}/${result.total} passed in ${result.runtime} ms`
);
/* Console errors from the browser aren't necessarily test failures
* (some tests intentionally provoke errors), so we don't assert on them.
* But if a fail occurs AND there were console errors, include them in the
* diagnostic output. */
if (failures.length > 0 && consoleErrors.length > 0) {
// eslint-disable-next-line no-console
console.error(
`browser console errors during failing run:\n ${consoleErrors.join('\n ')}`
);
}
});
}