Skip to content

Commit 087fa87

Browse files
authored
fix(browser): always clean up iframes on rerun (#5827)
1 parent 1831008 commit 087fa87

File tree

6 files changed

+33
-9
lines changed

6 files changed

+33
-9
lines changed

packages/browser/src/client/orchestrator.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ interface IframeViewportEvent {
7676
id: string
7777
}
7878

79-
type IframeChannelEvent = IframeDoneEvent | IframeErrorEvent | IframeViewportEvent
79+
interface IframeViewportChannelEvent {
80+
type: 'viewport:done' | 'viewport:fail'
81+
}
82+
83+
type IframeChannelEvent = IframeDoneEvent | IframeErrorEvent | IframeViewportEvent | IframeViewportChannelEvent
8084

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

100+
const runningFiles = new Set<string>()
101+
96102
client.ws.addEventListener('open', async () => {
97103
const testFiles = getBrowserState().files
98104

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

101-
const runningFiles = new Set<string>(testFiles)
107+
runningFiles.clear()
108+
testFiles.forEach(file => runningFiles.add(file))
102109

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

173180
async function createTesters(testFiles: string[]) {
181+
runningFiles.clear()
182+
testFiles.forEach(file => runningFiles.add(file))
183+
174184
const config = getConfig()
175185
const container = await getContainer(config)
176186

packages/browser/src/node/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ export default (project: WorkspaceProject, base = '/'): Plugin[] => {
104104
}
105105

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

111112
if (!testerScripts)

packages/browser/src/node/providers/preview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class PreviewBrowserProvider implements BrowserProvider {
1818
this.ctx = ctx
1919
this.open = false
2020
if (ctx.config.browser.headless)
21-
throw new Error('You\'ve enabled headless mode for "preview" provider but it doesn\'t support it.')
21+
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')
2222
}
2323

2424
async openPage(_url: string) {

packages/ui/client/components/TasksList.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ const filteredTests: ComputedRef<File[]> = computed(() => isFiltered.value ? fil
3636
const failed = computed(() => filtered.value.filter(task => task.result?.state === 'fail'))
3737
const success = computed(() => filtered.value.filter(task => task.result?.state === 'pass'))
3838
const skipped = computed(() => filtered.value.filter(task => task.mode === 'skip' || task.mode === 'todo'))
39-
const running = computed(() => filtered.value.filter(task => !task.result || task.result.state === 'run'))
39+
const running = computed(() => filtered.value.filter(task =>
40+
!failed.value.includes(task)
41+
&& !success.value.includes(task)
42+
&& !skipped.value.includes(task),
43+
))
4044
4145
const disableClearSearch = computed(() => search.value === '')
4246
@@ -128,7 +132,7 @@ function matchTasks(tasks: Task[], search: string): boolean {
128132
:on-item-click="onItemClick"
129133
/>
130134
</DetailsPanel>
131-
<DetailsPanel v-if="running.length || testRunState === 'running'">
135+
<DetailsPanel v-if="throttledRunning.length">
132136
<template #summary>
133137
<div text-yellow5>
134138
RUNNING ({{ throttledRunning.length }})

packages/vitest/src/node/pools/browser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function createBrowserPool(ctx: Vitest): ProcessPool {
1414
files,
1515
resolve: () => {
1616
defer.resolve()
17+
project.browserState = undefined
1718
},
1819
reject: defer.reject,
1920
}

packages/vitest/src/utils/serialization.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ function cloneByOwnProperties(value: any) {
1515
* flatted.stringify().
1616
*/
1717
export function stringifyReplace(key: string, value: any) {
18-
if (value instanceof Error)
19-
return cloneByOwnProperties(value)
20-
else
18+
if (value instanceof Error) {
19+
const cloned = cloneByOwnProperties(value)
20+
return {
21+
name: value.name,
22+
message: value.message,
23+
stack: value.stack,
24+
...cloned,
25+
}
26+
}
27+
else {
2128
return value
29+
}
2230
}

0 commit comments

Comments
 (0)