From 614d3f34c687221b7bab081b69a68cf815d6355f Mon Sep 17 00:00:00 2001 From: Raul de Melo Date: Wed, 9 Aug 2023 08:48:10 +0200 Subject: [PATCH 1/2] fix: pass environmentOptions to happy-dom integration --- packages/vitest/src/integrations/env/happy-dom.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/vitest/src/integrations/env/happy-dom.ts b/packages/vitest/src/integrations/env/happy-dom.ts index 3df3b2bd3b5b..9c4fecc40b20 100644 --- a/packages/vitest/src/integrations/env/happy-dom.ts +++ b/packages/vitest/src/integrations/env/happy-dom.ts @@ -5,9 +5,12 @@ import { populateGlobal } from './utils' export default ({ name: 'happy-dom', transformMode: 'web', - async setupVM() { + async setupVM({ 'happy-dom': happyDom = {} }) { const { Window } = await importModule('happy-dom') as typeof import('happy-dom') - const win = new Window() as any + const win = new Window({ + ...happyDom, + url: happyDom.url || 'http://localhost:3000', + }) as any // TODO: browser doesn't expose Buffer, but a lot of dependencies use it win.Buffer = Buffer @@ -26,11 +29,14 @@ export default ({ }, } }, - async setup(global) { + async setup(global, { 'happy-dom': happyDom = {} }) { // happy-dom v3 introduced a breaking change to Window, but // provides GlobalWindow as a way to use previous behaviour const { Window, GlobalWindow } = await importModule('happy-dom') as typeof import('happy-dom') - const win = new (GlobalWindow || Window)() + const win = new (GlobalWindow || Window)({ + ...happyDom, + url: happyDom.url || 'http://localhost:3000', + }) const { keys, originals } = populateGlobal(global, win, { bindFunctions: true }) From 54866e16a5ebe03f9767f5da25779b506f55753f Mon Sep 17 00:00:00 2001 From: Raul Melo Date: Mon, 14 Aug 2023 08:26:31 +0200 Subject: [PATCH 2/2] add unit test for happy-dom --- test/core/test/happy-dom.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/core/test/happy-dom.test.ts b/test/core/test/happy-dom.test.ts index d234ec8ae016..06641fceb403 100644 --- a/test/core/test/happy-dom.test.ts +++ b/test/core/test/happy-dom.test.ts @@ -2,6 +2,7 @@ /** * @vitest-environment happy-dom + * @vitest-environment-options { "settings": { "disableCSSFileLoading": true }, "width": 1920 } */ /* eslint-disable vars-on-top */ @@ -11,8 +12,19 @@ import { expect, it, vi } from 'vitest' declare global { // eslint-disable-next-line no-var var __property_dom: unknown + // eslint-disable-next-line no-var + var happyDOM: unknown } +it('defaults URL to localhost:3000', () => { + expect(location.href).toBe('http://localhost:3000/') +}) + +it('accepts custom environment options', () => { + // default is false + expect((window.happyDOM as any).settings.disableCSSFileLoading).toBe(true) +}) + it('defined on self/window are defined on global', () => { expect(self).toBeDefined() expect(window).toBeDefined()