Skip to content

Commit

Permalink
fix(browser): add vitest/ imports to entries (#4514)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Nov 16, 2023
1 parent b92f108 commit 648bccb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
24 changes: 23 additions & 1 deletion packages/browser/src/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<ReturnType<typeof prepareTestEnvironment>> | 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)
})
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/client/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// it's possible that import was not optimized yet
async function tryImport(id: string, tries = 10): Promise<any> {
async function tryImport(id: string, tries = 20): Promise<any> {
try {
return await import(id)
}
Expand Down
7 changes: 6 additions & 1 deletion packages/browser/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 648bccb

Please sign in to comment.