Skip to content

Commit 0f898d8

Browse files
authoredFeb 9, 2024
fix(vitest): add types for the new global jsdom variable (#5164)
1 parent 3afe68f commit 0f898d8

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed
 

‎docs/config/index.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,15 @@ export default <Environment>{
512512
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).
513513

514514
::: tip
515-
Since Vitest 1.3.0 jsdom environment exposes `jsdom` global variable equal to the current [JSDOM](https://github.com/jsdom/jsdom) instance.
515+
Since Vitest 1.3.0 jsdom environment exposes `jsdom` global variable equal to the current [JSDOM](https://github.com/jsdom/jsdom) instance. If you want TypeScript to recognize it, you can add `vitest/jsdom` to your `tsconfig.json` when you use this environment:
516+
517+
```json
518+
{
519+
"compilerOptions": {
520+
"types": ["vitest/jsdom"]
521+
}
522+
}
523+
```
516524
:::
517525

518526
### environmentOptions

‎packages/vitest/jsdom.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { JSDOM } from 'jsdom'
2+
3+
declare global {
4+
const jsdom: JSDOM
5+
}
6+
export {}

‎packages/vitest/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
"./globals": {
3939
"types": "./globals.d.ts"
4040
},
41+
"./jsdom": {
42+
"types": "./jsdom.d.ts"
43+
},
4144
"./importMeta": {
4245
"types": "./importMeta.d.ts"
4346
},

‎test/core/test/environments/jsdom.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,11 @@ test('request doesn\'t support absolute URL because jsdom doesn\'t provide compa
105105
const _r = new Request('/api', { method: 'GET' })
106106
}).toThrow(/Failed to parse URL/)
107107
})
108+
109+
test('jsdom global is exposed', () => {
110+
// @ts-expect-error -- jsdom is not exposed in our types because we use a single tsconfig for all
111+
const dom = jsdom
112+
expect(dom).toBeDefined()
113+
dom.reconfigure({ url: 'https://examples.new.com' })
114+
expect(location.href).toBe('https://examples.new.com/')
115+
})

0 commit comments

Comments
 (0)
Please sign in to comment.