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 }) 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()