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: pass environmentOptions to happy-dom integration #3902

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/vitest/src/integrations/env/happy-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { populateGlobal } from './utils'
export default <Environment>({
name: 'happy-dom',
transformMode: 'web',
async setupVM() {
async setupVM({ 'happy-dom': happyDom = {} }) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think naming it happyDOM in the config is fine

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types can be found here:

     import IHappyDOMOptions from 'happy-dom/lib/window/IHappyDOMOptions.js';

The source code for them is here:
https://github.com/capricorn86/happy-dom/blob/master/packages/happy-dom/src/window/IHappyDOMOptions.ts

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
Expand All @@ -26,11 +29,14 @@ export default <Environment>({
},
}
},
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 })

Expand Down
12 changes: 12 additions & 0 deletions test/core/test/happy-dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* @vitest-environment happy-dom
* @vitest-environment-options { "settings": { "disableCSSFileLoading": true }, "width": 1920 }
*/

/* eslint-disable vars-on-top */
Expand All @@ -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', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same applies here.

I'd like to try passing and not passing URL, but no idea how to do that using JSDoc comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a separate test file

expect(location.href).toBe('http://localhost:3000/')
})

it('accepts custom environment options', () => {
// default is false
expect((window.happyDOM as any).settings.disableCSSFileLoading).toBe(true)
})
Comment on lines +23 to +26
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we leverage on passing envrionment options via JSDoc code block, I didn't find a way where I could test passing the option vs not passing.


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