-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
180 lines (142 loc) · 4.38 KB
/
index.js
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
//import with relative paths to shim for browser
//this way the same code will work here as it does
import ansi from '../ansi-colors-es6/index.js';
import Is from '../strong-type/index.js';
class VanillaTest{
constructor(){
}
get is(){
return this.#is;
}
get compare(){
return this.#is.compare;
}
get throw(){
return this.#is.throw;
}
get strict(){
return this.#is.strict;
}
set strict(strict){
this.#is.boolean(strict);
return this.#is.strict=strict;
}
expects(description){
this.#is.string(description);
if(this.#test){
throw new ReferenceError(
`${ansi.red(this.#test)} is not complete. So ${ansi.red(description)} can not be started.`
);
}
this.#test=`${
this.#failed.length+this.#passed.length+1
}) .expects ${description}`;
console.log(`\n${ansi.bgBlack.white(this.#test)}`);
if(this.#failed.includes(this.#test)
||this.#passed.includes(this.#test)
){
this.#test='';
throw new ReferenceError(
`vanilla-test expects test descriptors to be unique
${ansi.red(this.#test)} has already been declared and run.
Please consider a more descriptive and clear test name.`
);
}
return this.#test;
}
pass(strict=false){
this.#is.boolean(strict);
if(this.#passed.includes(this.#test)
|| this.#failed.includes(this.#test)
){
if(strict){
throw new ReferenceError(
`${ansi.red(this.#test)} has already passed or failed and is waiting for .done()
it can not pass or fail again.`
);
}
return this.#test;
}
this.#passed.push(this.#test);
console.log(
ansi.greenBright(` pass\n`)
);
return this.#test;
}
fail(strict=false){
this.#is.boolean(strict);
if(this.#passed.includes(this.#test)
|| this.#failed.includes(this.#test)
){
if(strict){
throw new ReferenceError(
`${ansi.red(this.#test)} has already passed or failed and is waiting for .done()
It can not pass or fail again.`
);
}
return this.#test;
}
this.#failed.push(this.#test);
console.log(
ansi.redBright(` fail\n`)
);
return this.#test;
}
done(){
//if neither passed nor failed, it will now fail
if(!this.#passed.includes(this.#test)
&& !this.#failed.includes(this.#test)
){
this.failed();
}
const test=this.#test;
this.#test='';
return test;
}
report(CI=true){
let report=`
Result : ${
this.#failed.length? ansi.redBright('FAILED'):ansi.greenBright('PASSED')
}
Test Total : ${this.#passed.length+this.#failed.length}
${ansi.greenBright('Passed :')} ${this.#passed.length}
${ansi.redBright('Failed :')} ${this.#failed.length}\n`;
report+=ansi.bgRedBright.black('\nFAILED TESTS :\n');
for(let test of this.#failed){
report+=ansi.bgBlack.redBright(`${test}\n`);
}
report+=ansi.bgGreenBright.black('\nPASSED TESTS :\n');
for(let test of this.#passed){
report+=ansi.bgBlack.greenBright(`${test}\n`);
}
console.log(ansi.bgBlack(report));
if(!CI){
return {
passed:this.#passed,
failed:this.#failed
};
}
if(globalThis.process){
process.exit(this.#failed.length);
//just incase
return report;
}
//incase you want to execute
//the same tests on multiple platforms or get fancy
return this.#failed.length;
}
//chew on something to let async operations have a moment
delay(delay=1000){
this.#is.number(delay);
let current=0;
while(current<delay){
current++;
}
return this;
}
#is=new Is;
#test='';
#passed=[];
#failed=[];
}
export {VanillaTest as default, VanillaTest};