2026-09-15 15:56:23 +00:00
|
|
|
import {inspect} from "node:util"
|
2026-09-16 06:48:40 +00:00
|
|
|
import type {BrowserContext, ConsoleMessage} from "@playwright/test"
|
2026-09-15 15:56:23 +00:00
|
|
|
|
2026-09-24 18:23:21 +00:00
|
|
|
// A CSP refusal reaches the console and nothing else, so a policy that has rotted is invisible (#535).
|
2026-09-15 15:56:23 +00:00
|
|
|
const isRefusal = (text: string) => text.includes("Content Security Policy")
|
|
|
|
|
|
2026-09-24 18:23:21 +00:00
|
|
|
// Recreating the container between tests aborts route chunks in flight, until #529 stops the churn.
|
2026-09-15 15:56:23 +00:00
|
|
|
const isChunkLoss = (text: string) => text.includes("Failed to fetch dynamically imported module")
|
|
|
|
|
|
2026-09-24 18:23:21 +00:00
|
|
|
// Chrome carries a failed request's url nowhere but the message's location.
|
2026-09-16 06:48:40 +00:00
|
|
|
const locate = (message: ConsoleMessage) => {
|
|
|
|
|
const text = message.text()
|
|
|
|
|
const {url} = message.location()
|
|
|
|
|
|
|
|
|
|
return url && !text.includes(url) ? `${text} ${url}` : text
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 18:23:21 +00:00
|
|
|
// A page that throws something other than an Error leaves playwright neither message nor stack.
|
2026-09-15 15:56:23 +00:00
|
|
|
const describe = (error: Error) => error.stack || error.message || inspect(error)
|
|
|
|
|
|
|
|
|
|
export type FaultWatch = {
|
|
|
|
|
// Everything either side said, for the account attached to a failing test.
|
|
|
|
|
log: string[]
|
|
|
|
|
// The subset of it that means the app broke rather than the box being noisy.
|
|
|
|
|
found: string[]
|
|
|
|
|
observe(context: BrowserContext, who: string): void
|
2026-09-24 18:23:21 +00:00
|
|
|
// Throws on an uncaught exception or code the browser refused to run, not on a failed request.
|
2026-09-15 15:56:23 +00:00
|
|
|
assertNone(): void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const watchFaults = (): FaultWatch => {
|
|
|
|
|
const log: string[] = []
|
|
|
|
|
const found: string[] = []
|
|
|
|
|
|
|
|
|
|
const record = (line: string, fault: boolean) => {
|
|
|
|
|
log.push(line)
|
|
|
|
|
|
2026-09-21 16:48:35 +00:00
|
|
|
if (fault) {
|
|
|
|
|
found.push(line)
|
|
|
|
|
}
|
2026-09-15 15:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
log,
|
|
|
|
|
found,
|
|
|
|
|
observe(context, who) {
|
|
|
|
|
context.on("console", message => {
|
|
|
|
|
const type = message.type()
|
2026-09-16 06:48:40 +00:00
|
|
|
const text = locate(message)
|
2026-09-15 15:56:23 +00:00
|
|
|
|
|
|
|
|
if (["error", "warning"].includes(type)) {
|
|
|
|
|
record(`[${who}] ${type}: ${text}`, type === "error" && isRefusal(text))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
context.on("weberror", error => {
|
|
|
|
|
const text = describe(error.error())
|
|
|
|
|
|
|
|
|
|
record(`[${who}] uncaught: ${text}`, !isChunkLoss(text))
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
assertNone() {
|
|
|
|
|
if (found.length > 0) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
[
|
|
|
|
|
"The app broke while the test ran, so nothing it asserted means anything:",
|
|
|
|
|
...found.map(fault => ` ${fault}`),
|
|
|
|
|
].join("\n"),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|