Repository navigation
Expand file tree
/
Copy pathrunner.ts
More file actions
158 lines (145 loc) · 4.17 KB
/
Copy pathrunner.ts
File metadata and controls
158 lines (145 loc) · 4.17 KB
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
import type {
CancelReason,
File,
Task,
TaskEventPack,
TaskResultPack,
VitestRunner,
VitestRunnerConfig,
} from '@vitest/runner';
import type { WorkerGlobalState } from 'vitest';
import type {
NativeScriptTestDescriptor,
NativeScriptTestEvent,
NativeScriptTestState,
} from '../protocol.js';
import type { NativeScriptTestModuleRegistry } from './registry.js';
type EventSink = (event: NativeScriptTestEvent) => void;
function initialState(task: Task): NativeScriptTestState {
if (task.mode === 'skip') return 'skipped';
if (task.mode === 'todo') return 'todo';
return 'queued';
}
function resultState(state: string | undefined): NativeScriptTestState {
switch (state) {
case 'pass':
return 'passed';
case 'fail':
return 'failed';
case 'skip':
return 'skipped';
case 'todo':
return 'todo';
case 'run':
return 'running';
default:
return 'queued';
}
}
function errorMessage(value: unknown): string | undefined {
if (typeof value === 'object' && value !== null && 'message' in value) {
return String(value.message);
}
return value === undefined ? undefined : String(value);
}
function collectTestDescriptors(
task: Task,
output: NativeScriptTestDescriptor[],
): void {
if (task.type === 'test') {
output.push({
id: task.id,
name: task.name,
fullName: task.fullName,
file: task.file.filepath,
state: initialState(task),
});
return;
}
task.tasks.forEach((child) => collectTestDescriptors(child, output));
}
export class NativeScriptDeviceRunner implements VitestRunner {
cancel?: (reason: CancelReason) => void;
constructor(
public readonly config: VitestRunnerConfig,
private readonly worker: number,
private readonly registry: NativeScriptTestModuleRegistry,
private readonly state: WorkerGlobalState,
private readonly emit: EventSink,
/**
* Shared across runner instances: Vitest's throttled task updates may
* flush after `startTests` resolves, landing while a later file's runner
* is active — descriptors collected by one instance must stay resolvable
* for packs processed by another, or those updates silently vanish from
* the on-device stream.
*/
private readonly tests: Map<string, NativeScriptTestDescriptor> = new Map(),
) {
state.onCancel((reason) => this.cancel?.(reason));
}
importFile(filepath: string): unknown {
return this.registry.load(filepath);
}
onBeforeRunFiles(files: File[]): void {
this.emit({
type: 'worker-run-started',
worker: this.worker,
files: files.map((file) => file.filepath),
timestamp: Date.now(),
});
}
onCleanupWorkerContext(cleanup: () => unknown): void {
this.state.onCleanup(cleanup);
}
async onCollected(files: File[]): Promise<void> {
const descriptors: NativeScriptTestDescriptor[] = [];
files.forEach((file) => collectTestDescriptors(file, descriptors));
descriptors.forEach((test) => this.tests.set(test.id, test));
this.emit({
type: 'tests-collected',
worker: this.worker,
tests: descriptors,
});
await this.state.rpc.onCollected(files);
}
async onTaskUpdate(
packs: TaskResultPack[],
events: TaskEventPack[],
): Promise<void> {
for (const [id, result] of packs) {
const previous = this.tests.get(id);
if (!result) continue;
if (!previous) {
const error = errorMessage(result.errors?.[0]);
if (result.state === 'fail' && error) {
this.emit({
type: 'worker-error',
worker: this.worker,
message: error,
});
}
continue;
}
const test: NativeScriptTestDescriptor = {
...previous,
state: resultState(result.state),
duration: result.duration,
error: errorMessage(result.errors?.[0]),
};
this.tests.set(id, test);
this.emit({
type: 'test-updated',
worker: this.worker,
test,
});
}
await this.state.rpc.onTaskUpdate(packs, events);
}
finishRun(): void {
this.emit({
type: 'worker-run-finished',
worker: this.worker,
timestamp: Date.now(),
});
}
}