Skip to content

Commit

Permalink
fix(browser): always clean up iframes on rerun (#5827)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jun 3, 2024
1 parent 1831008 commit 087fa87
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
14 changes: 12 additions & 2 deletions packages/browser/src/client/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ interface IframeViewportEvent {
id: string
}

type IframeChannelEvent = IframeDoneEvent | IframeErrorEvent | IframeViewportEvent
interface IframeViewportChannelEvent {
type: 'viewport:done' | 'viewport:fail'
}

type IframeChannelEvent = IframeDoneEvent | IframeErrorEvent | IframeViewportEvent | IframeViewportChannelEvent

async function getContainer(config: ResolvedConfig): Promise<HTMLDivElement> {
if (config.browser.ui) {
Expand All @@ -93,12 +97,15 @@ async function getContainer(config: ResolvedConfig): Promise<HTMLDivElement> {
return document.querySelector('#vitest-tester') as HTMLDivElement
}

const runningFiles = new Set<string>()

client.ws.addEventListener('open', async () => {
const testFiles = getBrowserState().files

debug('test files', testFiles.join(', '))

const runningFiles = new Set<string>(testFiles)
runningFiles.clear()
testFiles.forEach(file => runningFiles.add(file))

channel.addEventListener('message', async (e: MessageEvent<IframeChannelEvent>): Promise<void> => {
debug('channel event', JSON.stringify(e.data))
Expand Down Expand Up @@ -171,6 +178,9 @@ client.ws.addEventListener('open', async () => {
})

async function createTesters(testFiles: string[]) {
runningFiles.clear()
testFiles.forEach(file => runningFiles.add(file))

const config = getConfig()
const container = await getContainer(config)

Expand Down
3 changes: 2 additions & 1 deletion packages/browser/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ export default (project: WorkspaceProject, base = '/'): Plugin[] => {
}

const decodedTestFile = decodeURIComponent(url.pathname.slice(testerPrefix.length))
const testFiles = await project.globTestFiles()
// if decoded test file is "__vitest_all__" or not in the list of known files, run all tests
const tests = decodedTestFile === '__vitest_all__' || !files.includes(decodedTestFile) ? '__vitest_browser_runner__.files' : JSON.stringify([decodedTestFile])
const tests = decodedTestFile === '__vitest_all__' || !testFiles.includes(decodedTestFile) ? '__vitest_browser_runner__.files' : JSON.stringify([decodedTestFile])
const iframeId = JSON.stringify(decodedTestFile)

if (!testerScripts)
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/node/providers/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class PreviewBrowserProvider implements BrowserProvider {
this.ctx = ctx
this.open = false
if (ctx.config.browser.headless)
throw new Error('You\'ve enabled headless mode for "preview" provider but it doesn\'t support it.')
throw new Error('You\'ve enabled headless mode for "preview" provider but it doesn\'t support it. Use "playwright" or "webdriverio" instead: https://vitest.dev/guide/browser#configuration')
}

async openPage(_url: string) {
Expand Down
8 changes: 6 additions & 2 deletions packages/ui/client/components/TasksList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ const filteredTests: ComputedRef<File[]> = computed(() => isFiltered.value ? fil
const failed = computed(() => filtered.value.filter(task => task.result?.state === 'fail'))
const success = computed(() => filtered.value.filter(task => task.result?.state === 'pass'))
const skipped = computed(() => filtered.value.filter(task => task.mode === 'skip' || task.mode === 'todo'))
const running = computed(() => filtered.value.filter(task => !task.result || task.result.state === 'run'))
const running = computed(() => filtered.value.filter(task =>
!failed.value.includes(task)
&& !success.value.includes(task)
&& !skipped.value.includes(task),
))
const disableClearSearch = computed(() => search.value === '')
Expand Down Expand Up @@ -128,7 +132,7 @@ function matchTasks(tasks: Task[], search: string): boolean {
:on-item-click="onItemClick"
/>
</DetailsPanel>
<DetailsPanel v-if="running.length || testRunState === 'running'">
<DetailsPanel v-if="throttledRunning.length">
<template #summary>
<div text-yellow5>
RUNNING ({{ throttledRunning.length }})
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/pools/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function createBrowserPool(ctx: Vitest): ProcessPool {
files,
resolve: () => {
defer.resolve()
project.browserState = undefined
},
reject: defer.reject,
}
Expand Down
14 changes: 11 additions & 3 deletions packages/vitest/src/utils/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ function cloneByOwnProperties(value: any) {
* flatted.stringify().
*/
export function stringifyReplace(key: string, value: any) {
if (value instanceof Error)
return cloneByOwnProperties(value)
else
if (value instanceof Error) {
const cloned = cloneByOwnProperties(value)
return {
name: value.name,
message: value.message,
stack: value.stack,
...cloned,
}
}
else {
return value
}
}

0 comments on commit 087fa87

Please sign in to comment.