Skip to content

Commit

Permalink
fix(vitest): support new Request('/api') in happy-dom (#4671)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Dec 5, 2023
1 parent 748205d commit 6e6ee10
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
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/)
})

0 comments on commit 6e6ee10

Please sign in to comment.