diff --git a/packages/vitest/src/integrations/env/happy-dom.ts b/packages/vitest/src/integrations/env/happy-dom.ts index fe7a0d360..0f662c28b 100644 --- a/packages/vitest/src/integrations/env/happy-dom.ts +++ b/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; cancelAsync: () => void }; close?: () => void }) { + if (win.close && win.happyDOM.abort) { + await win.happyDOM.abort() + win.close() + } + else { + win.happyDOM.cancelAsync() + } +} + export default ({ name: 'happy-dom', transformMode: 'web', @@ -28,7 +38,7 @@ export default ({ return win }, async teardown() { - await win.happyDOM.cancelAsync() + await teardownWindow(win) }, } }, @@ -53,8 +63,8 @@ export default ({ }) 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) }, diff --git a/test/core/test/happy-dom-custom.test.ts b/test/core/test/happy-dom-custom.test.ts index 1bdb2be8f..4c1c05c9b 100644 --- a/test/core/test/happy-dom-custom.test.ts +++ b/test/core/test/happy-dom-custom.test.ts @@ -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) }) diff --git a/test/core/test/happy-dom.test.ts b/test/core/test/happy-dom.test.ts index f346db0ee..034a8c5f1 100644 --- a/test/core/test/happy-dom.test.ts +++ b/test/core/test/happy-dom.test.ts @@ -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', () => {