Skip to content

Commit

Permalink
fix(jsdom): correctly resolve buffer on typed arrays (#3998)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Aug 24, 2023
1 parent 8caabaa commit b42cf36
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/vitest/src/integrations/env/edge-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { importModule } from 'local-pkg'
import type { Environment } from '../../types'
import { populateGlobal } from './utils'
import { KEYS } from './jsdom-keys'

export default <Environment>({
name: 'edge-runtime',
Expand Down Expand Up @@ -29,6 +30,10 @@ export default <Environment>({
extend: (context) => {
context.global = context
context.Buffer = Buffer
KEYS.forEach((key) => {
if (key in global)
context[key] = global[key]
})
return context
},
})
Expand Down
5 changes: 2 additions & 3 deletions packages/vitest/src/integrations/env/happy-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default <Environment>({

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

// inject structuredClone if it exists
if (typeof structuredClone !== 'undefined' && !win.structuredClone)
Expand All @@ -24,8 +23,8 @@ export default <Environment>({
getVmContext() {
return win
},
teardown() {
win.happyDOM.cancelAsync()
async teardown() {
await win.happyDOM.cancelAsync()
},
}
},
Expand Down
10 changes: 10 additions & 0 deletions packages/vitest/src/integrations/env/jsdom-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ const LIVING_KEYS = [
'Headers',
'AbortController',
'AbortSignal',

'Uint8Array',
'Uint16Array',
'Uint32Array',
'Uint8ClampedArray',
'Int8Array',
'Int16Array',
'Int32Array',
'Float32Array',
'Float64Array',
'ArrayBuffer',
'DOMRectReadOnly',
'DOMRect',
Expand Down
2 changes: 0 additions & 2 deletions packages/vitest/src/integrations/env/jsdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ export default <Environment>({

// TODO: browser doesn't expose Buffer, but a lot of dependencies use it
dom.window.Buffer = Buffer
// Buffer extends Uint8Array
dom.window.Uint8Array = Uint8Array

// inject structuredClone if it exists
if (typeof structuredClone !== 'undefined' && !dom.window.structuredClone)
Expand Down
19 changes: 19 additions & 0 deletions test/core/test/dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,25 @@ it('uses jsdom ArrayBuffer', async () => {
expect(arraybuffer.constructor === ArrayBuffer).toBeTruthy()
})

it.each([
'Uint8Array',
'Uint16Array',
'Uint32Array',
'Uint8ClampedArray',
'Int16Array',
'Int32Array',
'Int8Array',
'Float32Array',
'Float64Array',
] as const)('%s has buffer as ArrayBuffer', async (constructorName) => {
const Constructor = globalThis[constructorName]
const typedArray = new Constructor([1])
expect(typedArray.constructor.name).toBe(constructorName)
expect(typedArray instanceof Constructor).toBeTruthy()
expect(ArrayBuffer.isView(typedArray)).toBeTruthy()
expect(typedArray.buffer instanceof ArrayBuffer).toBeTruthy()
})

it('doesn\'t throw, if listening for error', () => {
const spy = vi.fn((e: Event) => e.preventDefault())
window.addEventListener('error', spy)
Expand Down

0 comments on commit b42cf36

Please sign in to comment.