Skip to content

Commit 6743008

Browse files
authored
fix: use happy-dom/jsdom types for envionmentOptions (#7795)
1 parent 03f55d7 commit 6743008

File tree

11 files changed

+35
-59
lines changed

11 files changed

+35
-59
lines changed

packages/vitest/optional-types.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* eslint-disable ts/ban-ts-comment */
2+
3+
// @ts-ignore optional peer dep
4+
export type * as jsdomTypes from 'jsdom'
5+
6+
// @ts-ignore optional peer dep
7+
export type * as happyDomTypes from 'happy-dom'

packages/vitest/rollup.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const external = [
7676
'node:http',
7777
'node:console',
7878
'inspector',
79+
'vitest/optional-types.js',
7980
'vite-node/source-map',
8081
'vite-node/client',
8182
'vite-node/server',

packages/vitest/src/integrations/env/jsdom.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Environment } from '../../types/environment'
2+
import type { JSDOMOptions } from '../../types/jsdom-options'
23
import { populateGlobal } from './utils'
34

45
function catchWindowErrors(window: Window) {
@@ -51,7 +52,7 @@ export default <Environment>{
5152
console = false,
5253
cookieJar = false,
5354
...restOptions
54-
} = jsdom as any
55+
} = jsdom as JSDOMOptions
5556
let dom = new JSDOM(html, {
5657
pretendToBeVisual,
5758
resources:
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
/**
2-
* Happy DOM options.
3-
*/
4-
export interface HappyDOMOptions {
5-
width?: number
6-
height?: number
7-
url?: string
8-
settings?: {
9-
disableJavaScriptEvaluation?: boolean
10-
disableJavaScriptFileLoading?: boolean
11-
disableCSSFileLoading?: boolean
12-
disableIframePageLoading?: boolean
13-
disableComputedStyleRendering?: boolean
14-
enableFileSystemHttpRequests?: boolean
15-
navigator?: {
16-
userAgent?: string
17-
}
18-
device?: {
19-
prefersColorScheme?: string
20-
mediaType?: string
21-
}
22-
}
23-
}
1+
import type { happyDomTypes } from 'vitest/optional-types.js'
2+
3+
export type HappyDOMOptions = Omit<
4+
NonNullable<ConstructorParameters<typeof happyDomTypes.Window>[0]>,
5+
'console'
6+
>

packages/vitest/src/types/jsdom-options.ts

+5-35
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
export interface JSDOMOptions {
1+
import type { jsdomTypes } from 'vitest/optional-types.js'
2+
3+
export type JSDOMOptions = ConstructorOptionsOverride & Omit<jsdomTypes.ConstructorOptions, keyof ConstructorOptionsOverride>
4+
5+
interface ConstructorOptionsOverride {
26
/**
37
* The html content for the test.
48
*
59
* @default '<!DOCTYPE html>'
610
*/
711
html?: string | ArrayBufferLike
8-
/**
9-
* referrer just affects the value read from document.referrer.
10-
* It defaults to no referrer (which reflects as the empty string).
11-
*/
12-
referrer?: string
1312
/**
1413
* userAgent affects the value read from navigator.userAgent, as well as the User-Agent header sent while fetching subresources.
1514
*
@@ -24,21 +23,6 @@ export interface JSDOMOptions {
2423
* @default 'http://localhost:3000'.
2524
*/
2625
url?: string
27-
/**
28-
* contentType affects the value read from document.contentType, and how the document is parsed: as HTML or as XML.
29-
* Values that are not "text/html" or an XML mime type will throw.
30-
*
31-
* @default 'text/html'.
32-
*/
33-
contentType?: string
34-
/**
35-
* The maximum size in code units for the separate storage areas used by localStorage and sessionStorage.
36-
* Attempts to store data larger than this limit will cause a DOMException to be thrown. By default, it is set
37-
* to 5,000,000 code units per origin, as inspired by the HTML specification.
38-
*
39-
* @default 5_000_000
40-
*/
41-
storageQuota?: number
4226
/**
4327
* Enable console?
4428
*
@@ -55,20 +39,6 @@ export interface JSDOMOptions {
5539
* @default true
5640
*/
5741
pretendToBeVisual?: boolean
58-
/**
59-
* `includeNodeLocations` preserves the location info produced by the HTML parser,
60-
* allowing you to retrieve it with the nodeLocation() method (described below).
61-
*
62-
* It defaults to false to give the best performance,
63-
* and cannot be used with an XML content type since our XML parser does not support location info.
64-
*
65-
* @default false
66-
*/
67-
includeNodeLocations?: boolean | undefined
68-
/**
69-
* @default 'dangerously'
70-
*/
71-
runScripts?: 'dangerously' | 'outside-only'
7242
/**
7343
* Enable CookieJar
7444
*

test/dts-config/happy-dom-patch.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// happy-dom's dts will break `skipLibCheck: false`.
2+
// for now, override happy-dom type for the sake of testing other packages.
3+
export const Window = {} as any

test/dts-config/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"extends": ["./tsconfig.patch.json"],
23
"compilerOptions": {
34
"target": "ESNext",
45
"module": "ESNext",

test/dts-config/tsconfig.patch.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"paths": {
4+
"happy-dom": ["./happy-dom-patch.ts"]
5+
}
6+
}
7+
}

test/dts-fixture/tsconfig.check.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"extends": ["../dts-config/tsconfig.patch.json"],
23
"compilerOptions": {
34
"target": "ESNext",
45
"module": "NodeNext",

test/dts-fixture/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"extends": ["../dts-config/tsconfig.patch.json"],
23
"compilerOptions": {
34
"target": "ESNext",
45
"module": "ESNext",

test/dts-playwright/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"extends": ["../dts-config/tsconfig.patch.json"],
23
"compilerOptions": {
34
"target": "ESNext",
45
"module": "ESNext",

0 commit comments

Comments
 (0)