e2e: add console-error gate fixture with allowlist

This commit is contained in:
2026-04-25 02:21:03 +03:00
parent de5decce03
commit d458664b55
2 changed files with 73 additions and 0 deletions
@@ -0,0 +1,3 @@
{
"patterns": []
}
+70
View File
@@ -0,0 +1,70 @@
import { test as base, expect } from "@playwright/test";
import fs from "node:fs";
import path from "node:path";
interface AllowlistEntry {
pattern: string;
reason: string;
}
interface Allowlist {
patterns: AllowlistEntry[];
}
const ALLOWLIST_PATH = path.join(__dirname, "console-allowlist.json");
function loadAllowlist(): RegExp[] {
const raw = fs.readFileSync(ALLOWLIST_PATH, "utf8");
const parsed: Allowlist = JSON.parse(raw);
for (const entry of parsed.patterns) {
if (!entry.reason || entry.reason.trim() === "") {
throw new Error(
`console-allowlist.json: pattern ${JSON.stringify(entry.pattern)} has no reason`
);
}
}
return parsed.patterns.map((e) => new RegExp(e.pattern));
}
const allowlist = loadAllowlist();
function isAllowed(message: string): boolean {
return allowlist.some((re) => re.test(message));
}
interface ConsoleGateFixtures {
consoleMessages: string[];
}
export const test = base.extend<ConsoleGateFixtures>({
consoleMessages: async ({ page }, use, testInfo) => {
const messages: string[] = [];
page.on("console", (msg) => {
const type = msg.type();
if (type !== "error" && type !== "warning") return;
const text = `[${type}] ${msg.text()}`;
if (isAllowed(text)) return;
messages.push(text);
});
page.on("pageerror", (err) => {
const text = `[pageerror] ${err.message}`;
if (!isAllowed(text)) messages.push(text);
});
await use(messages);
if (messages.length > 0) {
testInfo.attachments.push({
name: "console-violations.txt",
contentType: "text/plain",
body: Buffer.from(messages.join("\n"), "utf8"),
});
throw new Error(
`Console gate: ${messages.length} disallowed message(s):\n` +
messages.map((m) => ` ${m}`).join("\n")
);
}
},
});
export { expect };