Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jsdom): correctly resolve buffer on typed arrays #3998

Merged
merged 3 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading