cloudflare/vinext
Publicmirrored from https://github.com/cloudflare/vinextAvailable
tests/e2e/fixtures.ts
67lines · modecode
| 1 | /** |
| 2 | * Shared Playwright fixtures for all e2e tests. |
| 3 | * |
| 4 | * This file provides a custom `test` fixture that automatically fails |
| 5 | * tests if any console errors occur during execution. This helps catch |
| 6 | * React hydration mismatches, runtime errors, and other bugs early. |
| 7 | */ |
| 8 | import { test as base, expect } from "@playwright/test"; |
| 9 | |
| 10 | /** |
| 11 | * Patterns to ignore in console error checking. |
| 12 | * Some errors are expected or come from third-party code. |
| 13 | */ |
| 14 | const IGNORED_ERROR_PATTERNS = [ |
| 15 | // Vite HMR connection messages (not actual errors) |
| 16 | /\[vite\] connected/, |
| 17 | // React's development-only "Invalid hook call" warning when testing hooks |
| 18 | // outside component context (expected in some test scenarios) |
| 19 | /Invalid hook call.*mismatching versions/, |
| 20 | ]; |
| 21 | |
| 22 | /** |
| 23 | * Check if an error message should be ignored. |
| 24 | */ |
| 25 | function shouldIgnoreError(message: string): boolean { |
| 26 | return IGNORED_ERROR_PATTERNS.some((pattern) => pattern.test(message)); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Extended test fixture that collects console errors and fails if any occur. |
| 31 | */ |
| 32 | export const test = base.extend<{ consoleErrors: string[] }>({ |
| 33 | consoleErrors: async ({ page }, use) => { |
| 34 | const errors: string[] = []; |
| 35 | |
| 36 | // Collect console errors |
| 37 | page.on("console", (msg) => { |
| 38 | if (msg.type() === "error") { |
| 39 | const text = msg.text(); |
| 40 | if (!shouldIgnoreError(text)) { |
| 41 | errors.push(text); |
| 42 | } |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | // Collect uncaught page errors |
| 47 | page.on("pageerror", (error) => { |
| 48 | const text = error.message || String(error); |
| 49 | if (!shouldIgnoreError(text)) { |
| 50 | errors.push(`[PageError] ${text}`); |
| 51 | } |
| 52 | }); |
| 53 | |
| 54 | // Run the test |
| 55 | await use(errors); |
| 56 | |
| 57 | // After the test, fail if there were any console errors |
| 58 | if (errors.length > 0) { |
| 59 | const errorList = errors.map((e, i) => ` ${i + 1}. ${e}`).join("\n"); |
| 60 | throw new Error( |
| 61 | `Test failed due to ${errors.length} console error(s):\n${errorList}`, |
| 62 | ); |
| 63 | } |
| 64 | }, |
| 65 | }); |
| 66 | |
| 67 | export { expect }; |
| 68 | |