Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(happy-dom): window.close() for environment teardown #4931

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/vitest/src/integrations/env/happy-dom.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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