Skip to content

Commit a3bc35d

Browse files
committed
better har integration
1 parent 0785955 commit a3bc35d

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

browser-extension/tests/lib/enhancers/github.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { vi } from 'vitest'
22
import { EnhancerRegistry } from '../../../src/lib/registries'
3-
import { describe, expect, it } from '../../test-fixtures'
3+
import { describe, expect, usingHar } from '../../test-fixtures'
44

55
vi.stubGlobal('defineContentScript', vi.fn())
66
vi.mock('../../../src/overtype/overtype', () => {
@@ -23,10 +23,7 @@ vi.mock('../../../src/overtype/overtype', () => {
2323
})
2424

2525
describe('github', () => {
26-
it('should identify gh_pr textarea and create proper spot object', async ({ harDOM }) => {
27-
// Setup DOM from HAR snapshot
28-
await harDOM('gh_pr')
29-
26+
usingHar('gh_pr').it('should identify gh_pr textarea and create proper spot object', async () => {
3027
const enhancers = new EnhancerRegistry()
3128
const textareas = document.querySelectorAll('textarea')
3229

browser-extension/tests/test-fixtures.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,54 @@ export const it = test
5151

5252
// Re-export expect from vitest
5353
export { expect }
54+
55+
// Fluent interface for HAR-based tests
56+
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+
return {
94+
describe: (name: string, fn: () => void) => {
95+
return baseDescribe(name, fn)
96+
},
97+
98+
it: (name: string, fn: () => void | Promise<void>) => {
99+
return harTest(name, async () => {
100+
return await fn()
101+
})
102+
},
103+
}
104+
}

0 commit comments

Comments
 (0)