Skip to content

Commit

Permalink
feat(vitest): expose jsdom global if jsdom environment is enabled (#5155
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sheremet-va authored Feb 8, 2024
1 parent 219d763 commit 567d20b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
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

0 comments on commit 567d20b

Please sign in to comment.