forked from as-pect/as-pect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestContext.ts
429 lines (375 loc) · 13.8 KB
/
TestContext.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import { TestGroup } from "./TestGroup";
import { TestReporter } from "./TestReporter";
import { TestResult } from "./TestResult";
import VerboseReporter from "../reporter/VerboseReporter";
import { performance } from "perf_hooks";
import { timeDifference } from "../util/timeDifference";
import { IAspectExports } from "../util/IAspectExports";
import { TestCollector, ITestCollectorParameters } from "./TestCollector";
import { IWritable } from "../util/IWriteable";
export interface ITestContextParameters extends ITestCollectorParameters {
reporter?: TestReporter;
stdout?: IWritable;
stderr?: IWritable;
}
export class TestContext extends TestCollector {
public time: number = 0;
public pass: boolean = true;
public startupTime: number = 0;
public reporter: TestReporter = new VerboseReporter({});
/* istanbul ignore next */
public stdout: IWritable | null =
typeof process !== "undefined" ? process.stdout : null;
/* istanbul ignore next */
public stderr: IWritable | null =
typeof process !== "undefined" ? process.stderr : null;
private endGroup: boolean = false;
constructor(props?: ITestContextParameters) {
super(props);
/* istanbul ignore next */
if (props) {
/* istanbul ignore next */
if (props.reporter) this.reporter = props.reporter;
/* istanbul ignore next */
if (props.stdout) this.stdout = props.stdout;
/* istanbul ignore next */
if (props.stderr) this.stderr = props.stderr;
}
}
/**
* Run the tests on the wasm module.
*/
public run(wasm: IAspectExports): void {
// set wasm immediately
this.wasm = wasm;
// start the timer
const start = performance.now();
try {
// start the module up
super.collectTests();
if (!this.rtraceEnabled) {
wasm.__disableRTrace();
}
// calculate startuptime
this.startupTime = timeDifference(performance.now(), start);
} catch (ex) {
/** This skipped line is related to the message coalescing, which is just a fallback. */
/* istanbul ignore next */
this.pushError({
message:
"TestCollectionError: " + (this.message || (ex as Error).message),
stackTrace: this.getErrorStackTrace(ex),
type: "TestCollectionError",
});
}
if (this.errors.length > 0) return;
// start the test suite
this.reporter.onStart(this);
this.runGroup(this.topLevelGroup!);
const end = performance.now();
this.time = timeDifference(end, start);
this.reporter.onFinish(this);
}
private runGroup(group: TestGroup): void {
if (!group.willRun) {
for (const child of group.children) {
this.runGroup(child);
/* istanbul ignore next */
if (this.endGroup) return;
}
}
this.endGroup = false;
if (this.rtraceEnabled) {
// reset all the reference counting properties
this.groupAllocationCount = 0;
this.groupFreeCount = 0;
this.groupDecrementCount = 0;
this.groupIncrementCount = 0;
group.rtraceStart = this.blocks.size;
this.groupBlocks.clear();
}
// set the group starttime
group.start = performance.now();
// set the log target
this.logTarget = group;
// for each beforeAllCallback
this.runBeforeAll(group);
// report the group as started, and log all the beforeAll logs outside the describe block
this.reporter.onGroupStart(group);
if (this.endGroup) return;
for (const result of group.tests) {
this.runTest(group, result);
if (this.endGroup) return;
this.reporter.onTestFinish(group, result);
this.logTarget = group;
}
// run the children
for (const child of group.children) {
this.runGroup(child);
}
// for each afterAllCallback
this.runAfterAll(group);
if (this.endGroup) {
this.pass = false;
group.pass = false;
group.reason = `Test suite ${group.name} failed because of an afterAll() callback occurred.`;
return;
}
if (this.rtraceEnabled) {
// calculate reference counts for the group
group.allocationCount = this.groupAllocationCount;
group.freeCount = this.groupFreeCount;
group.decrementCount = this.groupDecrementCount;
group.incrementCount = this.groupIncrementCount;
group.rtraceEnd = this.blocks.size;
group.rtraceDelta = group.rtraceEnd - group.rtraceStart;
}
// finish the group
group.end = performance.now();
group.time = timeDifference(group.end, group.start);
this.reporter.onGroupFinish(group);
}
/**
* Run a given test.
*
* @param {RunContext} runContext - The current run context.
* @param {TestGroup} group - The current run group.
* @param {number} testIndex - The current test index.
*/
private runTest(group: TestGroup, result: TestResult): void {
// set the log target
this.logTarget = result;
this.reporter.onTestStart(group, result);
result.ran = true;
if (this.rtraceEnabled) {
// reset all the reference counting properties
this.testAllocationCount = 0;
this.testFreeCount = 0;
this.testDecrementCount = 0;
this.testIncrementCount = 0;
result.rtraceStart = this.blocks.size;
this.testBlocks.clear();
}
result.start = performance.now();
// If performance is enabled, use the performance values, otherwise, just run once.
if (result.performance) {
// we must ignore the log values to increase performance execution speed
this.wasm!.__ignoreLogs(1);
let runCount = 0;
const testStartTime = performance.now();
let currentTestRunTime = 0;
// run the test loop
while (true) {
// always run at least once
this.runBeforeEach(group, result);
/**
* Especially because the performance functions are run repeatedly, if an error occurs, assume the
* worst and skip the test group. These functions definitely are assumed to be safe by the test context.
*/
if (this.endGroup) {
this.wasm!.__ignoreLogs(0);
this.wasm!.__cleanup();
this.wasm!.__collect();
return;
}
this.runTestCall(group, result);
this.runAfterEach(group, result);
// check to see if the afterEach functions errored (see above)
if (this.endGroup) {
this.wasm!.__ignoreLogs(0);
this.wasm!.__cleanup();
this.wasm!.__collect();
return;
}
currentTestRunTime = performance.now() - testStartTime; // calculate how long the current test has run
runCount += 1; // increase the run count
if (runCount >= result.maxSamples) {
this.wasm!.__ignoreLogs(0);
break; // if we have reached the max sample count
}
if (currentTestRunTime >= result.maxRuntime) {
this.wasm!.__ignoreLogs(0);
break; // weve collected enough samples and the test is over
}
}
this.wasm!.__ignoreLogs(0);
if (result.calculateAverageValue) result.calculateAverage();
if (result.calculateMaxValue) result.calculateMax();
if (result.calculateMedianValue) result.calculateMedian();
if (result.calculateMinValue) result.calculateMin();
if (result.calculateVarianceValue) result.calculateVariance();
if (result.calculateStandardDeviationValue)
result.calculateStandardDeviation();
} else {
this.runBeforeEach(group, result);
if (this.endGroup) return;
this.runTestCall(group, result);
this.runAfterEach(group, result);
if (this.endGroup) return;
}
if (this.rtraceEnabled) {
// calculate reference counts for the group
result.allocationCount = this.groupAllocationCount;
result.freeCount = this.groupFreeCount;
result.decrementCount = this.groupDecrementCount;
result.incrementCount = this.groupIncrementCount;
result.rtraceEnd = this.blocks.size;
result.rtraceDelta = result.rtraceEnd - result.rtraceStart;
}
/**
* In the chance that some kind of memory error occurs as a result of executing the test,
* this should end the test unsuccesfully. Since most errors that end with some kind of
* runtime error using `unreachable()`, it's impossible to test this branch meaningfully.
* This if statement is ignored because it's a failsafe in case some kind of error occurs
* that doesn't cause an unreachable state.
*/
/* istanbul ignore next */
if (result.errors.length > 0) {
this.pass = false;
result.pass = false;
}
result.end = performance.now();
result.runTime = timeDifference(result.end, result.start);
}
/**
* Run the current test once and collect statistics.
*
* @param {RunContext} runContext - The current run context.
* @param {TestGroup} group - The current test group.
* @param {TestResult} result - The current test result.
* @param {number} testIndex - The current test index.
*/
private runTestCall(group: TestGroup, result: TestResult): void {
const start = performance.now();
const testCallResult = this.tryCall(result.functionPointer);
const end = performance.now();
result.times.push(timeDifference(end, start));
result.pass = result.negated ? testCallResult === 0 : testCallResult === 1;
if (!result.pass) {
this.pass = false;
group.pass = false;
// if it's not negated then set the message, the actual, expected, and stack values
if (!result.negated) {
this.wasm!.__sendActual();
this.wasm!.__sendExpected();
result.actual = this.actual;
result.expected = this.expected;
result.message = this.message;
result.stack = this.stack;
}
}
if (testCallResult === 0) {
this.wasm!.__cleanup();
this.wasm!.__collect();
}
// always clear the values
this.message = "";
this.actual = null;
this.expected = null;
this.stack = "";
}
/**
* Run the afterEach callbacks before running the test.
*
* @param {RunContext} runContext - The current run context.
* @param {TestGroup} group - The current test group.
* @param {TestResult} result - The current test result.
*/
private runAfterEach(group: TestGroup, result: TestResult): void {
if (group.parent) this.runAfterEach(group.parent, result);
// for each afterEach callback function pointer
for (const afterEachCallback of group.afterEachPointers) {
const afterEachResult = this.tryCall(afterEachCallback);
// if afterEach fails
if (afterEachResult === 0) {
this.wasm!.__collect();
this.endGroup = true;
group.end = result.end = performance.now();
group.pass = false;
this.pass = false;
group.reason = `Test suite ${group.name} failed because an error occurred in an afterEach() callback.`;
result.pass = false;
group.time = timeDifference(group.end, group.start);
this.reporter.onTestFinish(group, result);
this.reporter.onGroupFinish(group);
return;
}
}
}
/**
* Run the beforeEach callbacks before running the test.
*
* @param {RunContext} runContext - The current run context.
* @param {TestGroup} group - The current test group.
* @param {TestResult} result - The current test result.
*/
private runBeforeEach(group: TestGroup, result: TestResult): void {
if (group.parent) this.runBeforeEach(group.parent, result);
// for each beforeEach callback function pointer
for (const beforeEachCallback of group.beforeEachPointers) {
const beforeEachResult = this.tryCall(beforeEachCallback);
// if beforeEach fails
if (beforeEachResult === 0) {
this.wasm!.__collect();
result.end = group.end = performance.now();
this.pass = false;
group.pass = false;
group.reason = `Test suite ${group.name} failed because an error occurred in a beforeEach() callback.`;
result.pass = false;
group.time = timeDifference(group.end, group.start);
this.reporter.onTestFinish(group, result);
this.reporter.onGroupFinish(group);
this.endGroup = true;
return;
}
}
}
/**
* Run the beforeAll callbacks with the given runContext and group. This
* method only calls the current group's beforeAll callbacks.
*
* @param {RunContext} runContext - The current run context.
* @param {TestGroup} group - The current test group.
*/
private runAfterAll(group: TestGroup): void {
for (const afterAllCallback of group.afterAllPointers) {
// call each afterAll callback
const afterAllResult = this.tryCall(afterAllCallback);
// if the test fails
if (afterAllResult === 0) {
group.end = performance.now();
this.pass = false;
group.pass = false;
group.reason = `Test suite ${group.name} failed because an error occurred in an afterAll() callback.`;
this.pass = false;
group.time = timeDifference(group.end, group.start);
this.reporter.onGroupFinish(group);
this.endGroup = true;
return;
}
}
}
/**
* Run the beforeAll callbacks with the given runContext and group. This
* method only calls the current group's beforeAll callbacks.
*
* @param {RunContext} runContext - The current run context.
* @param {TestGroup} group - The current test group.
*/
private runBeforeAll(group: TestGroup): void {
for (const beforeAllCallback of group.beforeAllPointers) {
// call each beforeAll callback
const beforeAllResult = this.tryCall(beforeAllCallback);
// if the test fails
if (beforeAllResult === 0) {
group.end = performance.now();
group.pass = false;
group.reason = `Test suite ${group.name} failed because an error occurred in a beforeAll() callback.`;
this.pass = false;
group.time = timeDifference(group.end, group.start);
this.endGroup = true;
return;
}
}
}
}