-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
runner.ts
70 lines (58 loc) · 2.09 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
60
61
62
63
64
65
66
67
68
69
70
import type { File, TaskResultPack, Test } from '@vitest/runner'
import { rpc } from './rpc'
import type { ResolvedConfig } from '#types'
interface BrowserRunnerOptions {
config: ResolvedConfig
browserHashMap: Map<string, [test: boolean, timstamp: 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, [test: boolean, timstamp: string]>()
constructor(options: BrowserRunnerOptions) {
super(options.config)
this.config = options.config
this.hashMap = options.browserHashMap
}
async onAfterRunTest(task: Test) {
await super.onAfterRunTest?.(task)
task.result?.errors?.forEach((error) => {
console.error(error.message)
})
if (this.config.bail && task.result?.state === 'fail') {
const previousFailures = await rpc().getCountOfFailedTests()
const currentFailures = 1 + previousFailures
if (currentFailures >= this.config.bail) {
rpc().onCancel('test-failure')
this.onCancel?.('test-failure')
}
}
}
async onAfterRunSuite() {
await super.onAfterRunSuite?.()
const coverage = await coverageModule?.takeCoverage?.()
await rpc().onAfterSuiteRun({ coverage })
}
onCollected(files: File[]): unknown {
return rpc().onCollected(files)
}
onTaskUpdate(task: TaskResultPack[]): Promise<void> {
return rpc().onTaskUpdate(task)
}
async importFile(filepath: string) {
let [test, hash] = this.hashMap.get(filepath) ?? [false, '']
if (hash === '') {
hash = Date.now().toString()
this.hashMap.set(filepath, [false, hash])
}
// on Windows we need the unit to resolve the test file
const importpath = /^\w:/.test(filepath)
? `/@fs/${filepath}?${test ? 'browserv' : 'v'}=${hash}`
: `${filepath}?${test ? 'browserv' : 'v'}=${hash}`
await import(importpath)
}
}
}