Skip to content

Commit 0fb74bd

Browse files
authored
fix(happy-dom): support fetch globals (#8791)
1 parent 22f03aa commit 0fb74bd

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/vitest/src/integrations/env/happy-dom.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,18 @@ export default <Environment>{
6363

6464
const { keys, originals } = populateGlobal(global, win, {
6565
bindFunctions: true,
66-
// jsdom doesn't support Request and Response, but happy-dom does
67-
additionalKeys: ['Request', 'Response', 'MessagePort', 'fetch'],
66+
// jsdom doesn't support fetch API, but happy-dom does
67+
additionalKeys: [
68+
'Request',
69+
'Response',
70+
'MessagePort',
71+
'fetch',
72+
'Headers',
73+
'AbortController',
74+
'AbortSignal',
75+
'URL',
76+
'URLSearchParams',
77+
],
6878
})
6979

7080
return {

test/core/test/happy-dom.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@ it('globals are the same', () => {
120120
expect(Blob).toBe(globalThis.Blob)
121121
})
122122

123+
it('fetch globals work', () => {
124+
const file = new File([window.Buffer.from('<foo />')], 'a.xml')
125+
expect(() => URL.createObjectURL(file)).not.toThrow()
126+
127+
expect(new File([], 'test.txt')).toBeInstanceOf(Blob)
128+
129+
const response = new Response('', {
130+
headers: new Headers({
131+
'Content-Type': 'application/json',
132+
}),
133+
})
134+
expect(response.headers.get('content-type')).toBe('application/json')
135+
})
136+
123137
it.skipIf(import.meta.env.VITEST_VM_POOL)('default view references global object', () => {
124138
expect(document.defaultView).toBe(window)
125139
expect(document.defaultView).toBe(globalThis)

0 commit comments

Comments
 (0)