/
browser.ts
88 lines (77 loc) · 2.6 KB
/
browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { createDefer } from '@vitest/utils'
import { relative } from 'pathe'
import type { Vitest } from '../core'
import type { ProcessPool } from '../pool'
import type { WorkspaceProject } from '../workspace'
import type { BrowserProvider } from '../../types/browser'
export function createBrowserPool(ctx: Vitest): ProcessPool {
const providers = new Set<BrowserProvider>()
const waitForTest = async (provider: BrowserProvider, id: string) => {
const defer = createDefer()
ctx.state.browserTestPromises.set(id, defer)
const off = provider.catchError((error) => {
if (id !== 'no-isolate') {
Object.defineProperty(error, 'VITEST_TEST_PATH', {
value: id,
})
}
defer.reject(error)
})
try {
return await defer
}
finally {
off()
}
}
const runTests = async (project: WorkspaceProject, files: string[]) => {
ctx.state.clearFiles(project, files)
let isCancelled = false
project.ctx.onCancel(() => {
isCancelled = true
})
const provider = project.browserProvider!
providers.add(provider)
const origin = `http://${ctx.config.browser.api?.host || 'localhost'}:${project.browser.config.server.port}`
const paths = files.map(file => relative(project.config.root, file))
const isolate = project.config.isolate
if (isolate) {
for (const path of paths) {
if (isCancelled) {
ctx.state.cancelFiles(files.slice(paths.indexOf(path)), ctx.config.root)
break
}
const url = new URL('/', origin)
url.searchParams.append('path', path)
url.searchParams.set('id', path)
await provider.openPage(url.toString())
await waitForTest(provider, path)
}
}
else {
const url = new URL('/', origin)
url.searchParams.set('id', 'no-isolate')
paths.forEach(path => url.searchParams.append('path', path))
await provider.openPage(url.toString())
await waitForTest(provider, 'no-isolate')
}
}
const runWorkspaceTests = async (specs: [WorkspaceProject, string][]) => {
const groupedFiles = new Map<WorkspaceProject, string[]>()
for (const [project, file] of specs) {
const files = groupedFiles.get(project) || []
files.push(file)
groupedFiles.set(project, files)
}
for (const [project, files] of groupedFiles.entries())
await runTests(project, files)
}
return {
async close() {
ctx.state.browserTestPromises.clear()
await Promise.all([...providers].map(provider => provider.close()))
providers.clear()
},
runTests: runWorkspaceTests,
}
}