Skip to content

Commit

Permalink
fix(happy-dom): window.close() for environment teardown (#4931)
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Jan 11, 2024
1 parent 9ec3f74 commit 91719bb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions packages/vitest/src/integrations/env/happy-dom.ts
@@ -1,6 +1,16 @@
import type { Environment } from '../../types'
import { populateGlobal } from './utils'

async function teardownWindow(win: { happyDOM: { abort?: () => Promise<void>; cancelAsync: () => void }; close?: () => void }) {
if (win.close && win.happyDOM.abort) {
await win.happyDOM.abort()
win.close()
}
else {
win.happyDOM.cancelAsync()
}
}

export default <Environment>({
name: 'happy-dom',
transformMode: 'web',
Expand Down Expand Up @@ -28,7 +38,7 @@ export default <Environment>({
return win
},
async teardown() {
await win.happyDOM.cancelAsync()
await teardownWindow(win)
},
}
},
Expand All @@ -53,8 +63,8 @@ export default <Environment>({
})

return {
teardown(global) {
win.happyDOM.cancelAsync()
async teardown(global) {
await teardownWindow(win)
keys.forEach(key => delete global[key])
originals.forEach((v, k) => global[k] = v)
},
Expand Down
2 changes: 1 addition & 1 deletion test/core/test/happy-dom-custom.test.ts
Expand Up @@ -18,5 +18,5 @@ it('custom URL is changed to my-website:5435', () => {

it('accepts custom environment options', () => {
// default is false
expect(window.happyDOM.settings.disableCSSFileLoading).toBe(true)
expect(window.happyDOM?.settings.disableCSSFileLoading).toBe(true)
})
2 changes: 1 addition & 1 deletion test/core/test/happy-dom.test.ts
Expand Up @@ -20,7 +20,7 @@ it('defaults URL to localhost:3000', () => {
})

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

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

0 comments on commit 91719bb

Please sign in to comment.