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(vitest): support new Request('/api') in happy-dom #4671

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/vitest/src/integrations/env/happy-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export default <Environment>({
},
})

const { keys, originals } = populateGlobal(global, win, { bindFunctions: true })
const { keys, originals } = populateGlobal(global, win, {
bindFunctions: true,
// jsdom doesn't support Request and Response, but happy-dom does
additionalKeys: ['Request', 'Response'],
})

return {
teardown(global) {
Expand Down
11 changes: 7 additions & 4 deletions packages/vitest/src/integrations/env/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ const skipKeys = [
'parent',
]

export function getWindowKeys(global: any, win: any) {
const keys = new Set(KEYS.concat(Object.getOwnPropertyNames(win))
export function getWindowKeys(global: any, win: any, additionalKeys: string[] = []) {
const keysArray = [...additionalKeys, ...KEYS]
const keys = new Set(keysArray.concat(Object.getOwnPropertyNames(win))
.filter((k) => {
if (skipKeys.includes(k))
return false
if (k in global)
return KEYS.includes(k)
return keysArray.includes(k)

return true
}))
Expand All @@ -31,11 +32,13 @@ interface PopulateOptions {
// has a priority for getting implementation from symbols
// (global doesn't have these symbols, but window - does)
bindFunctions?: boolean

additionalKeys?: string[]
}

export function populateGlobal(global: any, win: any, options: PopulateOptions = {}) {
const { bindFunctions = false } = options
const keys = getWindowKeys(global, win)
const keys = getWindowKeys(global, win, options.additionalKeys)

const originals = new Map<string | symbol, any>()

Expand Down
6 changes: 6 additions & 0 deletions test/core/test/environments/happy-dom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ test('atob and btoa are available', () => {
expect(atob('aGVsbG8gd29ybGQ=')).toBe('hello world')
expect(btoa('hello world')).toBe('aGVsbG8gd29ybGQ=')
})

test('request doesn\'t fail when using absolute url because it supports it', () => {
expect(() => {
const _r = new Request('/api', { method: 'GET' })
}).not.toThrow()
})
6 changes: 6 additions & 0 deletions test/core/test/environments/jsdom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,9 @@ test('toContain correctly handles DOM nodes', () => {
`)
}
})

test('request doesn\'t support absolute URL because jsdom doesn\'t provide compatible Request so Vitest is using Node.js Request', () => {
expect(() => {
const _r = new Request('/api', { method: 'GET' })
}).toThrow(/Failed to parse URL/)
})
Loading