-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
worker.ts
165 lines (143 loc) · 3.8 KB
/
worker.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
158
159
160
161
162
163
164
165
import v8 from 'node:v8'
import { register } from 'node:module'
import { dirname } from 'pathe'
import { parseErrorStacktrace } from '@vitest/utils/source-map'
import type { BirpcReturn } from 'birpc'
import type { File, Reporter, TaskResultPack, UserConsoleLog, Vitest } from 'vitest'
import type { BirpcEvents, BirpcMethods } from '../api/rpc'
import { createWorkerRPC } from './rpc'
interface VitestMeta {
folder: string
vitestNodePath: string
id: string
configFile?: string
workspaceFile?: string
env: Record<string, any> | undefined
}
interface RunnerOptions {
type: 'init'
meta: VitestMeta[]
loader?: string
}
class VSCodeReporter implements Reporter {
private rpc!: BirpcReturn<BirpcEvents, BirpcMethods>
private ctx!: Vitest
private folder!: string
initVitest(ctx: Vitest) {
this.ctx = ctx
this.folder = ctx.config.root
}
initRpc(rpc: BirpcReturn<BirpcEvents, BirpcMethods>) {
this.rpc = rpc
}
onUserConsoleLog(log: UserConsoleLog) {
this.rpc.onConsoleLog(this.folder, log)
}
onTaskUpdate(packs: TaskResultPack[]) {
packs.forEach(([taskId, result]) => {
const project = this.ctx.getProjectByTaskId(taskId)
result?.errors?.forEach((error) => {
if (typeof error === 'object' && error) {
error.stacks = parseErrorStacktrace(error, {
getSourceMap: file => project.getBrowserSourceMapModuleById(file),
})
}
})
})
this.rpc.onTaskUpdate(this.folder, packs)
}
onFinished(files?: File[], errors?: unknown[]) {
this.rpc.onFinished(this.folder, files, errors)
}
onCollected(files?: File[]) {
this.rpc.onCollected(this.folder, files)
}
onWatcherStart(files?: File[], errors?: unknown[]) {
this.rpc.onWatcherStart(this.folder, files, errors)
}
onWatcherRerun(files: string[], trigger?: string) {
this.rpc.onWatcherRerun(this.folder, files, trigger)
}
}
async function initVitest(meta: VitestMeta) {
const vitestMode = await import(meta.vitestNodePath) as typeof import('vitest/node')
const reporter = new VSCodeReporter()
const vitest = await vitestMode.createVitest(
'test',
{
config: meta.configFile,
workspace: meta.workspaceFile,
watch: true,
api: false,
root: dirname(meta.id),
reporters: [reporter],
ui: false,
env: meta.env,
includeTaskLocation: true,
},
{
server: {
middlewareMode: true,
},
},
)
reporter.initVitest(vitest)
return {
vitest,
reporter,
}
}
const cwd = process.cwd()
process.on('message', async function init(message: any) {
if (message.type === 'init') {
process.off('message', init)
const data = message as RunnerOptions
try {
if (data.loader)
register(data.loader)
const errors = []
const vitest = []
for (const meta of data.meta) {
process.chdir(dirname(meta.id))
try {
vitest.push(await initVitest(meta))
}
catch (err: any) {
errors.push([meta.configFile, err.stack])
}
}
process.chdir(cwd)
if (!vitest.length) {
process.send!({ type: 'error', errors })
return
}
const rpc = createWorkerRPC(vitest.map(v => v.vitest), {
on(listener) {
process.on('message', listener)
},
post(message) {
process.send!(message)
},
serialize: v8.serialize,
deserialize: v => v8.deserialize(Buffer.from(v)),
})
vitest.forEach(v => v.reporter.initRpc(rpc))
process.send!({ type: 'ready', errors })
}
catch (err: any) {
error(err)
}
}
})
function error(err: any) {
process.send!({
type: 'error',
errors: ['', String(err.stack)],
})
}
function _debug(...args: any[]) {
process.send!({
type: 'debug',
args,
})
}