Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(browser): add vitest/ imports to entries #4514

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading