/
state.ts
157 lines (133 loc) · 4.08 KB
/
state.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { relative } from 'pathe'
import type { File, Task, TaskResultPack, UserConsoleLog } from '../types'
// can't import actual functions from utils, because it's incompatible with @vitest/browsers
import type { AggregateError as AggregateErrorPonyfill } from '../utils'
import type { WorkspaceProject } from './workspace'
interface CollectingPromise {
promise: Promise<void>
resolve: () => void
}
export function isAggregateError(err: unknown): err is AggregateErrorPonyfill {
if (typeof AggregateError !== 'undefined' && err instanceof AggregateError)
return true
return err instanceof Error && 'errors' in err
}
// Note this file is shared for both node and browser, be aware to avoid node specific logic
export class StateManager {
filesMap = new Map<string, File[]>()
pathsSet: Set<string> = new Set()
collectingPromise: CollectingPromise | undefined = undefined
browserTestPromises = new Map<string, { resolve: (v: unknown) => void; reject: (v: unknown) => void }>()
idMap = new Map<string, Task>()
taskFileMap = new WeakMap<Task, File>()
errorsSet = new Set<unknown>()
processTimeoutCauses = new Set<string>()
catchError(err: unknown, type: string): void {
if (isAggregateError(err))
return err.errors.forEach(error => this.catchError(error, type))
if (err === Object(err))
(err as Record<string, unknown>).type = type
else
err = { type, message: err }
this.errorsSet.add(err)
}
clearErrors() {
this.errorsSet.clear()
}
getUnhandledErrors() {
return Array.from(this.errorsSet.values())
}
addProcessTimeoutCause(cause: string) {
this.processTimeoutCauses.add(cause)
}
getProcessTimeoutCauses() {
return Array.from(this.processTimeoutCauses.values())
}
getPaths() {
return Array.from(this.pathsSet)
}
getFiles(keys?: string[]): File[] {
if (keys)
return keys.map(key => this.filesMap.get(key)!).filter(Boolean).flat()
return Array.from(this.filesMap.values()).flat()
}
getFilepaths(): string[] {
return Array.from(this.filesMap.keys())
}
getFailedFilepaths() {
return this.getFiles()
.filter(i => i.result?.state === 'fail')
.map(i => i.filepath)
}
collectPaths(paths: string[] = []) {
paths.forEach((path) => {
this.pathsSet.add(path)
})
}
collectFiles(files: File[] = []) {
files.forEach((file) => {
const existing = (this.filesMap.get(file.filepath) || [])
const otherProject = existing.filter(i => i.projectName !== file.projectName)
otherProject.push(file)
this.filesMap.set(file.filepath, otherProject)
this.updateId(file)
})
}
clearFiles(project: WorkspaceProject, paths: string[] = []) {
paths.forEach((path) => {
const files = this.filesMap.get(path)
if (!files)
return
const filtered = files.filter(file => file.projectName !== project.config.name)
if (!filtered.length)
this.filesMap.delete(path)
else
this.filesMap.set(path, filtered)
})
}
updateId(task: Task) {
if (this.idMap.get(task.id) === task)
return
this.idMap.set(task.id, task)
if (task.type === 'suite') {
task.tasks.forEach((task) => {
this.updateId(task)
})
}
}
updateTasks(packs: TaskResultPack[]) {
for (const [id, result, meta] of packs) {
const task = this.idMap.get(id)
if (task) {
task.result = result
task.meta = meta
}
}
}
updateUserLog(log: UserConsoleLog) {
const task = log.taskId && this.idMap.get(log.taskId)
if (task) {
if (!task.logs)
task.logs = []
task.logs.push(log)
}
}
getCountOfFailedTests() {
return Array.from(this.idMap.values()).filter(t => t.result?.state === 'fail').length
}
cancelFiles(files: string[], root: string) {
this.collectFiles(files.map(filepath => ({
filepath,
name: relative(root, filepath),
id: filepath,
mode: 'skip',
type: 'suite',
result: {
state: 'skip',
},
meta: {},
// Cancelled files have not yet collected tests
tasks: [],
})))
}
}