diff --git a/packages/vitest/optional-types.d.ts b/packages/vitest/optional-types.d.ts new file mode 100644 index 000000000..49cdb4fae --- /dev/null +++ b/packages/vitest/optional-types.d.ts @@ -0,0 +1,7 @@ +/* eslint-disable ts/ban-ts-comment */ + +// @ts-ignore optional peer dep +export type * as jsdomTypes from 'jsdom' + +// @ts-ignore optional peer dep +export type * as happyDomTypes from 'happy-dom' diff --git a/packages/vitest/rollup.config.js b/packages/vitest/rollup.config.js index 0e1e6b66e..f2ad94aff 100644 --- a/packages/vitest/rollup.config.js +++ b/packages/vitest/rollup.config.js @@ -76,6 +76,7 @@ const external = [ 'node:http', 'node:console', 'inspector', + 'vitest/optional-types.js', 'vite-node/source-map', 'vite-node/client', 'vite-node/server', diff --git a/packages/vitest/src/integrations/env/jsdom.ts b/packages/vitest/src/integrations/env/jsdom.ts index c1d6c181a..d9020c27c 100644 --- a/packages/vitest/src/integrations/env/jsdom.ts +++ b/packages/vitest/src/integrations/env/jsdom.ts @@ -1,4 +1,5 @@ import type { Environment } from '../../types/environment' +import type { JSDOMOptions } from '../../types/jsdom-options' import { populateGlobal } from './utils' function catchWindowErrors(window: Window) { @@ -51,7 +52,7 @@ export default { console = false, cookieJar = false, ...restOptions - } = jsdom as any + } = jsdom as JSDOMOptions let dom = new JSDOM(html, { pretendToBeVisual, resources: diff --git a/packages/vitest/src/types/happy-dom-options.ts b/packages/vitest/src/types/happy-dom-options.ts index 67a7b6958..ec2e73a9c 100644 --- a/packages/vitest/src/types/happy-dom-options.ts +++ b/packages/vitest/src/types/happy-dom-options.ts @@ -1,23 +1,6 @@ -/** - * Happy DOM options. - */ -export interface HappyDOMOptions { - width?: number - height?: number - url?: string - settings?: { - disableJavaScriptEvaluation?: boolean - disableJavaScriptFileLoading?: boolean - disableCSSFileLoading?: boolean - disableIframePageLoading?: boolean - disableComputedStyleRendering?: boolean - enableFileSystemHttpRequests?: boolean - navigator?: { - userAgent?: string - } - device?: { - prefersColorScheme?: string - mediaType?: string - } - } -} +import type { happyDomTypes } from 'vitest/optional-types.js' + +export type HappyDOMOptions = Omit< + NonNullable[0]>, + 'console' +> diff --git a/packages/vitest/src/types/jsdom-options.ts b/packages/vitest/src/types/jsdom-options.ts index 6248812aa..b1939c4cd 100644 --- a/packages/vitest/src/types/jsdom-options.ts +++ b/packages/vitest/src/types/jsdom-options.ts @@ -1,15 +1,14 @@ -export interface JSDOMOptions { +import type { jsdomTypes } from 'vitest/optional-types.js' + +export type JSDOMOptions = ConstructorOptionsOverride & Omit + +interface ConstructorOptionsOverride { /** * The html content for the test. * * @default '' */ html?: string | ArrayBufferLike - /** - * referrer just affects the value read from document.referrer. - * It defaults to no referrer (which reflects as the empty string). - */ - referrer?: string /** * userAgent affects the value read from navigator.userAgent, as well as the User-Agent header sent while fetching subresources. * @@ -24,21 +23,6 @@ export interface JSDOMOptions { * @default 'http://localhost:3000'. */ url?: string - /** - * contentType affects the value read from document.contentType, and how the document is parsed: as HTML or as XML. - * Values that are not "text/html" or an XML mime type will throw. - * - * @default 'text/html'. - */ - contentType?: string - /** - * The maximum size in code units for the separate storage areas used by localStorage and sessionStorage. - * Attempts to store data larger than this limit will cause a DOMException to be thrown. By default, it is set - * to 5,000,000 code units per origin, as inspired by the HTML specification. - * - * @default 5_000_000 - */ - storageQuota?: number /** * Enable console? * @@ -55,20 +39,6 @@ export interface JSDOMOptions { * @default true */ pretendToBeVisual?: boolean - /** - * `includeNodeLocations` preserves the location info produced by the HTML parser, - * allowing you to retrieve it with the nodeLocation() method (described below). - * - * It defaults to false to give the best performance, - * and cannot be used with an XML content type since our XML parser does not support location info. - * - * @default false - */ - includeNodeLocations?: boolean | undefined - /** - * @default 'dangerously' - */ - runScripts?: 'dangerously' | 'outside-only' /** * Enable CookieJar * diff --git a/test/dts-config/happy-dom-patch.ts b/test/dts-config/happy-dom-patch.ts new file mode 100644 index 000000000..41ec74058 --- /dev/null +++ b/test/dts-config/happy-dom-patch.ts @@ -0,0 +1,3 @@ +// happy-dom's dts will break `skipLibCheck: false`. +// for now, override happy-dom type for the sake of testing other packages. +export const Window = {} as any diff --git a/test/dts-config/tsconfig.json b/test/dts-config/tsconfig.json index 7d66e7d2d..79dc9ff48 100644 --- a/test/dts-config/tsconfig.json +++ b/test/dts-config/tsconfig.json @@ -1,4 +1,5 @@ { + "extends": ["./tsconfig.patch.json"], "compilerOptions": { "target": "ESNext", "module": "ESNext", diff --git a/test/dts-config/tsconfig.patch.json b/test/dts-config/tsconfig.patch.json new file mode 100644 index 000000000..cc74095b6 --- /dev/null +++ b/test/dts-config/tsconfig.patch.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "paths": { + "happy-dom": ["./happy-dom-patch.ts"] + } + } +} diff --git a/test/dts-fixture/tsconfig.check.json b/test/dts-fixture/tsconfig.check.json index bd4257e82..7e3181eba 100644 --- a/test/dts-fixture/tsconfig.check.json +++ b/test/dts-fixture/tsconfig.check.json @@ -1,4 +1,5 @@ { + "extends": ["../dts-config/tsconfig.patch.json"], "compilerOptions": { "target": "ESNext", "module": "NodeNext", diff --git a/test/dts-fixture/tsconfig.json b/test/dts-fixture/tsconfig.json index 764098fa8..76cc525c8 100644 --- a/test/dts-fixture/tsconfig.json +++ b/test/dts-fixture/tsconfig.json @@ -1,4 +1,5 @@ { + "extends": ["../dts-config/tsconfig.patch.json"], "compilerOptions": { "target": "ESNext", "module": "ESNext", diff --git a/test/dts-playwright/tsconfig.json b/test/dts-playwright/tsconfig.json index 87c7ac1e7..66b2370df 100644 --- a/test/dts-playwright/tsconfig.json +++ b/test/dts-playwright/tsconfig.json @@ -1,4 +1,5 @@ { + "extends": ["../dts-config/tsconfig.patch.json"], "compilerOptions": { "target": "ESNext", "module": "ESNext",