debian-image-builder-frontend/playwright/fixtures/cleanup.ts
Tom Koscielniak 0bddb80e94 tests/playwright: Add Hostname customization test
Adds a Hostname customization test that creates a BP, edits BP, exports and imports it back and verifies the BP content.
This test also servers as a template for other customization tests. Includes a refactor of existing helper functions.
2025-04-15 14:14:40 +02:00

45 lines
1.1 KiB
TypeScript

import { test as oldTest } from '@playwright/test';
type WithCleanup = {
cleanup: Cleanup;
};
export interface Cleanup {
add: (cleanupFn: () => Promise<unknown>) => symbol;
runAndAdd: (cleanupFn: () => Promise<unknown>) => Promise<symbol>;
remove: (key: symbol) => void;
}
export const test = oldTest.extend<WithCleanup>({
// eslint-disable-next-line no-empty-pattern
cleanup: async ({}, use) => {
const cleanupFns: Map<symbol, () => Promise<unknown>> = new Map();
// eslint-disable-next-line react-hooks/rules-of-hooks
await use({
add: (cleanupFn) => {
const key = Symbol();
cleanupFns.set(key, cleanupFn);
return key;
},
runAndAdd: async (cleanupFn) => {
await cleanupFn();
const key = Symbol();
cleanupFns.set(key, cleanupFn);
return key;
},
remove: (key) => {
cleanupFns.delete(key);
},
});
await test.step(
'Post-test cleanup',
async () => {
await Promise.all(Array.from(cleanupFns).map(([, fn]) => fn()));
},
{ box: true }
);
},
});