Skip to content

Commit 34a310d

Browse files
authored
fix(browser): use iframe id instead of calculating it from filenames (#5823)
1 parent f814aef commit 34a310d

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

packages/browser/src/client/orchestrator.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ async function done() {
5858
interface IframeDoneEvent {
5959
type: 'done'
6060
filenames: string[]
61+
id: string
6162
}
6263

6364
interface IframeErrorEvent {
6465
type: 'error'
6566
error: any
6667
errorType: string
6768
files: string[]
69+
id: string
6870
}
6971

7072
interface IframeViewportEvent {
@@ -133,15 +135,15 @@ client.ws.addEventListener('open', async () => {
133135
}
134136
else {
135137
// keep the last iframe
136-
const iframeId = filenames.length > 1 ? ID_ALL : filenames[0]
138+
const iframeId = e.data.id
137139
iframes.get(iframeId)?.remove()
138140
iframes.delete(iframeId)
139141
}
140142
break
141143
}
142144
// error happened at the top level, this should never happen in user code, but it can trigger during development
143145
case 'error': {
144-
const iframeId = e.data.files.length > 1 ? ID_ALL : e.data.files[0]
146+
const iframeId = e.data.id
145147
iframes.delete(iframeId)
146148
await client.rpc.onUnhandledError(e.data.error, e.data.errorType)
147149
if (iframeId === ID_ALL)
@@ -178,6 +180,9 @@ async function createTesters(testFiles: string[]) {
178180
}
179181
const { width, height } = config.browser.viewport
180182

183+
iframes.forEach(iframe => iframe.remove())
184+
iframes.clear()
185+
181186
if (config.isolate === false) {
182187
const iframe = createIframe(
183188
container,

packages/browser/src/client/tester.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ async function prepareTestEnvironment(files: string[]) {
140140
}
141141

142142
function done(files: string[]) {
143-
channel.postMessage({ type: 'done', filenames: files })
143+
channel.postMessage({
144+
type: 'done',
145+
filenames: files,
146+
id: getBrowserState().iframeId!,
147+
})
144148
}
145149

146150
async function runTests(files: string[]) {

packages/browser/src/client/unhandled.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ export function serializeError(unhandledError: any) {
1919
// we can't import "processError" yet because error might've been thrown before the module was loaded
2020
async function defaultErrorReport(type: string, unhandledError: any) {
2121
const error = serializeError(unhandledError)
22-
channel.postMessage({ type: 'error', files: getBrowserState().runningFiles, error, errorType: type })
22+
channel.postMessage({
23+
type: 'error',
24+
files: getBrowserState().runningFiles,
25+
error,
26+
errorType: type,
27+
id: getBrowserState().iframeId!,
28+
})
2329
}
2430

2531
function catchWindowErrors(cb: (e: ErrorEvent) => void) {

packages/browser/src/node/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export default (project: WorkspaceProject, base = '/'): Plugin[] => {
106106
const decodedTestFile = decodeURIComponent(url.pathname.slice(testerPrefix.length))
107107
// if decoded test file is "__vitest_all__" or not in the list of known files, run all tests
108108
const tests = decodedTestFile === '__vitest_all__' || !files.includes(decodedTestFile) ? '__vitest_browser_runner__.files' : JSON.stringify([decodedTestFile])
109-
const iframeId = decodedTestFile === '__vitest_all__' ? '"__vitest_all__"' : JSON.stringify(decodedTestFile)
109+
const iframeId = JSON.stringify(decodedTestFile)
110110

111111
if (!testerScripts)
112112
testerScripts = await formatScripts(project.config.browser.testerScripts, server)

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

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export function createBrowserPool(ctx: Vitest): ProcessPool {
1414
files,
1515
resolve: () => {
1616
defer.resolve()
17-
project.browserState = undefined
1817
},
1918
reject: defer.reject,
2019
}

0 commit comments

Comments
 (0)