Files
FitOS/web/test/e2e/fixtures.ts

78 lines
2.1 KiB
TypeScript

import { test as base, expect, type Page } from '@playwright/test';
export type { Page };
export interface ConsoleIssue {
kind: 'console.error' | 'pageerror';
text: string;
}
interface GuardState {
issues: ConsoleIssue[];
allowed: RegExp[];
}
const guards = new WeakMap<Page, GuardState>();
/**
* Strona web nie wykonuje żadnych żądań — brak domyślnie ignorowanych błędów.
* Jakikolwiek console.error/pageerror = FAIL.
*/
const DEFAULT_ALLOWED: RegExp[] = [];
/**
* Test z automatycznym guardem błędów konsoli (kopia wzorca z app, bez mockApi).
*
* Każdy test importujący `test` z tego pliku FAILUJE, jeśli na stronie pojawi się:
* - console.error (błąd z konsoli przeglądarki)
* - nieobsłużony wyjątek (pageerror)
*
* Assert dzieje się automatycznie w afterEach — nie trzeba nic deklarować w teście.
*/
export const test = base;
test.beforeEach(({ page }) => {
const state: GuardState = { issues: [], allowed: [...DEFAULT_ALLOWED] };
guards.set(page, state);
page.on('console', (msg) => {
if (msg.type() !== 'error') return;
const text = msg.text();
if (state.allowed.some((re) => re.test(text))) return;
state.issues.push({ kind: 'console.error', text });
});
page.on('pageerror', (err) => {
const text = `${err.message}${err.stack ? `\n${err.stack}` : ''}`;
if (state.allowed.some((re) => re.test(err.message))) return;
state.issues.push({ kind: 'pageerror', text });
});
});
test.afterEach(({ page }, testInfo) => {
const state = guards.get(page);
if (!state || state.issues.length === 0) return;
testInfo.annotations.push({
type: 'console-errors',
description: state.issues.map((i) => `[${i.kind}] ${i.text}`).join('\n'),
});
expect(
state.issues,
`Wykryto błędy konsoli / nieobsłużone wyjątki:\n${state.issues
.map((i) => ` [${i.kind}] ${i.text}`)
.join('\n')}`,
).toEqual([]);
});
/**
* Dozwolone ignorowanie błędów konsoli pasujących do wzorca (wyjątki).
*/
export function allowConsoleError(page: Page, pattern: RegExp) {
const state = guards.get(page);
if (state) state.allowed.push(pattern);
}
export { expect };