Skip to content

Commit 6e6ee10

Browse files
authoredDec 5, 2023
fix(vitest): support new Request('/api') in happy-dom (#4671)
1 parent 748205d commit 6e6ee10

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed
 

Diff for: ‎packages/vitest/src/integrations/env/happy-dom.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ export default <Environment>({
4747
},
4848
})
4949

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

5256
return {
5357
teardown(global) {

Diff for: ‎packages/vitest/src/integrations/env/utils.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ const skipKeys = [
77
'parent',
88
]
99

10-
export function getWindowKeys(global: any, win: any) {
11-
const keys = new Set(KEYS.concat(Object.getOwnPropertyNames(win))
10+
export function getWindowKeys(global: any, win: any, additionalKeys: string[] = []) {
11+
const keysArray = [...additionalKeys, ...KEYS]
12+
const keys = new Set(keysArray.concat(Object.getOwnPropertyNames(win))
1213
.filter((k) => {
1314
if (skipKeys.includes(k))
1415
return false
1516
if (k in global)
16-
return KEYS.includes(k)
17+
return keysArray.includes(k)
1718

1819
return true
1920
}))
@@ -31,11 +32,13 @@ interface PopulateOptions {
3132
// has a priority for getting implementation from symbols
3233
// (global doesn't have these symbols, but window - does)
3334
bindFunctions?: boolean
35+
36+
additionalKeys?: string[]
3437
}
3538

3639
export function populateGlobal(global: any, win: any, options: PopulateOptions = {}) {
3740
const { bindFunctions = false } = options
38-
const keys = getWindowKeys(global, win)
41+
const keys = getWindowKeys(global, win, options.additionalKeys)
3942

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

Diff for: ‎test/core/test/environments/happy-dom.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ test('atob and btoa are available', () => {
2020
expect(atob('aGVsbG8gd29ybGQ=')).toBe('hello world')
2121
expect(btoa('hello world')).toBe('aGVsbG8gd29ybGQ=')
2222
})
23+
24+
test('request doesn\'t fail when using absolute url because it supports it', () => {
25+
expect(() => {
26+
const _r = new Request('/api', { method: 'GET' })
27+
}).not.toThrow()
28+
})

Diff for: ‎test/core/test/environments/jsdom.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,9 @@ test('toContain correctly handles DOM nodes', () => {
9999
`)
100100
}
101101
})
102+
103+
test('request doesn\'t support absolute URL because jsdom doesn\'t provide compatible Request so Vitest is using Node.js Request', () => {
104+
expect(() => {
105+
const _r = new Request('/api', { method: 'GET' })
106+
}).toThrow(/Failed to parse URL/)
107+
})

0 commit comments

Comments
 (0)
Please sign in to comment.