1- import { GlobalWindow } from "happy-dom" ;
2-
3- const win = new GlobalWindow ( ) ;
4- // @ts -expect-error -- Patching global for test environment
5- global . window = win ;
6- // @ts -expect-error -- Patching global for test environment
7- global . document = win . document ;
8- // @ts -expect-error -- Patching global for test environment
9- global . navigator = win . navigator ;
10-
11- import { describe , it , expect , beforeEach , afterEach , mock , type Mock } from "bun:test" ;
1+ import { describe , it , expect , beforeEach , afterEach , mock , type Mock , beforeAll , afterAll } from "bun:test" ;
122import { renderHook , waitFor } from "@testing-library/react" ;
133import { useAvailableScripts } from "./useAvailableScripts" ;
4+ import { GlobalWindow } from "happy-dom" ;
145
156// Define types for our mock
167type ListScriptsMock = Mock < ( ) => Promise < { success : boolean ; data ?: unknown [ ] ; error ?: string } > > ;
178
189describe ( "useAvailableScripts" , ( ) => {
1910 const mockListScripts = mock ( ) as unknown as ListScriptsMock ;
11+ let originalWindow : unknown ;
12+ let originalDocument : unknown ;
13+ let originalNavigator : unknown ;
14+
15+ beforeAll ( ( ) => {
16+ // Save originals
17+ originalWindow = global . window ;
18+ originalDocument = global . document ;
19+ originalNavigator = global . navigator ;
20+
21+ // Setup happy-dom
22+ const win = new GlobalWindow ( ) ;
23+ // @ts -expect-error -- Patching global for test environment
24+ global . window = win ;
25+ // @ts -expect-error -- Patching global for test environment
26+ global . document = win . document ;
27+ // @ts -expect-error -- Patching global for test environment
28+ global . navigator = win . navigator ;
29+ } ) ;
30+
31+ afterAll ( ( ) => {
32+ // Restore originals
33+ // @ts -expect-error -- Restoring globals
34+ global . window = originalWindow ;
35+ // @ts -expect-error -- Restoring globals
36+ global . document = originalDocument ;
37+ // @ts -expect-error -- Restoring globals
38+ global . navigator = originalNavigator ;
39+ } ) ;
2040
2141 beforeEach ( ( ) => {
2242 // Mock window.api attached to the global window
@@ -31,7 +51,10 @@ describe("useAvailableScripts", () => {
3151 afterEach ( ( ) => {
3252 mock . restore ( ) ;
3353 // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
34- delete ( global . window as any ) . api ;
54+ if ( global . window ) {
55+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
56+ delete ( global . window as any ) . api ;
57+ }
3558 } ) ;
3659
3760 it ( "should fetch and return executable scripts" , async ( ) => {
0 commit comments