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

feat(vitest): expose jsdom global if jsdom environment is enabled #5155

Merged
merged 3 commits into from
Feb 8, 2024
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
4 changes: 4 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ export default <Environment>{

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>`
Expand Down
4 changes: 4 additions & 0 deletions packages/vitest/src/integrations/env/jsdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default <Environment>({

// 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
Expand Down Expand Up @@ -141,10 +142,13 @@ export default <Environment>({

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)
},
Expand Down