|
1 | 1 | import { describe as baseDescribe, test as baseTest, expect } from 'vitest' |
2 | | -import { PAGES } from './har-index' |
3 | | -import { |
4 | | - cleanupDOM, |
5 | | - createDOMFromHar, |
6 | | - loadHtmlFromHar, |
7 | | - setupDOMFromHar, |
8 | | - type TestDOMGlobals, |
9 | | -} from './test-utils' |
10 | | - |
11 | | -export interface TestFixtures { |
12 | | - harDOM: (key: keyof typeof PAGES) => Promise<TestDOMGlobals> |
13 | | -} |
14 | | - |
15 | | -export const test = baseTest.extend<TestFixtures>({ |
16 | | - // biome-ignore lint/correctness/noEmptyPattern: Required by Vitest fixture API |
17 | | - harDOM: async ({}, use) => { |
18 | | - let currentDOM: TestDOMGlobals | null = null |
19 | | - |
20 | | - const setupDOM = async (key: keyof typeof PAGES): Promise<TestDOMGlobals> => { |
21 | | - // Clean up any existing DOM |
22 | | - if (currentDOM) { |
23 | | - cleanupDOM() |
24 | | - } |
25 | | - |
26 | | - // Load HTML from HAR file |
27 | | - const html = await loadHtmlFromHar(key) |
28 | | - const url = PAGES[key] |
29 | | - |
30 | | - // Create and setup new DOM |
31 | | - const domGlobals = createDOMFromHar(html, url) |
32 | | - setupDOMFromHar(domGlobals) |
33 | | - |
34 | | - currentDOM = domGlobals |
35 | | - return domGlobals |
36 | | - } |
37 | | - |
38 | | - // Provide the setup function to the test |
39 | | - await use(setupDOM) |
40 | | - |
41 | | - // Cleanup after test completes |
42 | | - if (currentDOM) { |
43 | | - cleanupDOM() |
44 | | - currentDOM = null |
45 | | - } |
46 | | - }, |
47 | | -}) |
| 2 | +import type { PAGES } from './har-index' |
| 3 | +import { cleanupDOM, setupHarDOM } from './test-utils' |
48 | 4 |
|
49 | 5 | export const describe = baseDescribe |
50 | | -export const it = test |
51 | 6 |
|
52 | 7 | // Re-export expect from vitest |
53 | 8 | export { expect } |
54 | 9 |
|
55 | 10 | // Fluent interface for HAR-based tests |
56 | 11 | export function usingHar(harKey: keyof typeof PAGES) { |
57 | | - // Create a test with auto-setup fixture for this HAR |
58 | | - const harTest = baseTest.extend<TestFixtures & { _harAutoSetup: undefined }>({ |
59 | | - // Auto-setup fixture that runs the HAR setup automatically |
60 | | - _harAutoSetup: [ |
61 | | - async ({ harDOM }, use) => { |
62 | | - await harDOM(harKey) |
63 | | - await use(undefined) |
64 | | - }, |
65 | | - { auto: true }, |
66 | | - ], |
67 | | - // Keep the original harDOM fixture |
68 | | - // biome-ignore lint/correctness/noEmptyPattern: Required by Vitest fixture API |
69 | | - harDOM: async ({}, use) => { |
70 | | - let currentDOM: TestDOMGlobals | null = null |
71 | | - |
72 | | - const setupDOM = async (key: keyof typeof PAGES): Promise<TestDOMGlobals> => { |
73 | | - if (currentDOM) { |
74 | | - cleanupDOM() |
75 | | - } |
76 | | - const html = await loadHtmlFromHar(key) |
77 | | - const url = PAGES[key] |
78 | | - const domGlobals = createDOMFromHar(html, url) |
79 | | - setupDOMFromHar(domGlobals) |
80 | | - currentDOM = domGlobals |
81 | | - return domGlobals |
82 | | - } |
83 | | - |
84 | | - await use(setupDOM) |
85 | | - |
86 | | - if (currentDOM) { |
87 | | - cleanupDOM() |
88 | | - currentDOM = null |
89 | | - } |
90 | | - }, |
91 | | - }) |
92 | | - |
93 | 12 | return { |
94 | | - describe: (name: string, fn: () => void) => { |
95 | | - return baseDescribe(name, fn) |
96 | | - }, |
97 | | - |
98 | 13 | it: (name: string, fn: () => void | Promise<void>) => { |
99 | | - return harTest(name, async () => { |
100 | | - return await fn() |
| 14 | + return baseTest(name, async () => { |
| 15 | + // Setup HAR DOM before test |
| 16 | + await setupHarDOM(harKey) |
| 17 | + |
| 18 | + try { |
| 19 | + return await fn() |
| 20 | + } finally { |
| 21 | + // Cleanup after test |
| 22 | + cleanupDOM() |
| 23 | + } |
101 | 24 | }) |
102 | 25 | }, |
103 | 26 | } |
|
0 commit comments