From dc810aa76b02d35b50696e9ffe738857ebfcedfe Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 5 Dec 2023 12:36:11 +0100 Subject: [PATCH] fix(vitest): support new Request('/api') in happy-dom --- packages/vitest/src/integrations/env/happy-dom.ts | 6 +++++- packages/vitest/src/integrations/env/utils.ts | 11 +++++++---- test/core/test/environments/happy-dom.spec.ts | 6 ++++++ test/core/test/environments/jsdom.spec.ts | 6 ++++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/vitest/src/integrations/env/happy-dom.ts b/packages/vitest/src/integrations/env/happy-dom.ts index 5bcb794cb561..5f0bf594d6a3 100644 --- a/packages/vitest/src/integrations/env/happy-dom.ts +++ b/packages/vitest/src/integrations/env/happy-dom.ts @@ -47,7 +47,11 @@ export default ({ }, }) - const { keys, originals } = populateGlobal(global, win, { bindFunctions: true }) + const { keys, originals } = populateGlobal(global, win, { + bindFunctions: true, + // jsdom doesn't support Request and Response, but happy-dom does + additionalKeys: ['Request', 'Response'], + }) return { teardown(global) { diff --git a/packages/vitest/src/integrations/env/utils.ts b/packages/vitest/src/integrations/env/utils.ts index 230f1f2bbd55..28c9edba4884 100644 --- a/packages/vitest/src/integrations/env/utils.ts +++ b/packages/vitest/src/integrations/env/utils.ts @@ -7,13 +7,14 @@ const skipKeys = [ 'parent', ] -export function getWindowKeys(global: any, win: any) { - const keys = new Set(KEYS.concat(Object.getOwnPropertyNames(win)) +export function getWindowKeys(global: any, win: any, additionalKeys: string[] = []) { + const keysArray = [...additionalKeys, ...KEYS] + const keys = new Set(keysArray.concat(Object.getOwnPropertyNames(win)) .filter((k) => { if (skipKeys.includes(k)) return false if (k in global) - return KEYS.includes(k) + return keysArray.includes(k) return true })) @@ -31,11 +32,13 @@ interface PopulateOptions { // has a priority for getting implementation from symbols // (global doesn't have these symbols, but window - does) bindFunctions?: boolean + + additionalKeys?: string[] } export function populateGlobal(global: any, win: any, options: PopulateOptions = {}) { const { bindFunctions = false } = options - const keys = getWindowKeys(global, win) + const keys = getWindowKeys(global, win, options.additionalKeys) const originals = new Map() diff --git a/test/core/test/environments/happy-dom.spec.ts b/test/core/test/environments/happy-dom.spec.ts index f61fcf089a17..be62003a77fe 100644 --- a/test/core/test/environments/happy-dom.spec.ts +++ b/test/core/test/environments/happy-dom.spec.ts @@ -20,3 +20,9 @@ test('atob and btoa are available', () => { expect(atob('aGVsbG8gd29ybGQ=')).toBe('hello world') expect(btoa('hello world')).toBe('aGVsbG8gd29ybGQ=') }) + +test('request doesn\'t fail when using absolute url because it supports it', () => { + expect(() => { + const _r = new Request('/api', { method: 'GET' }) + }).not.toThrow() +}) diff --git a/test/core/test/environments/jsdom.spec.ts b/test/core/test/environments/jsdom.spec.ts index 31c5a466fa0d..f07287325d99 100644 --- a/test/core/test/environments/jsdom.spec.ts +++ b/test/core/test/environments/jsdom.spec.ts @@ -99,3 +99,9 @@ test('toContain correctly handles DOM nodes', () => { `) } }) + +test('request doesn\'t support absolute URL because jsdom doesn\'t provide compatible Request so Vitest is using Node.js Request', () => { + expect(() => { + const _r = new Request('/api', { method: 'GET' }) + }).toThrow(/Failed to parse URL/) +})