Skip to content

Commit b42cf36

Browse files
authored
fix(jsdom): correctly resolve buffer on typed arrays (#3998)
1 parent 8caabaa commit b42cf36

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

packages/vitest/src/integrations/env/edge-runtime.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { importModule } from 'local-pkg'
22
import type { Environment } from '../../types'
33
import { populateGlobal } from './utils'
4+
import { KEYS } from './jsdom-keys'
45

56
export default <Environment>({
67
name: 'edge-runtime',
@@ -29,6 +30,10 @@ export default <Environment>({
2930
extend: (context) => {
3031
context.global = context
3132
context.Buffer = Buffer
33+
KEYS.forEach((key) => {
34+
if (key in global)
35+
context[key] = global[key]
36+
})
3237
return context
3338
},
3439
})

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export default <Environment>({
1414

1515
// TODO: browser doesn't expose Buffer, but a lot of dependencies use it
1616
win.Buffer = Buffer
17-
win.Uint8Array = Uint8Array
1817

1918
// inject structuredClone if it exists
2019
if (typeof structuredClone !== 'undefined' && !win.structuredClone)
@@ -24,8 +23,8 @@ export default <Environment>({
2423
getVmContext() {
2524
return win
2625
},
27-
teardown() {
28-
win.happyDOM.cancelAsync()
26+
async teardown() {
27+
await win.happyDOM.cancelAsync()
2928
},
3029
}
3130
},

packages/vitest/src/integrations/env/jsdom-keys.ts

+10
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ const LIVING_KEYS = [
163163
'Headers',
164164
'AbortController',
165165
'AbortSignal',
166+
167+
'Uint8Array',
168+
'Uint16Array',
169+
'Uint32Array',
170+
'Uint8ClampedArray',
171+
'Int8Array',
172+
'Int16Array',
173+
'Int32Array',
174+
'Float32Array',
175+
'Float64Array',
166176
'ArrayBuffer',
167177
'DOMRectReadOnly',
168178
'DOMRect',

packages/vitest/src/integrations/env/jsdom.ts

-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ export default <Environment>({
6868

6969
// TODO: browser doesn't expose Buffer, but a lot of dependencies use it
7070
dom.window.Buffer = Buffer
71-
// Buffer extends Uint8Array
72-
dom.window.Uint8Array = Uint8Array
7371

7472
// inject structuredClone if it exists
7573
if (typeof structuredClone !== 'undefined' && !dom.window.structuredClone)

test/core/test/dom.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ it('uses jsdom ArrayBuffer', async () => {
164164
expect(arraybuffer.constructor === ArrayBuffer).toBeTruthy()
165165
})
166166

167+
it.each([
168+
'Uint8Array',
169+
'Uint16Array',
170+
'Uint32Array',
171+
'Uint8ClampedArray',
172+
'Int16Array',
173+
'Int32Array',
174+
'Int8Array',
175+
'Float32Array',
176+
'Float64Array',
177+
] as const)('%s has buffer as ArrayBuffer', async (constructorName) => {
178+
const Constructor = globalThis[constructorName]
179+
const typedArray = new Constructor([1])
180+
expect(typedArray.constructor.name).toBe(constructorName)
181+
expect(typedArray instanceof Constructor).toBeTruthy()
182+
expect(ArrayBuffer.isView(typedArray)).toBeTruthy()
183+
expect(typedArray.buffer instanceof ArrayBuffer).toBeTruthy()
184+
})
185+
167186
it('doesn\'t throw, if listening for error', () => {
168187
const spy = vi.fn((e: Event) => e.preventDefault())
169188
window.addEventListener('error', spy)

0 commit comments

Comments
 (0)