-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestExecutor.ts
More file actions
56 lines (45 loc) · 1.24 KB
/
TestExecutor.ts
File metadata and controls
56 lines (45 loc) · 1.24 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
import { AssertionError } from "assert";
import { performance } from "perf_hooks";
import { TestResult } from "./TestResult";
import { TestResultState } from "./TestResult";
import { TestFunction } from "./TestFunction";
export class TestExecutor {
async executeTest(
test: TestFunction,
timeoutMs: number
): Promise<TestResult> {
let t0;
try {
t0 = performance.now();
const testTimeout = new Promise((_, reject) => {
const id = setTimeout(() => {
clearTimeout(id);
reject(`Test timed out after ${timeoutMs}ms`);
}, timeoutMs);
});
await Promise.race([test.apply(null), testTimeout]);
const t1 = performance.now();
const time = t1 - t0;
return {
state: TestResultState.PASS,
time,
};
} catch (e) {
const t1 = performance.now();
const time = t1 - (t0 as number);
const isAssertionError = e instanceof AssertionError;
const state = isAssertionError
? TestResultState.FAIL
: TestResultState.ERROR;
const result: TestResult = {
state,
time,
message: e.message,
};
if (!isAssertionError) {
result.error = e;
}
return result;
}
}
}