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: [#3905] default url when using happy-dom as environment #3907

Closed
Closed
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
20 changes: 13 additions & 7 deletions packages/vitest/src/integrations/env/happy-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { importModule } from 'local-pkg'
import type { Environment } from '../../types'
import { populateGlobal } from './utils'

export default <Environment>({
export default <Environment>{
name: 'happy-dom',
transformMode: 'web',
async setupVM() {
const { Window } = await importModule('happy-dom') as typeof import('happy-dom')
const { Window } = (await importModule(
'happy-dom',
)) as typeof import('happy-dom')
const win = new Window() as any

// TODO: browser doesn't expose Buffer, but a lot of dependencies use it
Expand All @@ -29,17 +31,21 @@ export default <Environment>({
async setup(global) {
// 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 { Window, GlobalWindow } = (await importModule(
'happy-dom',
)) as typeof import('happy-dom')
const win = new (GlobalWindow || Window)({ url: 'http://localhost:3000' })

const { keys, originals } = populateGlobal(global, win, { bindFunctions: true })
const { keys, originals } = populateGlobal(global, win, {
bindFunctions: true,
})

return {
teardown(global) {
win.happyDOM.cancelAsync()
keys.forEach(key => delete global[key])
originals.forEach((v, k) => global[k] = v)
originals.forEach((v, k) => (global[k] = v))
},
}
},
})
}