From b42cf36e1433f6bb7e3b8dd6e6976dc741d54df5 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 24 Aug 2023 15:30:00 +0200 Subject: [PATCH] fix(jsdom): correctly resolve buffer on typed arrays (#3998) --- .../src/integrations/env/edge-runtime.ts | 5 +++++ .../vitest/src/integrations/env/happy-dom.ts | 5 ++--- .../vitest/src/integrations/env/jsdom-keys.ts | 10 ++++++++++ packages/vitest/src/integrations/env/jsdom.ts | 2 -- test/core/test/dom.test.ts | 19 +++++++++++++++++++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/packages/vitest/src/integrations/env/edge-runtime.ts b/packages/vitest/src/integrations/env/edge-runtime.ts index 9aa3a0fb6cbf..875544c08a32 100644 --- a/packages/vitest/src/integrations/env/edge-runtime.ts +++ b/packages/vitest/src/integrations/env/edge-runtime.ts @@ -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 ({ name: 'edge-runtime', @@ -29,6 +30,10 @@ export default ({ extend: (context) => { context.global = context context.Buffer = Buffer + KEYS.forEach((key) => { + if (key in global) + context[key] = global[key] + }) return context }, }) diff --git a/packages/vitest/src/integrations/env/happy-dom.ts b/packages/vitest/src/integrations/env/happy-dom.ts index fcab5fff7585..21936fafe990 100644 --- a/packages/vitest/src/integrations/env/happy-dom.ts +++ b/packages/vitest/src/integrations/env/happy-dom.ts @@ -14,7 +14,6 @@ export default ({ // 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) @@ -24,8 +23,8 @@ export default ({ getVmContext() { return win }, - teardown() { - win.happyDOM.cancelAsync() + async teardown() { + await win.happyDOM.cancelAsync() }, } }, diff --git a/packages/vitest/src/integrations/env/jsdom-keys.ts b/packages/vitest/src/integrations/env/jsdom-keys.ts index 220624cb987c..677ab3ab8f20 100644 --- a/packages/vitest/src/integrations/env/jsdom-keys.ts +++ b/packages/vitest/src/integrations/env/jsdom-keys.ts @@ -163,6 +163,16 @@ const LIVING_KEYS = [ 'Headers', 'AbortController', 'AbortSignal', + + 'Uint8Array', + 'Uint16Array', + 'Uint32Array', + 'Uint8ClampedArray', + 'Int8Array', + 'Int16Array', + 'Int32Array', + 'Float32Array', + 'Float64Array', 'ArrayBuffer', 'DOMRectReadOnly', 'DOMRect', diff --git a/packages/vitest/src/integrations/env/jsdom.ts b/packages/vitest/src/integrations/env/jsdom.ts index 30910b082f28..f6cc4dd14ef8 100644 --- a/packages/vitest/src/integrations/env/jsdom.ts +++ b/packages/vitest/src/integrations/env/jsdom.ts @@ -68,8 +68,6 @@ export default ({ // 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) diff --git a/test/core/test/dom.test.ts b/test/core/test/dom.test.ts index 355155347a65..5e5246096df0 100644 --- a/test/core/test/dom.test.ts +++ b/test/core/test/dom.test.ts @@ -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)