diff --git a/packages/browser/src/client/main.ts b/packages/browser/src/client/main.ts index 778cff644e97..0780333421f8 100644 --- a/packages/browser/src/client/main.ts +++ b/packages/browser/src/client/main.ts @@ -135,7 +135,7 @@ ws.addEventListener('open', async () => { await runTests(paths, config!) }) -async function runTests(paths: string[], config: ResolvedConfig) { +async function prepareTestEnvironment(config: ResolvedConfig) { // need to import it before any other import, otherwise Vite optimizer will hang const viteClientPath = '/@vite/client' await import(viteClientPath) @@ -157,6 +157,28 @@ async function runTests(paths: string[], config: ResolvedConfig) { runner = new BrowserRunner({ config, browserHashMap }) } + return { + startTests, + setupCommonEnv, + loadDiffConfig, + executor, + runner, + } +} + +async function runTests(paths: string[], config: ResolvedConfig) { + let preparedData: Awaited> | undefined + // if importing /@id/ failed, we reload the page waiting until Vite prebundles it + try { + preparedData = await prepareTestEnvironment(config) + } + catch (err) { + location.reload() + return + } + + const { startTests, setupCommonEnv, loadDiffConfig, executor, runner } = preparedData! + onCancel.then((reason) => { runner?.onCancel?.(reason) }) diff --git a/packages/browser/src/client/utils.ts b/packages/browser/src/client/utils.ts index aa9bebe46368..b79f353d319e 100644 --- a/packages/browser/src/client/utils.ts +++ b/packages/browser/src/client/utils.ts @@ -1,5 +1,5 @@ // it's possible that import was not optimized yet -async function tryImport(id: string, tries = 10): Promise { +async function tryImport(id: string, tries = 20): Promise { try { return await import(id) } diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index 696eebfc3999..448665992c5d 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -47,7 +47,12 @@ export default (project: WorkspaceProject, base = '/'): Plugin[] => { const entries = await project.globAllTestFiles(include, exclude, includeSource, projectRoot) return { optimizeDeps: { - entries, + entries: [ + ...entries, + 'vitest/utils', + 'vitest/browser', + 'vitest/runners', + ], exclude: [ ...builtinModules, 'vitest',