-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
VestTest.ts
128 lines (104 loc) · 2.47 KB
/
VestTest.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
import genId from 'genId';
import { removePending } from 'pending';
import removeTestFromState from 'removeTestFromState';
import shouldUseErrorAsMessage from 'shouldUseErrorAsMessage';
export default class VestTest {
fieldName: string;
testFn: TTestFn;
asyncTest?: TAsyncTest;
groupName?: string;
message?: string;
id = genId();
warns = false;
status:
| 'UNTESTED'
| 'SKIPPED'
| 'FAILED'
| 'WARNING'
| 'PASSING'
| 'PENDING'
| 'CANCELED' = STATUS_UNTESTED;
constructor(
fieldName: string,
testFn: TTestFn,
{ message, groupName }: { message?: string; groupName?: string } = {}
) {
this.fieldName = fieldName;
this.testFn = testFn;
if (groupName) {
this.groupName = groupName;
}
if (message) {
this.message = message;
}
}
setPending() {
this.status = STATUS_PENDING;
}
run(): TTestResult {
let result: TTestResult;
try {
result = this.testFn();
} catch (error) {
if (shouldUseErrorAsMessage(this.message, error)) {
this.message = error;
}
result = false;
}
if (result === false) {
this.fail();
}
return result;
}
fail(): void {
this.status = this.warns ? STATUS_WARNING : STATUS_FAILED;
}
done(): void {
if (this.isWarning() || this.isCanceled() || this.isFailing()) {
return;
}
this.status = STATUS_PASSING;
}
warn(): void {
this.warns = true;
}
skip(): void {
this.status = STATUS_SKIPPED;
}
cancel(): void {
this.status = STATUS_CANCELED;
removePending(this);
removeTestFromState(this);
}
valueOf(): boolean {
return !this.isFailing();
}
hasFailures(): boolean {
return this.isFailing() || this.isWarning();
}
isFailing(): boolean {
return this.status === STATUS_FAILED;
}
isCanceled(): boolean {
return this.status === STATUS_CANCELED;
}
isSkipped(): boolean {
return this.status === STATUS_SKIPPED;
}
isPassing(): boolean {
return this.status === STATUS_PASSING;
}
isWarning(): boolean {
return this.status === STATUS_WARNING;
}
}
type TAsyncTest = Promise<string | void>;
export type TTestResult = TAsyncTest | boolean | void;
export type TTestFn = () => TTestResult;
const STATUS_UNTESTED = 'UNTESTED';
const STATUS_SKIPPED = 'SKIPPED';
const STATUS_FAILED = 'FAILED';
const STATUS_WARNING = 'WARNING';
const STATUS_PASSING = 'PASSING';
const STATUS_PENDING = 'PENDING';
const STATUS_CANCELED = 'CANCELED';