From e82aca4bcfe3046d9929b7724c61ba8c6788d55c Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 8 Feb 2024 13:17:05 +0100 Subject: [PATCH 1/2] feat(vitest): expose jsdom global if jsdom environment is enabled --- packages/vitest/src/integrations/env/jsdom.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/vitest/src/integrations/env/jsdom.ts b/packages/vitest/src/integrations/env/jsdom.ts index 7141e95b60d9..0b509072a7f0 100644 --- a/packages/vitest/src/integrations/env/jsdom.ts +++ b/packages/vitest/src/integrations/env/jsdom.ts @@ -67,6 +67,7 @@ export default ({ // TODO: browser doesn't expose Buffer, but a lot of dependencies use it dom.window.Buffer = Buffer + dom.window.jsdom = dom // inject web globals if they missing in JSDOM but otherwise available in Nodejs // https://nodejs.org/dist/latest/docs/api/globals.html @@ -141,10 +142,13 @@ export default ({ const clearWindowErrors = catchWindowErrors(global) + global.jsdom = dom + return { teardown(global) { clearWindowErrors() dom.window.close() + delete global.jsdom keys.forEach(key => delete global[key]) originals.forEach((v, k) => global[k] = v) }, From 43ed5b78f41f93be4d634b7f79d2234933b59c1e Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 8 Feb 2024 13:19:00 +0100 Subject: [PATCH 2/2] docs: cleanup --- docs/config/index.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/config/index.md b/docs/config/index.md index d0206a66699e..e2690c3d499a 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -511,6 +511,10 @@ export default { Vitest also exposes `builtinEnvironments` through `vitest/environments` entry, in case you just want to extend it. You can read more about extending environments in [our guide](/guide/environment). +::: tip +Since Vitest 1.3.0 jsdom environment exposes `jsdom` global variable equal to the current [JSDOM](https://github.com/jsdom/jsdom) instance. +::: + ### environmentOptions - **Type:** `Record<'jsdom' | string, unknown>`