Skip to content
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ test/cli/fixtures/browser-multiple/basic-*
# exclude static html reporter folder
test/browser/html/
test/core/html/
.vitest-attachments
.vitest-attachments
explainFiles.txt
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"test:ci": "CI=true pnpm -r --reporter-hide-prefix --stream --sequential --filter '@vitest/test-*' --filter !test-browser run test",
"test:examples": "CI=true pnpm -r --reporter-hide-prefix --stream --filter '@vitest/example-*' run test",
"test:ecosystem-ci": "ECOSYSTEM_CI=true pnpm test:ci",
"typebuild": "tsx ./scripts/explain-types.ts",
"typecheck": "tsc -p tsconfig.check.json --noEmit",
"typecheck:why": "tsc -p tsconfig.check.json --noEmit --explainFiles > explainTypes.txt",
"ui:build": "vite build packages/ui",
Expand Down
2 changes: 2 additions & 0 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
"providers"
],
"scripts": {
"typecheck": "tsc -p ./src/client/tsconfig.json --noEmit",
"typecheck:why": "tsc -p ./src/client/tsconfig.json --noEmit --explainFiles > explainTypes.txt",
"build": "rimraf dist && pnpm build:node && pnpm build:client",
"build:client": "vite build src/client",
"build:node": "rollup -c",
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ModuleMocker } from '@vitest/mocker/browser'
import type { CancelReason } from '@vitest/runner'
import type { BirpcReturn } from 'birpc'
import type { WebSocketBrowserEvents, WebSocketBrowserHandlers } from '../node/types'
import type { WebSocketBrowserEvents, WebSocketBrowserHandlers } from '../types'
import type { IframeOrchestrator } from './orchestrator'
import { createBirpc } from 'birpc'
import { parse, stringify } from 'flatted'
Expand Down
28 changes: 19 additions & 9 deletions packages/browser/src/client/tester/context.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { Options as TestingLibraryOptions, UserEvent as TestingLibraryUserEvent } from '@testing-library/user-event'
import type { RunnerTask } from 'vitest'
import type {
Options as TestingLibraryOptions,
UserEvent as TestingLibraryUserEvent,
} from '@testing-library/user-event'
import type {
BrowserLocators,
BrowserPage,
Locator,
UserEvent,
} from '../../../context'
} from '@vitest/browser/context'
import type { RunnerTask } from 'vitest'
import type { IframeViewportEvent } from '../client'
import type { BrowserRunnerState } from '../utils'
import type { Locator as LocatorAPI } from './locators/index'
Expand Down Expand Up @@ -289,12 +292,19 @@ export const page: BrowserPage = {
const name
= options.path || `${taskName.replace(/[^a-z0-9]/gi, '-')}-${number}.png`

return ensureAwaited(error => triggerCommand('__vitest_screenshot', [name, processTimeoutOptions({
...options,
element: options.element
? convertToSelector(options.element)
: undefined,
})], error))
return ensureAwaited(error => triggerCommand(
'__vitest_screenshot',
[
name,
processTimeoutOptions({
...options,
element: options.element
? convertToSelector(options.element)
: undefined,
} as any /** TODO */),
],
error,
))
},
getByRole() {
throw new Error(`Method "getByRole" is not implemented in the "${provider}" provider.`)
Expand Down
10 changes: 9 additions & 1 deletion packages/browser/src/client/tester/locators/playwright.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import type { UserEventClearOptions, UserEventClickOptions, UserEventDragAndDropOptions, UserEventFillOptions, UserEventHoverOptions, UserEventSelectOptions, UserEventUploadOptions } from '@vitest/browser/context'
import type {
UserEventClearOptions,
UserEventClickOptions,
UserEventDragAndDropOptions,
UserEventFillOptions,
UserEventHoverOptions,
UserEventSelectOptions,
UserEventUploadOptions,
} from '@vitest/browser/context'
import { page, server } from '@vitest/browser/context'
import {
getByAltTextSelector,
Expand Down
7 changes: 6 additions & 1 deletion packages/browser/src/client/tester/locators/webdriverio.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { UserEventClickOptions, UserEventDragAndDropOptions, UserEventHoverOptions, UserEventSelectOptions } from '@vitest/browser/context'
import type {
UserEventClickOptions,
UserEventDragAndDropOptions,
UserEventHoverOptions,
UserEventSelectOptions,
} from '@vitest/browser/context'
import { page, server } from '@vitest/browser/context'
import {
getByAltTextSelector,
Expand Down
7 changes: 3 additions & 4 deletions packages/browser/src/client/tester/runner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { CancelReason, File, Suite, Task, TaskEventPack, TaskResultPack, Test, TestAnnotation, VitestRunner } from '@vitest/runner'
import type { SerializedConfig, TestExecutionMethod, WorkerGlobalState } from 'vitest'
import type { VitestExecutor } from 'vitest/execute'
import type { VitestBrowserClientMocker } from './mocker'
import { globalChannel, onCancel } from '@vitest/browser/client'
import { page, userEvent } from '@vitest/browser/context'
Expand Down Expand Up @@ -77,7 +76,7 @@ export function createBrowserRunner(
if (this.config.browser.screenshotFailures && document.body.clientHeight > 0 && task.result?.state === 'fail') {
const screenshot = await page.screenshot({
timeout: this.config.browser.providerOptions?.actionTimeout ?? 5_000,
}).catch((err) => {
} as any /** TODO */).catch((err) => {
console.error('[vitest] Failed to take a screenshot', err)
})
if (screenshot) {
Expand Down Expand Up @@ -239,8 +238,8 @@ export async function initiateRunner(
})

const [diffOptions] = await Promise.all([
loadDiffConfig(config, executor as unknown as VitestExecutor),
loadSnapshotSerializers(config, executor as unknown as VitestExecutor),
loadDiffConfig(config, executor as any),
loadSnapshotSerializers(config, executor as any),
])
runner.config.diffOptions = diffOptions
getWorkerState().onFilterStackTrace = (stack: string) => {
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/client/tester/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const state: WorkerGlobalState = {
},
onCleanup: fn => getBrowserState().cleanups.push(fn),
moduleCache: getBrowserState().moduleCache,
moduleExecutionInfo: new Map(),
rpc: null as any,
durations: {
environment: 0,
Expand Down
21 changes: 21 additions & 0 deletions packages/browser/src/client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"lib": [
"dom",
"esnext",
"DOM.Iterable"
],
"types": ["vite/client"],
"noEmit": true
},
"include": [
"./**/*.ts",
"../types.ts",
"../../matchers.d.ts",
"../../../vitest/src/integrations/chai/chai-subset.d.ts"
],
"exclude": [
"./vite.config.ts"
]
}
2 changes: 1 addition & 1 deletion packages/browser/src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function ensureAwaited<T>(promise: (error?: Error) => Promise<T>): Promis
export interface BrowserRunnerState {
files: string[]
runningFiles: string[]
moduleCache: WorkerGlobalState['moduleCache']
moduleCache: Map<string, any>
config: SerializedConfig
provider: string
runner: VitestRunner
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/node/cdp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CDPSession } from 'vitest/node'
import type { WebSocketBrowserRPC } from './types'
import type { WebSocketBrowserRPC } from '../types'

export class BrowserServerCDPHandler {
private listenerIds: Record<string, string[]> = {}
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/node/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type { Duplex } from 'node:stream'
import type { TestError } from 'vitest'
import type { BrowserCommandContext, ResolveSnapshotPathHandlerContext, TestProject } from 'vitest/node'
import type { WebSocket } from 'ws'
import type { WebSocketBrowserEvents, WebSocketBrowserHandlers } from '../types'
import type { ParentBrowserProject } from './projectParent'
import type { WebdriverBrowserProvider } from './providers/webdriver'
import type { BrowserServerState } from './state'
import type { WebSocketBrowserEvents, WebSocketBrowserHandlers } from './types'
import { existsSync, promises as fs } from 'node:fs'
import { AutomockedModule, AutospiedModule, ManualMockedModule, RedirectedModule } from '@vitest/mocker'
import { ServerMockResolver } from '@vitest/mocker/node'
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/node/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BrowserServerState as IBrowserServerState } from 'vitest/node'
import type { WebSocketBrowserRPC } from './types'
import type { WebSocketBrowserRPC } from '../types'

export class BrowserServerState implements IBrowserServerState {
public readonly orchestrators: Map<string, WebSocketBrowserRPC> = new Map()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { MockedModuleSerialized } from '@vitest/mocker'
import type { ServerIdResolution, ServerMockResolution } from '@vitest/mocker/node'
import type { MockedModuleSerialized, ServerIdResolution, ServerMockResolution } from '@vitest/mocker'
import type { TaskEventPack, TaskResultPack, TestAnnotation } from '@vitest/runner'
import type { BirpcReturn } from 'birpc'
import type {
AfterSuiteRunMeta,
BrowserTesterOptions,
CancelReason,
Reporter,
RunnerTestFile,
SerializedTestSpecification,
SnapshotResult,
TestExecutionMethod,
UserConsoleLog,
Expand Down Expand Up @@ -60,16 +59,19 @@ export interface WebSocketBrowserHandlers {
trackCdpEvent: (sessionId: string, type: 'on' | 'once' | 'off', event: string, listenerId: string) => void
}

export interface WebSocketEvents
extends Pick<
Reporter,
| 'onCollected'
| 'onFinished'
| 'onTaskUpdate'
| 'onUserConsoleLog'
| 'onPathsCollected'
| 'onSpecsCollected'
> {
export type Awaitable<T> = T | PromiseLike<T>

export interface WebSocketEvents {
onCollected?: (files: RunnerTestFile[]) => Awaitable<void>
onFinished?: (
files: File[],
errors: unknown[],
coverage?: unknown
) => Awaitable<void>
onTaskUpdate?: (packs: TaskResultPack[]) => Awaitable<void>
onUserConsoleLog?: (log: UserConsoleLog) => Awaitable<void>
onPathsCollected?: (paths?: string[]) => Awaitable<void>
onSpecsCollected?: (specs?: SerializedTestSpecification[]) => Awaitable<void>
onFinishedReportCoverage: () => void
}

Expand Down
7 changes: 6 additions & 1 deletion packages/browser/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
"types": ["node", "vite/client"],
"isolatedDeclarations": true
},
"exclude": ["dist", "node_modules", "**/vite.config.ts"]
"exclude": [
"dist",
"node_modules",
"**/vite.config.ts",
"src/client/**/*.ts"
]
}
2 changes: 2 additions & 0 deletions packages/mocker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ export type {
ModuleMockFactory,
ModuleMockFactoryWithHelper,
ModuleMockOptions,
ServerIdResolution,
ServerMockResolution,
} from './types'
6 changes: 1 addition & 5 deletions packages/mocker/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@ export type { InterceptorPluginOptions } from './interceptorPlugin'
export { mockerPlugin } from './mockerPlugin'
export { findMockRedirect } from './redirect'
export { ServerMockResolver } from './resolver'
export type {
ServerIdResolution,
ServerMockResolution,
ServerResolverOptions,
} from './resolver'
export type { ServerResolverOptions } from './resolver'
15 changes: 1 addition & 14 deletions packages/mocker/src/node/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Rollup, ResolvedConfig as ViteConfig, ViteDevServer } from 'vite'
import type { ServerIdResolution, ServerMockResolution } from '../types'
import { existsSync, readFileSync } from 'node:fs'
import { isAbsolute, join, resolve } from 'pathe'
import { cleanUrl } from '../utils'
Expand Down Expand Up @@ -181,17 +182,3 @@ function withTrailingSlash(path: string): string {

return path
}

export interface ServerMockResolution {
mockType: 'manual' | 'redirect' | 'automock' | 'autospy'
resolvedId: string
resolvedUrl: string
needsInterop?: boolean
redirectUrl?: string | null
}

export interface ServerIdResolution {
id: string
url: string
optimized: boolean
}
14 changes: 14 additions & 0 deletions packages/mocker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@ export type ModuleMockFactory = () => any
export interface ModuleMockOptions {
spy?: boolean
}

export interface ServerMockResolution {
mockType: 'manual' | 'redirect' | 'automock' | 'autospy'
resolvedId: string
resolvedUrl: string
needsInterop?: boolean
redirectUrl?: string | null
}

export interface ServerIdResolution {
id: string
url: string
optimized: boolean
}
1 change: 1 addition & 0 deletions packages/snapshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { default as SnapshotState } from './port/state'

export type {
SnapshotData,
SnapshotEnvironment,
SnapshotMatchOptions,
SnapshotResult,
SnapshotSerializer,
Expand Down
1 change: 1 addition & 0 deletions packages/ui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"jsx": "preserve",
"lib": ["dom", "esnext", "DOM.Iterable"],
"skipLibCheck": true
},
"include": ["./client/**/*"],
Expand Down
1 change: 0 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export type {
Constructable,
DeepMerge,
MergeInsertions,
MutableArray,
Nullable,
ParsedStack,
SerializedError,
Expand Down
21 changes: 14 additions & 7 deletions packages/utils/src/timers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
const SAFE_TIMERS_SYMBOL = Symbol('vitest:SAFE_TIMERS')

export interface SafeTimers {
nextTick: (cb: () => void) => void
// node.js timers
nextTick?: (cb: () => void) => void
setImmediate?: {
<TArgs extends any[]>(
callback: (...args: TArgs) => void,
...args: TArgs
): any
__promisify__: <T = void>(value?: T, options?: any) => Promise<T>
}
clearImmediate?: (immediateId: any) => void

// cross-platform timers
setTimeout: typeof setTimeout
setInterval: typeof setInterval
clearInterval: typeof clearInterval
clearTimeout: typeof clearTimeout
setImmediate: typeof setImmediate
clearImmediate: typeof clearImmediate
queueMicrotask: typeof queueMicrotask
}

Expand All @@ -23,7 +32,7 @@ export function getSafeTimers(): SafeTimers {
} = (globalThis as any)[SAFE_TIMERS_SYMBOL] || globalThis

const { nextTick: safeNextTick } = (globalThis as any)[SAFE_TIMERS_SYMBOL]
|| globalThis.process || { nextTick: (cb: () => void) => cb() }
|| globalThis.process || {}

return {
nextTick: safeNextTick,
Expand All @@ -48,9 +57,7 @@ export function setSafeTimers(): void {
queueMicrotask: safeQueueMicrotask,
} = globalThis

const { nextTick: safeNextTick } = globalThis.process || {
nextTick: cb => cb(),
}
const { nextTick: safeNextTick } = globalThis.process || {}

const timers = {
nextTick: safeNextTick,
Expand Down
4 changes: 0 additions & 4 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ export type DeepMerge<F, S> = MergeInsertions<{
: never;
}>

export type MutableArray<T extends readonly any[]> = {
-readonly [k in keyof T]: T[k];
}

export interface Constructable {
new (...args: any[]): any
}
Expand Down
2 changes: 0 additions & 2 deletions packages/vitest/src/integrations/chai/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// CI fails only for this file, but it works locally

import type { Assertion, ExpectStatic, MatcherState } from '@vitest/expect'
import type { TaskPopulated, Test } from '@vitest/runner'
import {
Expand Down
Loading