|
1 | | -import { test as base, expect } from '@playwright/test' |
| 1 | +import { test as base, expect } from "@playwright/test"; |
2 | 2 |
|
3 | 3 | export const test = base.extend({ |
4 | 4 | page: async ({ page }, use) => { |
5 | | - const errors: string[] = [] |
6 | | - let lastMe401At = 0 |
| 5 | + const errors: string[] = []; |
7 | 6 | // Log 4xx/5xx responses with URL and a short body snippet |
8 | | - page.on('response', async (resp) => { |
9 | | - const status = resp.status() |
| 7 | + page.on("response", async (resp) => { |
| 8 | + const status = resp.status(); |
10 | 9 | if (status >= 400) { |
11 | | - const req = resp.request() |
12 | | - const method = req.method() |
13 | | - const url = resp.url() |
14 | | - const debugAuth = (process.env.LOG_LEVEL || '').toLowerCase() === 'debug' |
15 | | - if (status === 401 && url.includes('/.gateway/api/me') && !debugAuth) { |
16 | | - return |
17 | | - } |
18 | | - let body = '' |
19 | | - try { body = await resp.text() } catch {} |
20 | | - const snippet = body ? body.slice(0, 200).replace(/\s+/g, ' ').trim() : '' |
21 | | - console.log(`[resp ${status}] ${method} ${url}${snippet ? ' body="' + snippet + '"' : ''}`) |
22 | | - if (status === 401 && url.includes('/.gateway/api/me')) { |
23 | | - lastMe401At = Date.now() |
| 10 | + const req = resp.request(); |
| 11 | + const method = req.method(); |
| 12 | + const url = resp.url(); |
| 13 | + const debugAuth = |
| 14 | + (process.env.LOG_LEVEL || "").toLowerCase() === "debug"; |
| 15 | + if (status === 401 && url.includes("/.gateway/api/me") && !debugAuth) { |
| 16 | + return; |
24 | 17 | } |
| 18 | + let body = ""; |
| 19 | + try { |
| 20 | + body = await resp.text(); |
| 21 | + } catch {} |
| 22 | + const snippet = body |
| 23 | + ? body.slice(0, 200).replace(/\s+/g, " ").trim() |
| 24 | + : ""; |
| 25 | + |
| 26 | + if (status === 401 && url.includes("/.gateway/api/me")) return; |
| 27 | + |
| 28 | + console.log( |
| 29 | + `[resp ${status}] ${method} ${url}${ |
| 30 | + snippet ? ' body="' + snippet + '"' : "" |
| 31 | + }` |
| 32 | + ); |
25 | 33 | } |
26 | | - }) |
27 | | - page.on('console', (msg) => { |
28 | | - const text = msg.text() |
29 | | - const url = page.url() |
| 34 | + }); |
| 35 | + page.on("console", (msg) => { |
| 36 | + const text = msg.text(); |
| 37 | + const url = page.url(); |
30 | 38 | // Suppress generic 401 console noise entirely (expected before login) |
31 | | - const isGeneric401 = /the server responded with a status of 401 \(Unauthorized\)\s*$/i.test(text) || /\b401\b/i.test(text) |
32 | | - if (isGeneric401) return |
| 39 | + const isGeneric401 = |
| 40 | + /the server responded with a status of 401 \(Unauthorized\)\s*$/i.test( |
| 41 | + text |
| 42 | + ) || /\b401\b/i.test(text); |
| 43 | + if (isGeneric401) return; |
| 44 | + if (msg.type() === "debug" && text.includes("[vite] connect")) return; |
| 45 | + if ( |
| 46 | + msg.type() === "info" && |
| 47 | + text.includes( |
| 48 | + "Download the React DevTools for a better development experience" |
| 49 | + ) |
| 50 | + ) |
| 51 | + return; |
| 52 | + |
33 | 53 | // Always print console logs for diagnosis (after suppression checks) |
34 | | - console.log('console:', msg.type(), text) |
35 | | - if (msg.type() === 'error') { |
| 54 | + console.log("console:", msg.type(), text); |
| 55 | + if (msg.type() === "error") { |
36 | 56 | // Ignore all 401/Unauthorized console errors (expected before login) |
37 | | - const is401 = /\b401\b/i.test(text) || /Unauthorized/i.test(text) |
38 | | - if (is401) return |
39 | | - errors.push(`console.${msg.type()}: ${text}`) |
| 57 | + const is401 = /\b401\b/i.test(text) || /Unauthorized/i.test(text); |
| 58 | + if (is401) return; |
| 59 | + errors.push(`console.${msg.type()}: ${text}`); |
40 | 60 | } |
41 | | - }) |
42 | | - page.on('pageerror', (err) => { |
43 | | - console.log('pageerror:', err) |
44 | | - errors.push(`pageerror: ${String(err)}`) |
45 | | - }) |
46 | | - await use(page) |
| 61 | + }); |
| 62 | + page.on("pageerror", (err) => { |
| 63 | + console.log("pageerror:", err); |
| 64 | + errors.push(`pageerror: ${String(err)}`); |
| 65 | + }); |
| 66 | + await use(page); |
47 | 67 | if (errors.length > 0) { |
48 | | - throw new Error('JS errors detected during test:\n' + errors.join('\n')) |
| 68 | + throw new Error("JS errors detected during test:\n" + errors.join("\n")); |
49 | 69 | } |
50 | 70 | }, |
51 | | -}) |
| 71 | +}); |
52 | 72 |
|
53 | | -export { expect } |
| 73 | +export { expect }; |
0 commit comments