Skip to content

Commit 91719bb

Browse files
authoredJan 11, 2024
fix(happy-dom): window.close() for environment teardown (#4931)
1 parent 9ec3f74 commit 91719bb

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed
 

‎packages/vitest/src/integrations/env/happy-dom.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import type { Environment } from '../../types'
22
import { populateGlobal } from './utils'
33

4+
async function teardownWindow(win: { happyDOM: { abort?: () => Promise<void>; cancelAsync: () => void }; close?: () => void }) {
5+
if (win.close && win.happyDOM.abort) {
6+
await win.happyDOM.abort()
7+
win.close()
8+
}
9+
else {
10+
win.happyDOM.cancelAsync()
11+
}
12+
}
13+
414
export default <Environment>({
515
name: 'happy-dom',
616
transformMode: 'web',
@@ -28,7 +38,7 @@ export default <Environment>({
2838
return win
2939
},
3040
async teardown() {
31-
await win.happyDOM.cancelAsync()
41+
await teardownWindow(win)
3242
},
3343
}
3444
},
@@ -53,8 +63,8 @@ export default <Environment>({
5363
})
5464

5565
return {
56-
teardown(global) {
57-
win.happyDOM.cancelAsync()
66+
async teardown(global) {
67+
await teardownWindow(win)
5868
keys.forEach(key => delete global[key])
5969
originals.forEach((v, k) => global[k] = v)
6070
},

‎test/core/test/happy-dom-custom.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ it('custom URL is changed to my-website:5435', () => {
1818

1919
it('accepts custom environment options', () => {
2020
// default is false
21-
expect(window.happyDOM.settings.disableCSSFileLoading).toBe(true)
21+
expect(window.happyDOM?.settings.disableCSSFileLoading).toBe(true)
2222
})

‎test/core/test/happy-dom.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ it('defaults URL to localhost:3000', () => {
2020
})
2121

2222
it('disableCSSFileLoading is false by default because we didn\'t change options', () => {
23-
expect(window.happyDOM.settings.disableCSSFileLoading).toBe(false)
23+
expect(window.happyDOM?.settings.disableCSSFileLoading).toBe(false)
2424
})
2525

2626
it('defined on self/window are defined on global', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.