diff --git a/packages/browser/src/client/orchestrator.ts b/packages/browser/src/client/orchestrator.ts index 5c9afed05581..98e3f0beba88 100644 --- a/packages/browser/src/client/orchestrator.ts +++ b/packages/browser/src/client/orchestrator.ts @@ -58,6 +58,7 @@ async function done() { interface IframeDoneEvent { type: 'done' filenames: string[] + id: string } interface IframeErrorEvent { @@ -65,6 +66,7 @@ interface IframeErrorEvent { error: any errorType: string files: string[] + id: string } interface IframeViewportEvent { @@ -133,7 +135,7 @@ client.ws.addEventListener('open', async () => { } else { // keep the last iframe - const iframeId = filenames.length > 1 ? ID_ALL : filenames[0] + const iframeId = e.data.id iframes.get(iframeId)?.remove() iframes.delete(iframeId) } @@ -141,7 +143,7 @@ client.ws.addEventListener('open', async () => { } // error happened at the top level, this should never happen in user code, but it can trigger during development case 'error': { - const iframeId = e.data.files.length > 1 ? ID_ALL : e.data.files[0] + const iframeId = e.data.id iframes.delete(iframeId) await client.rpc.onUnhandledError(e.data.error, e.data.errorType) if (iframeId === ID_ALL) @@ -178,6 +180,9 @@ async function createTesters(testFiles: string[]) { } const { width, height } = config.browser.viewport + iframes.forEach(iframe => iframe.remove()) + iframes.clear() + if (config.isolate === false) { const iframe = createIframe( container, diff --git a/packages/browser/src/client/tester.ts b/packages/browser/src/client/tester.ts index 1d0c02c84809..acee61f27341 100644 --- a/packages/browser/src/client/tester.ts +++ b/packages/browser/src/client/tester.ts @@ -140,7 +140,11 @@ async function prepareTestEnvironment(files: string[]) { } function done(files: string[]) { - channel.postMessage({ type: 'done', filenames: files }) + channel.postMessage({ + type: 'done', + filenames: files, + id: getBrowserState().iframeId!, + }) } async function runTests(files: string[]) { diff --git a/packages/browser/src/client/unhandled.ts b/packages/browser/src/client/unhandled.ts index 4dcaa527148a..e94efe7c68d6 100644 --- a/packages/browser/src/client/unhandled.ts +++ b/packages/browser/src/client/unhandled.ts @@ -19,7 +19,13 @@ export function serializeError(unhandledError: any) { // we can't import "processError" yet because error might've been thrown before the module was loaded async function defaultErrorReport(type: string, unhandledError: any) { const error = serializeError(unhandledError) - channel.postMessage({ type: 'error', files: getBrowserState().runningFiles, error, errorType: type }) + channel.postMessage({ + type: 'error', + files: getBrowserState().runningFiles, + error, + errorType: type, + id: getBrowserState().iframeId!, + }) } function catchWindowErrors(cb: (e: ErrorEvent) => void) { diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index 297bac8bf614..ed128d26bb53 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -106,7 +106,7 @@ export default (project: WorkspaceProject, base = '/'): Plugin[] => { const decodedTestFile = decodeURIComponent(url.pathname.slice(testerPrefix.length)) // 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 iframeId = decodedTestFile === '__vitest_all__' ? '"__vitest_all__"' : JSON.stringify(decodedTestFile) + const iframeId = JSON.stringify(decodedTestFile) if (!testerScripts) testerScripts = await formatScripts(project.config.browser.testerScripts, server) diff --git a/packages/vitest/src/node/pools/browser.ts b/packages/vitest/src/node/pools/browser.ts index 3d0e2b2c1977..367e8ac249d8 100644 --- a/packages/vitest/src/node/pools/browser.ts +++ b/packages/vitest/src/node/pools/browser.ts @@ -14,7 +14,6 @@ export function createBrowserPool(ctx: Vitest): ProcessPool { files, resolve: () => { defer.resolve() - project.browserState = undefined }, reject: defer.reject, }