/
runner.ts
59 lines (50 loc) · 1.63 KB
/
runner.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
import type { File, TaskResult, Test } from '@vitest/runner'
import { rpc } from './rpc'
import type { ResolvedConfig } from '#types'
interface BrowserRunnerOptions {
config: ResolvedConfig
browserHashMap: Map<string, string>
}
interface CoverageHandler {
takeCoverage: () => Promise<unknown>
}
export function createBrowserRunner(original: any, coverageModule: CoverageHandler | null) {
return class BrowserTestRunner extends original {
public config: ResolvedConfig
hashMap = new Map<string, string>()
constructor(options: BrowserRunnerOptions) {
super(options.config)
this.config = options.config
this.hashMap = options.browserHashMap
}
async onAfterRunTest(task: Test) {
await super.onAfterRunTest?.()
task.result?.errors?.forEach((error) => {
console.error(error.message)
})
}
async onAfterRunSuite() {
await super.onAfterRunSuite?.()
const coverage = await coverageModule?.takeCoverage?.()
await rpc().onAfterSuiteRun({ coverage })
}
onCollected(files: File[]): unknown {
return rpc().onCollected(files)
}
onTaskUpdate(task: [string, TaskResult | undefined][]): Promise<void> {
return rpc().onTaskUpdate(task)
}
async importFile(filepath: string) {
const match = filepath.match(/^(\w:\/)/)
let hash = this.hashMap.get(filepath)
if (!hash) {
hash = Date.now().toString()
this.hashMap.set(filepath, hash)
}
const importpath = match
? `/@fs/${filepath.slice(match[1].length)}?v=${hash}`
: `${filepath}?v=${hash}`
await import(importpath)
}
}
}