-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathtest-provider-helper.ts
200 lines (176 loc) · 5.9 KB
/
test-provider-helper.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import * as vscode from 'vscode';
import { JestExtOutput, JestOutputTerminal, OutputOptions } from '../JestExt/output-terminal';
import { JestExtExplorerContext, TestItemData } from './types';
/**
* provide context information from JestExt and test provider state:
* 1. TestData <-> TestItem
*
* as well as factory functions to create TestItem and TestRun that could impact the state
*/
export type TagIdType = 'run' | 'debug' | 'update-snapshot';
let RunSeq = 0;
export class JestTestProviderContext {
private testItemData: WeakMap<vscode.TestItem, TestItemData>;
constructor(
public readonly ext: JestExtExplorerContext,
private readonly controller: vscode.TestController,
private readonly profiles: vscode.TestRunProfile[]
) {
this.testItemData = new WeakMap();
}
get output(): JestOutputTerminal {
return this.ext.output;
}
createTestItem = (
id: string,
label: string,
uri: vscode.Uri,
data: TestItemData,
parent?: vscode.TestItem,
tagIds: TagIdType[] = ['run', 'debug']
): vscode.TestItem => {
const testItem = this.controller.createTestItem(id, label, uri);
this.testItemData.set(testItem, data);
const collection = parent ? parent.children : this.controller.items;
collection.add(testItem);
tagIds?.forEach((tId) => {
const tag = this.getTag(tId);
if (tag) {
testItem.tags = [...testItem.tags, tag];
}
});
return testItem;
};
/**
* check if there is such child in the item, if exists returns the associated data
*
* @param item
* @param childId id of the child item
* @returns data of the child item, casting for easy usage but does not guarantee type safety.
*/
getChildData = <T extends TestItemData = TestItemData>(
item: vscode.TestItem,
childId: string
): T | undefined => {
const cItem = item.children.get(childId);
// Note: casting for easy usage but does not guarantee type safety.
return cItem && (this.testItemData.get(cItem) as T);
};
/**
* get data associated with the item. All item used here should have some data associated with, otherwise
* an exception will be thrown
*
* @returns casting for easy usage but does not guarantee type safety
*/
getData = <T extends TestItemData>(item: vscode.TestItem): T | undefined => {
// Note: casting for easy usage but does not guarantee type safety.
return this.testItemData.get(item) as T | undefined;
};
createTestRun = (request: vscode.TestRunRequest, options?: JestTestRunOptions): JestTestRun => {
const name = options?.name ?? `run-${RunSeq++}`;
const opt = { ...(options ?? {}), name };
const vscodeRun = this.controller.createTestRun(request, name);
return new JestTestRun(this, vscodeRun, opt);
};
// tags
getTag = (tagId: TagIdType): vscode.TestTag => {
const tag = this.profiles.find((p) => p.tag?.id === tagId)?.tag;
if (!tag) {
throw new Error(`unrecognized tag: ${tagId}`);
}
return tag;
};
}
export interface JestTestRunOptions {
name?: string;
item?: vscode.TestItem;
// in addition to the regular end() method
onEnd?: () => void;
// replace the end function
end?: () => void;
}
export type TestRunProtocol = Pick<
vscode.TestRun,
'name' | 'enqueued' | 'started' | 'errored' | 'failed' | 'passed' | 'skipped' | 'end'
>;
export type ParentRun = vscode.TestRun | JestTestRun;
const isVscodeRun = (arg: ParentRun | undefined): arg is vscode.TestRun =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
arg != null && typeof (arg as any).appendOutput === 'function';
/** a wrapper for vscode.TestRun or another JestTestRun */
export class JestTestRun implements JestExtOutput, TestRunProtocol {
private output: JestOutputTerminal;
public item?: vscode.TestItem;
private parentRun?: ParentRun;
constructor(
context: JestTestProviderContext,
parentRun: ParentRun,
private options?: JestTestRunOptions
) {
this.parentRun = parentRun;
this.output = context.output;
this.item = options?.item;
}
get vscodeRun(): vscode.TestRun | undefined {
if (!this.parentRun) {
return;
}
if (isVscodeRun(this.parentRun)) {
return this.parentRun;
}
return this.parentRun.vscodeRun;
}
write(msg: string, opt?: OutputOptions): string {
const text = this.output.write(msg, opt);
this.vscodeRun?.appendOutput(text);
return text;
}
isClosed(): boolean {
return this.vscodeRun === undefined;
}
private updateState = (f: (pRun: ParentRun) => void): void => {
if (!this.parentRun || !this.vscodeRun) {
throw new Error(`run "${this.name}" has already closed`);
}
f(this.parentRun);
};
// TestRunProtocol
public get name(): string | undefined {
return this.options?.name;
}
public enqueued = (test: vscode.TestItem): void => {
this.updateState((pRun) => pRun.enqueued(test));
};
public started = (test: vscode.TestItem): void => {
this.updateState((pRun) => pRun.started(test));
};
public errored = (
test: vscode.TestItem,
message: vscode.TestMessage | readonly vscode.TestMessage[],
duration?: number | undefined
): void => {
this.updateState((pRun) => pRun.errored(test, message, duration));
};
public failed = (
test: vscode.TestItem,
message: vscode.TestMessage | readonly vscode.TestMessage[],
duration?: number | undefined
): void => {
this.updateState((pRun) => pRun.failed(test, message, duration));
};
public passed = (test: vscode.TestItem, duration?: number | undefined): void => {
this.updateState((pRun) => pRun.passed(test, duration));
};
public skipped = (test: vscode.TestItem): void => {
this.updateState((pRun) => pRun.skipped(test));
};
public end = (): void => {
if (this.options?.end) {
return this.options.end();
}
if (this.parentRun && isVscodeRun(this.parentRun)) {
this.parentRun = undefined;
}
this.options?.onEnd?.();
};
}