@locustjs/test 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.esm.js CHANGED
@@ -1,854 +1,1339 @@
1
1
  import {
2
- equals,
3
- isString,
4
- isNumber,
5
- isDate,
6
- isBool,
7
- isBasic,
8
- isPrimitive,
9
- isEmpty,
10
- isSomeString,
11
- isObject,
12
- isSomeObject,
13
- isFunction,
14
- isNumeric,
15
- isArray,
16
- isIterable,
17
- isSomeArray,
18
- isSubClassOf
19
- } from '@locustjs/base';
20
- import { Exception } from '@locustjs/exception';
21
- import fs from 'fs';
22
- import path from 'path';
2
+ equals,
3
+ isString,
4
+ isNumber,
5
+ isDate,
6
+ isBool,
7
+ isBasic,
8
+ isPrimitive,
9
+ isEmpty,
10
+ isSomeString,
11
+ isObject,
12
+ isSomeObject,
13
+ isFunction,
14
+ isNumeric,
15
+ isArray,
16
+ isIterable,
17
+ isSomeArray,
18
+ isSubClassOf,
19
+ } from "@locustjs/base";
20
+ import { Exception } from "@locustjs/exception";
21
+ import fs from "fs";
22
+ import path from "path";
23
23
 
24
24
  class Expect {
25
- constructor(value) {
26
- this.value = value
27
- }
28
- toBe(value) {
29
- if (this.value === value) {
30
- } else {
31
- throw new Exception({ message: `${this.value} is not equal to ${value}`, code: 1000, status: 'not-eq' })
32
- }
33
-
34
- return this;
35
- }
36
- toBeGt(value) {
37
- if (this.value <= value) {
38
- throw new Exception({ message: `${this.value} is not greater than ${value}`, code: 1001, status: 'lte' })
39
- }
40
-
41
- return this;
42
- }
43
- toBeGte(value) {
44
- if (this.value < value) {
45
- throw new Exception({ message: `${this.value} is not greater than or equal to ${value}`, code: 1002, status: 'lt' })
46
- }
47
-
48
- return this;
49
- }
50
- toBeLt(value) {
51
- if (this.value >= value) {
52
- throw new Exception({ message: `${this.value} is not less than ${value}`, code: 1003, status: 'gte' })
53
- }
54
-
55
- return this;
56
- }
57
- toBeLte(value) {
58
- if (this.value > value) {
59
- throw new Exception({ message: `${this.value} is not less than or equal to ${value}`, code: 1004, status: 'gt' })
60
- }
61
-
62
- return this;
63
- }
64
- toBeBetween(n, m) {
65
- if (!(this.value >= n && this.value < m)) {
66
- throw new Exception({ message: `${this.value} is not between ${n} and ${m}`, code: 1024, status: 'between' })
67
- }
68
-
69
- return this;
70
- }
71
- toBeOfType(type) {
72
- if (typeof this.value !== type) {
73
- throw new Exception({ message: `${this.value} is not of type ${type}`, code: 1025, status: 'of-type' })
74
- }
75
-
76
- return this;
77
- }
78
- toBeString() {
79
- if (!isString(this.value)) {
80
- throw new Exception({ message: `${this.value} is not string`, code: 1026, status: 'is-string' })
81
- }
82
-
83
- return this;
84
- }
85
- toBeSomeString() {
86
- if (!isSomeString(this.value)) {
87
- throw new Exception({ message: `${this.value} is not some string`, code: 1027, status: 'is-some-string' })
88
- }
89
-
90
- return this;
91
- }
92
- toBeNumber() {
93
- if (!isNumber(this.value)) {
94
- throw new Exception({ message: `${this.value} is not number`, code: 1028, status: 'is-number' })
95
- }
96
-
97
- return this;
98
- }
99
- toBeDate() {
100
- if (!isDate(this.value)) {
101
- throw new Exception({ message: `${this.value} is not date`, code: 1029, status: 'is-date' })
102
- }
103
-
104
- return this;
105
- }
106
- toBeBool() {
107
- if (!isBool(this.value)) {
108
- throw new Exception({ message: `${this.value} is not bool`, code: 1030, status: 'is-bool' })
109
- }
110
-
111
- return this;
112
- }
113
- toBeBasicType() {
114
- if (!isBasic(this.value)) {
115
- throw new Exception({ message: `${this.value} is not basic type`, code: 1031, status: 'is-basic-type' })
116
- }
117
-
118
- return this;
119
- }
120
- toBePrimitive() {
121
- if (!isPrimitive(this.value)) {
122
- throw new Exception({ message: `${this.value} is not primitive type`, code: 1032, status: 'is-primitive' })
123
- }
124
-
125
- return this;
126
- }
127
- toBeEmpty() {
128
- if (!isEmpty(this.value)) {
129
- throw new Exception({ message: `${this.value} is not empty`, code: 1033, status: 'is-empty' })
130
- }
131
-
132
- return this;
133
- }
134
- toBeObject() {
135
- if (!isObject(this.value)) {
136
- throw new Exception({ message: `${this.value} is not object`, code: 1034, status: 'is-object' })
137
- }
138
-
139
- return this;
140
- }
141
- toBeSomeObject() {
142
- if (!isSomeObject(this.value)) {
143
- throw new Exception({ message: `${this.value} is not some object`, code: 1035, status: 'is-some-object' })
144
- }
145
-
146
- return this;
147
- }
148
- toBeFunction() {
149
- if (!isFunction(this.value)) {
150
- throw new Exception({ message: `${this.value} is not function`, code: 1036, status: 'is-function' })
151
- }
152
-
153
- return this;
154
- }
155
- toBeNumeric() {
156
- if (!isNumeric(this.value)) {
157
- throw new Exception({ message: `${this.value} is not numeric`, code: 1037, status: 'is-numeric' })
158
- }
159
-
160
- return this;
161
- }
162
- toBeArray() {
163
- if (!isArray(this.value)) {
164
- throw new Exception({ message: `${this.value} is not array`, code: 1038, status: 'is-array' })
165
- }
166
-
167
- return this;
168
- }
169
- toBeSomeArray() {
170
- if (!isSomeArray(this.value)) {
171
- throw new Exception({ message: `${this.value} is not some array`, code: 1039, status: 'is-some-array' })
172
- }
173
-
174
- return this;
175
- }
176
- toBeIterable() {
177
- if (!isIterable(this.value)) {
178
- throw new Exception({ message: `${this.value} is not iterable`, code: 1040, status: 'is-iterable' })
179
- }
180
-
181
- return this;
182
- }
183
- toBeSubClassOf(type) {
184
- if (!isSubClassOf(this.value, type)) {
185
- throw new Exception({ message: `${this.value} is not subclass of ${type}`, code: 1041, status: 'is-subclass-of' })
186
- }
187
-
188
- return this;
189
- }
190
- toBeInstanceOf(type) {
191
- if (!(this.value instanceof type)) {
192
- throw new Exception({ message: `${this.value} is not instance of ${type}`, code: 1042, status: 'instanceof' })
193
- }
194
-
195
- return this;
196
- }
197
- toMatch(pattern, flags) {
198
- const r = new RegExp(pattern, flags)
199
-
200
- if (!r.test(this.value)) {
201
- throw new Exception({ message: `${this.value} does not match ${pattern}`, code: 1043, status: 'match' })
202
- }
203
-
204
- return this;
205
- }
206
- notToBe(value) {
207
- if (this.value === value) {
208
- throw new Exception({ message: `${value} is equal to ${this.value}`, code: 1005, status: 'eq' })
209
- }
210
-
211
- return this;
212
- }
213
- toBeDefined() {
214
- if (this.value === undefined) {
215
- throw new Exception({ message: `value is undefined`, code: 1006, status: 'undefined' })
216
- }
217
-
218
- return this;
219
- }
220
- toBeUndefined() {
221
- if (this.value !== undefined) {
222
- throw new Exception({ message: `value is defined`, code: 1007, status: 'defined' })
223
- }
224
-
225
- return this;
226
- }
227
- toBeNull() {
228
- if (this.value !== null) {
229
- throw new Exception({ message: `value is not null`, code: 1008, status: 'not-null' })
230
- }
231
-
232
- return this;
233
- }
234
- notToBeNull() {
235
- if (this.value === null) {
236
- throw new Exception({ message: `value is null`, code: 1009, status: 'null' })
237
- }
238
-
239
- return this;
240
- }
241
- toBeNullOrUndefined() {
242
- if (this.value == null) {
243
- } else {
244
- throw new Exception({ message: `value is not null/undefined`, code: 1010, status: 'not-null-or-undefined' })
245
- }
246
-
247
- return this;
248
- }
249
- notToBeNullOrUndefined() {
250
- if (this.value == null) {
251
- throw new Exception({ message: `value is null/undefined`, code: 1011, status: 'null-or-undefined' })
252
- }
253
-
254
- return this;
255
- }
256
- notToBeBetween(n, m) {
257
- if (this.value >= n && this.value < m) {
258
- throw new Exception({ message: `${this.value} is between ${n} and ${m}`, code: 1044, status: 'not-between' })
259
- }
260
-
261
- return this;
262
- }
263
- notToBeOfType(type) {
264
- if (typeof this.value === type) {
265
- throw new Exception({ message: `${this.value} is of type ${type}`, code: 1045, status: 'not-oftype' })
266
- }
267
-
268
- return this;
269
- }
270
- notToBeString() {
271
- if (isString(this.value)) {
272
- throw new Exception({ message: `${this.value} is string`, code: 1046, status: 'not-is-string' })
273
- }
274
-
275
- return this;
276
- }
277
- notToBeSomeString() {
278
- if (isSomeString(this.value)) {
279
- throw new Exception({ message: `${this.value} is some string`, code: 1047, status: 'not-is-some-string' })
280
- }
281
-
282
- return this;
283
- }
284
- notToBeNumber() {
285
- if (isNumber(this.value)) {
286
- throw new Exception({ message: `${this.value} is number`, code: 1048, status: 'not-is-number' })
287
- }
288
-
289
- return this;
290
- }
291
- notToBeDate() {
292
- if (isDate(this.value)) {
293
- throw new Exception({ message: `${this.value} is date`, code: 1049, status: 'not-is-date' })
294
- }
295
-
296
- return this;
297
- }
298
- notToBeBool() {
299
- if (isBool(this.value)) {
300
- throw new Exception({ message: `${this.value} is bool`, code: 1050, status: 'not-is-bool' })
301
- }
302
-
303
- return this;
304
- }
305
- notToBeBasicType() {
306
- if (isBasic(this.value)) {
307
- throw new Exception({ message: `${this.value} is basic type`, code: 1051, status: 'not-is-basic-type' })
308
- }
309
-
310
- return this;
311
- }
312
- notToBePrimitive() {
313
- if (isPrimitive(this.value)) {
314
- throw new Exception({ message: `${this.value} is primitive type`, code: 1052, status: 'not-is-primitive' })
315
- }
316
-
317
- return this;
318
- }
319
- notToBeEmpty() {
320
- if (isEmpty(this.value)) {
321
- throw new Exception({ message: `${this.value} is empty`, code: 1053, status: 'not-is-empty' })
322
- }
323
-
324
- return this;
325
- }
326
- notToBeObject() {
327
- if (isObject(this.value)) {
328
- throw new Exception({ message: `${this.value} is object`, code: 1054, status: 'not-is-object' })
329
- }
330
-
331
- return this;
332
- }
333
- notToBeSomeObject() {
334
- if (isSomeObject(this.value)) {
335
- throw new Exception({ message: `${this.value} is some object`, code: 1055, status: 'not-is-some-object' })
336
- }
337
-
338
- return this;
339
- }
340
- notToBeFunction() {
341
- if (isFunction(this.value)) {
342
- throw new Exception({ message: `${this.value} is function`, code: 1056, status: 'not-is-function' })
343
- }
344
-
345
- return this;
346
- }
347
- notToBeNumeric() {
348
- if (isNumeric(this.value)) {
349
- throw new Exception({ message: `${this.value} is numeric`, code: 1057, status: 'not-is-numeric' })
350
- }
351
-
352
- return this;
353
- }
354
- notToBeArray() {
355
- if (isArray(this.value)) {
356
- throw new Exception({ message: `${this.value} is array`, code: 1058, status: 'not-is-array' })
357
- }
358
-
359
- return this;
360
- }
361
- toBeEmptyArray() {
362
- if (isSomeArray(this.value)) {
363
- throw new Exception({ message: `${this.value} is some array`, code: 1059, status: 'to-be-empty-array' })
364
- }
365
-
366
- return this;
367
- }
368
- notToBeIterable() {
369
- if (isIterable(this.value)) {
370
- throw new Exception({ message: `${this.value} is iterable`, code: 1060, status: 'not-iterable' })
371
- }
372
-
373
- return this;
374
- }
375
- notToBeSubClassOf(type) {
376
- if (isSubClassOf(this.value, type)) {
377
- throw new Exception({ message: `${this.value} is subclass of ${type}`, code: 1061, status: 'not-subclassof' })
378
- }
379
-
380
- return this;
381
- }
382
- notToBeInstanceOf(type) {
383
- if (this.value instanceof type) {
384
- throw new Exception({ message: `${this.value} is instance of ${type}`, code: 1062, status: 'not-instanceof' })
385
- }
386
-
387
- return this;
388
- }
389
- doesNotMatch(pattern, flags) {
390
- const r = new RegExp(pattern, flags)
391
-
392
- if (r.test(this.value)) {
393
- throw new Exception({ message: `${this.value} matches ${pattern}`, code: 1063, status: 'not-match' })
394
- }
395
-
396
- return this;
397
- }
398
- toBeValid(fnValidation) {
399
- if (!isFunction(fnValidation)) {
400
- throw new Exception({ message: `fnValidation is not function`, code: 1064, status: 'to-be-valid' })
401
- }
402
- if (!fnValidation(this.value)) {
403
- throw new Exception({ message: `${this.value} is not valid`, code: 1065, status: 'to-be-valid' })
404
- }
405
-
406
- return this;
407
- }
408
- notToBeValid(fnValidation) {
409
- if (!isFunction(fnValidation)) {
410
- throw new Exception({ message: `fnValidation is not function`, code: 1066, status: 'not-to-be-valid' })
411
- }
412
- if (fnValidation(this.value)) {
413
- throw new Exception({ message: `${this.value} is valid`, code: 1067, status: 'not-to-be-valid' })
414
- }
415
-
416
- return this;
417
- }
418
- toThrow(ex, shape = false, strict = false) {
419
- if (!isFunction(this.value)) {
420
- throw new Exception({ message: `given argument is not a function.`, code: 1012, status: 'not-func' })
421
- }
422
-
423
- let ok = false;
424
-
425
- try {
426
- this.value();
427
-
428
- ok = true;
429
- } catch (e) {
430
- if (ex !== undefined) {
431
- if (isPrimitive(ex)) {
432
- if (e !== ex) {
433
- throw new Exception({ message: `given function threw incorrect error.`, code: 1018, status: 'incorrect-throw-error' })
434
- }
435
- } else if (isFunction(ex)) {
436
- if (!(e instanceof ex)) {
437
- throw new Exception({ message: `given function threw incorrect instance.`, code: 1019, status: 'incorrect-throw-instance' })
438
- }
439
- } else if (isObject(ex)) {
440
- if (shape) {
441
- if (!equals(e, ex, strict)) {
442
- throw new Exception({ message: `given function threw incorrect object shape.`, code: 1020, status: 'incorrect-throw-shape' })
443
- }
444
- } else {
445
- if (e !== ex) {
446
- throw new Exception({ message: `given function threw incorrect object.`, code: 1021, status: 'incorrect-throw-object' })
447
- }
448
- }
449
- } else {
450
- if (e !== ex) {
451
- throw new Exception({ message: `given function threw incorrect value.`, code: 1022, status: 'incorrect-throw-value' })
452
- }
453
- }
454
- }
455
- }
456
-
457
- if (ok) {
458
- throw new Exception({ message: `given function ran without throwing any errors.`, code: 1013, status: 'ran-to-completion' })
459
- }
460
-
461
- return this;
462
- }
463
- async toThrowAsync(ex, shape = false, strict = false) {
464
- if (!isFunction(this.value)) {
465
- throw new Exception({ message: `given argument is not a function.`, code: 1012, status: 'not-func' })
466
- }
467
-
468
- let ok = false;
469
-
470
- try {
471
- await this.value();
472
-
473
- ok = true;
474
- } catch (e) {
475
- if (ex !== undefined) {
476
- if (isPrimitive(ex)) {
477
- if (e !== ex) {
478
- throw new Exception({ message: `given function threw incorrect error.`, code: 1018, status: 'incorrect-throw-error' })
479
- }
480
- } else if (isFunction(ex)) {
481
- if (!(e instanceof ex)) {
482
- throw new Exception({ message: `given function threw incorrect instance.`, code: 1019, status: 'incorrect-throw-instance' })
483
- }
484
- } else if (isObject(ex)) {
485
- if (shape) {
486
- if (!equals(e, ex, strict)) {
487
- throw new Exception({ message: `given function threw incorrect object shape.`, code: 1020, status: 'incorrect-throw-shape' })
488
- }
489
- } else {
490
- if (e !== ex) {
491
- throw new Exception({ message: `given function threw incorrect object.`, code: 1021, status: 'incorrect-throw-object' })
492
- }
493
- }
494
- } else {
495
- if (e !== ex) {
496
- throw new Exception({ message: `given function threw incorrect value.`, code: 1022, status: 'incorrect-throw-value' })
497
- }
498
- }
499
- }
500
- }
501
-
502
- if (ok) {
503
- throw new Exception({ message: `given function ran without throwing any errors.`, code: 1013, status: 'ran-to-completion' })
504
- }
505
-
506
- return this;
507
- }
508
- notToThrow(ex, shape = false, strict = false) {
509
- if (!isFunction(this.value)) {
510
- throw new Exception({ message: `given argument is not a function.`, code: 1012, status: 'not-func' })
511
- }
512
-
513
- let ok = true;
514
- let error;
515
-
516
- try {
517
- this.value();
518
-
519
- ok = false;
520
- } catch (e) {
521
- error = e;
522
-
523
- if (ex !== undefined) {
524
- if (isPrimitive(ex)) {
525
- if (e === ex) {
526
- throw new Exception({ message: `given function threw incorrect error.`, code: 1018, status: 'incorrect-throw-error' })
527
- }
528
- } else if (isFunction(ex)) {
529
- if (e instanceof ex) {
530
- throw new Exception({ message: `given function threw incorrect instance.`, code: 1019, status: 'incorrect-throw-instance' })
531
- }
532
- } else if (isObject(ex)) {
533
- if (shape) {
534
- if (equals(e, ex, strict)) {
535
- throw new Exception({ message: `given function threw incorrect object shape.`, code: 1020, status: 'incorrect-throw-shape' })
536
- }
537
- } else {
538
- if (e === ex) {
539
- throw new Exception({ message: `given function threw incorrect object.`, code: 1021, status: 'incorrect-throw-object' })
540
- }
541
- }
542
- } else {
543
- if (e === ex) {
544
- throw new Exception({ message: `given function threw incorrect value.`, code: 1022, status: 'incorrect-throw-value' })
545
- }
546
- }
547
- }
548
- }
549
-
550
- if (ok) {
551
- throw new Exception({ message: `given function threw an error.`, code: 1014, status: 'ran-to-error', innerException: error })
552
- }
553
-
554
- return this;
555
- }
556
- async notToThrowAsync(ex, shape = false, strict = false) {
557
- if (!isFunction(this.value)) {
558
- throw new Exception({ message: `given argument is not a function.`, code: 1012, status: 'not-func' })
559
- }
560
-
561
- let ok = true;
562
- let error;
563
-
564
- try {
565
- await this.value();
566
-
567
- ok = false;
568
- } catch (e) {
569
- error = e;
570
-
571
- if (ex !== undefined) {
572
- if (isPrimitive(ex)) {
573
- if (e === ex) {
574
- throw new Exception({ message: `given function threw incorrect error.`, code: 1018, status: 'incorrect-throw-error' })
575
- }
576
- } else if (isFunction(ex)) {
577
- if (e instanceof ex) {
578
- throw new Exception({ message: `given function threw incorrect instance.`, code: 1019, status: 'incorrect-throw-instance' })
579
- }
580
- } else if (isObject(ex)) {
581
- if (shape) {
582
- if (equals(e, ex, strict)) {
583
- throw new Exception({ message: `given function threw incorrect object shape.`, code: 1020, status: 'incorrect-throw-shape' })
584
- }
585
- } else {
586
- if (e === ex) {
587
- throw new Exception({ message: `given function threw incorrect object.`, code: 1021, status: 'incorrect-throw-object' })
588
- }
589
- }
590
- } else {
591
- if (e === ex) {
592
- throw new Exception({ message: `given function threw incorrect value.`, code: 1022, status: 'incorrect-throw-value' })
593
- }
594
- }
595
- }
596
- }
597
-
598
- if (ok) {
599
- throw new Exception({ message: `given function threw an error.`, code: 1014, status: 'ran-to-error', innerException: error })
600
- }
601
-
602
- return this;
603
- }
604
- toBeTruthy() {
605
- if (this.value) {
606
- } else {
607
- throw new Exception({ message: `${this.value} is not truthy`, code: 1015, status: 'not-truthy' })
608
- }
609
-
610
- return this;
611
- }
612
- toBeFalsy() {
613
- if (!this.value) {
614
- } else {
615
- throw new Exception({ message: `${this.value} is not falsy`, code: 1016, status: 'not-falsy' })
616
- }
617
-
618
- return this;
619
- }
620
- toBeNaN() {
621
- if (isNaN(this.value)) {
622
- } else {
623
- throw new Exception({ message: `${this.value} is not NaN`, code: 1017, status: 'not-nan' })
624
- }
625
-
626
- return this;
627
- }
628
- notToBeNaN() {
629
- if (!isNaN(this.value)) {
630
- } else {
631
- throw new Exception({ message: `${this.value} is NaN`, code: 1023, status: 'is-nan' })
632
- }
633
-
634
- return this;
635
- }
25
+ constructor(value) {
26
+ this.value = value;
27
+ }
28
+ toBe(value) {
29
+ if (this.value === value) {
30
+ } else {
31
+ throw new Exception({
32
+ message: `${this.value} is not equal to ${value}`,
33
+ code: 1000,
34
+ status: "not-eq",
35
+ });
36
+ }
37
+
38
+ return this;
39
+ }
40
+ toBeGt(value) {
41
+ if (this.value <= value) {
42
+ throw new Exception({
43
+ message: `${this.value} is not greater than ${value}`,
44
+ code: 1001,
45
+ status: "lte",
46
+ });
47
+ }
48
+
49
+ return this;
50
+ }
51
+ toBeGreaterThan(value) {
52
+ return this.toBeGt(value);
53
+ }
54
+ toBeGte(value) {
55
+ if (this.value < value) {
56
+ throw new Exception({
57
+ message: `${this.value} is not greater than or equal to ${value}`,
58
+ code: 1002,
59
+ status: "lt",
60
+ });
61
+ }
62
+
63
+ return this;
64
+ }
65
+ toBeGreaterThanOrEqualTo(value) {
66
+ return this.toBeGte(value);
67
+ }
68
+ toBeLt(value) {
69
+ if (this.value >= value) {
70
+ throw new Exception({
71
+ message: `${this.value} is not less than ${value}`,
72
+ code: 1003,
73
+ status: "gte",
74
+ });
75
+ }
76
+
77
+ return this;
78
+ }
79
+ toBeLowerThan(value) {
80
+ return this.toBeLt(value);
81
+ }
82
+ toBeLte(value) {
83
+ if (this.value > value) {
84
+ throw new Exception({
85
+ message: `${this.value} is not less than or equal to ${value}`,
86
+ code: 1004,
87
+ status: "gt",
88
+ });
89
+ }
90
+
91
+ return this;
92
+ }
93
+ toBeLowerThanOrEqualTo(value) {
94
+ return this.toBeLte(value);
95
+ }
96
+ toBeBetween(n, m) {
97
+ if (!(this.value >= n && this.value < m)) {
98
+ throw new Exception({
99
+ message: `${this.value} is not between ${n} and ${m}`,
100
+ code: 1024,
101
+ status: "between",
102
+ });
103
+ }
104
+
105
+ return this;
106
+ }
107
+ toBeOfType(type) {
108
+ if (typeof this.value !== type) {
109
+ throw new Exception({
110
+ message: `${this.value} is not of type ${type}`,
111
+ code: 1025,
112
+ status: "of-type",
113
+ });
114
+ }
115
+
116
+ return this;
117
+ }
118
+ toBeString() {
119
+ if (!isString(this.value)) {
120
+ throw new Exception({
121
+ message: `${this.value} is not string`,
122
+ code: 1026,
123
+ status: "is-string",
124
+ });
125
+ }
126
+
127
+ return this;
128
+ }
129
+ toBeSomeString() {
130
+ if (!isSomeString(this.value)) {
131
+ throw new Exception({
132
+ message: `${this.value} is not some string`,
133
+ code: 1027,
134
+ status: "is-some-string",
135
+ });
136
+ }
137
+
138
+ return this;
139
+ }
140
+ toBeNumber() {
141
+ if (!isNumber(this.value)) {
142
+ throw new Exception({
143
+ message: `${this.value} is not number`,
144
+ code: 1028,
145
+ status: "is-number",
146
+ });
147
+ }
148
+
149
+ return this;
150
+ }
151
+ toBeDate() {
152
+ if (!isDate(this.value)) {
153
+ throw new Exception({
154
+ message: `${this.value} is not date`,
155
+ code: 1029,
156
+ status: "is-date",
157
+ });
158
+ }
159
+
160
+ return this;
161
+ }
162
+ toBeBool() {
163
+ if (!isBool(this.value)) {
164
+ throw new Exception({
165
+ message: `${this.value} is not bool`,
166
+ code: 1030,
167
+ status: "is-bool",
168
+ });
169
+ }
170
+
171
+ return this;
172
+ }
173
+ toBeBasicType() {
174
+ if (!isBasic(this.value)) {
175
+ throw new Exception({
176
+ message: `${this.value} is not basic type`,
177
+ code: 1031,
178
+ status: "is-basic-type",
179
+ });
180
+ }
181
+
182
+ return this;
183
+ }
184
+ toBePrimitive() {
185
+ if (!isPrimitive(this.value)) {
186
+ throw new Exception({
187
+ message: `${this.value} is not primitive type`,
188
+ code: 1032,
189
+ status: "is-primitive",
190
+ });
191
+ }
192
+
193
+ return this;
194
+ }
195
+ toBeEmpty() {
196
+ if (!isEmpty(this.value)) {
197
+ throw new Exception({
198
+ message: `${this.value} is not empty`,
199
+ code: 1033,
200
+ status: "is-empty",
201
+ });
202
+ }
203
+
204
+ return this;
205
+ }
206
+ toBeObject() {
207
+ if (!isObject(this.value)) {
208
+ throw new Exception({
209
+ message: `${this.value} is not object`,
210
+ code: 1034,
211
+ status: "is-object",
212
+ });
213
+ }
214
+
215
+ return this;
216
+ }
217
+ toBeSomeObject() {
218
+ if (!isSomeObject(this.value)) {
219
+ throw new Exception({
220
+ message: `${this.value} is not some object`,
221
+ code: 1035,
222
+ status: "is-some-object",
223
+ });
224
+ }
225
+
226
+ return this;
227
+ }
228
+ toBeFunction() {
229
+ if (!isFunction(this.value)) {
230
+ throw new Exception({
231
+ message: `${this.value} is not function`,
232
+ code: 1036,
233
+ status: "is-function",
234
+ });
235
+ }
236
+
237
+ return this;
238
+ }
239
+ toBeNumeric() {
240
+ if (!isNumeric(this.value)) {
241
+ throw new Exception({
242
+ message: `${this.value} is not numeric`,
243
+ code: 1037,
244
+ status: "is-numeric",
245
+ });
246
+ }
247
+
248
+ return this;
249
+ }
250
+ toBeArray() {
251
+ if (!isArray(this.value)) {
252
+ throw new Exception({
253
+ message: `${this.value} is not array`,
254
+ code: 1038,
255
+ status: "is-array",
256
+ });
257
+ }
258
+
259
+ return this;
260
+ }
261
+ toBeSomeArray() {
262
+ if (!isSomeArray(this.value)) {
263
+ throw new Exception({
264
+ message: `${this.value} is not some array`,
265
+ code: 1039,
266
+ status: "is-some-array",
267
+ });
268
+ }
269
+
270
+ return this;
271
+ }
272
+ toBeIterable() {
273
+ if (!isIterable(this.value)) {
274
+ throw new Exception({
275
+ message: `${this.value} is not iterable`,
276
+ code: 1040,
277
+ status: "is-iterable",
278
+ });
279
+ }
280
+
281
+ return this;
282
+ }
283
+ toBeSubClassOf(type) {
284
+ if (!isSubClassOf(this.value, type)) {
285
+ throw new Exception({
286
+ message: `${this.value} is not subclass of ${type}`,
287
+ code: 1041,
288
+ status: "is-subclass-of",
289
+ });
290
+ }
291
+
292
+ return this;
293
+ }
294
+ toBeInstanceOf(type) {
295
+ if (!(this.value instanceof type)) {
296
+ throw new Exception({
297
+ message: `${this.value} is not instance of ${type}`,
298
+ code: 1042,
299
+ status: "instanceof",
300
+ });
301
+ }
302
+
303
+ return this;
304
+ }
305
+ toMatch(pattern, flags) {
306
+ const r = new RegExp(pattern, flags);
307
+
308
+ if (!r.test(this.value)) {
309
+ throw new Exception({
310
+ message: `${this.value} does not match ${pattern}`,
311
+ code: 1043,
312
+ status: "match",
313
+ });
314
+ }
315
+
316
+ return this;
317
+ }
318
+ notToBe(value) {
319
+ if (this.value === value) {
320
+ throw new Exception({
321
+ message: `${value} is equal to ${this.value}`,
322
+ code: 1005,
323
+ status: "eq",
324
+ });
325
+ }
326
+
327
+ return this;
328
+ }
329
+ toBeDefined() {
330
+ if (this.value === undefined) {
331
+ throw new Exception({
332
+ message: `value is undefined`,
333
+ code: 1006,
334
+ status: "undefined",
335
+ });
336
+ }
337
+
338
+ return this;
339
+ }
340
+ toBeUndefined() {
341
+ if (this.value !== undefined) {
342
+ throw new Exception({
343
+ message: `value is defined`,
344
+ code: 1007,
345
+ status: "defined",
346
+ });
347
+ }
348
+
349
+ return this;
350
+ }
351
+ toBeNull() {
352
+ if (this.value !== null) {
353
+ throw new Exception({
354
+ message: `value is not null`,
355
+ code: 1008,
356
+ status: "not-null",
357
+ });
358
+ }
359
+
360
+ return this;
361
+ }
362
+ notToBeNull() {
363
+ if (this.value === null) {
364
+ throw new Exception({
365
+ message: `value is null`,
366
+ code: 1009,
367
+ status: "null",
368
+ });
369
+ }
370
+
371
+ return this;
372
+ }
373
+ toBeNullOrUndefined() {
374
+ if (this.value == null) {
375
+ } else {
376
+ throw new Exception({
377
+ message: `value is not null/undefined`,
378
+ code: 1010,
379
+ status: "not-null-or-undefined",
380
+ });
381
+ }
382
+
383
+ return this;
384
+ }
385
+ notToBeNullOrUndefined() {
386
+ if (this.value == null) {
387
+ throw new Exception({
388
+ message: `value is null/undefined`,
389
+ code: 1011,
390
+ status: "null-or-undefined",
391
+ });
392
+ }
393
+
394
+ return this;
395
+ }
396
+ notToBeBetween(n, m) {
397
+ if (this.value >= n && this.value < m) {
398
+ throw new Exception({
399
+ message: `${this.value} is between ${n} and ${m}`,
400
+ code: 1044,
401
+ status: "not-between",
402
+ });
403
+ }
404
+
405
+ return this;
406
+ }
407
+ notToBeOfType(type) {
408
+ if (typeof this.value === type) {
409
+ throw new Exception({
410
+ message: `${this.value} is of type ${type}`,
411
+ code: 1045,
412
+ status: "not-oftype",
413
+ });
414
+ }
415
+
416
+ return this;
417
+ }
418
+ notToBeString() {
419
+ if (isString(this.value)) {
420
+ throw new Exception({
421
+ message: `${this.value} is string`,
422
+ code: 1046,
423
+ status: "not-is-string",
424
+ });
425
+ }
426
+
427
+ return this;
428
+ }
429
+ notToBeSomeString() {
430
+ if (isSomeString(this.value)) {
431
+ throw new Exception({
432
+ message: `${this.value} is some string`,
433
+ code: 1047,
434
+ status: "not-is-some-string",
435
+ });
436
+ }
437
+
438
+ return this;
439
+ }
440
+ notToBeNumber() {
441
+ if (isNumber(this.value)) {
442
+ throw new Exception({
443
+ message: `${this.value} is number`,
444
+ code: 1048,
445
+ status: "not-is-number",
446
+ });
447
+ }
448
+
449
+ return this;
450
+ }
451
+ notToBeDate() {
452
+ if (isDate(this.value)) {
453
+ throw new Exception({
454
+ message: `${this.value} is date`,
455
+ code: 1049,
456
+ status: "not-is-date",
457
+ });
458
+ }
459
+
460
+ return this;
461
+ }
462
+ notToBeBool() {
463
+ if (isBool(this.value)) {
464
+ throw new Exception({
465
+ message: `${this.value} is bool`,
466
+ code: 1050,
467
+ status: "not-is-bool",
468
+ });
469
+ }
470
+
471
+ return this;
472
+ }
473
+ notToBeBasicType() {
474
+ if (isBasic(this.value)) {
475
+ throw new Exception({
476
+ message: `${this.value} is basic type`,
477
+ code: 1051,
478
+ status: "not-is-basic-type",
479
+ });
480
+ }
481
+
482
+ return this;
483
+ }
484
+ notToBePrimitive() {
485
+ if (isPrimitive(this.value)) {
486
+ throw new Exception({
487
+ message: `${this.value} is primitive type`,
488
+ code: 1052,
489
+ status: "not-is-primitive",
490
+ });
491
+ }
492
+
493
+ return this;
494
+ }
495
+ notToBeEmpty() {
496
+ if (isEmpty(this.value)) {
497
+ throw new Exception({
498
+ message: `${this.value} is empty`,
499
+ code: 1053,
500
+ status: "not-is-empty",
501
+ });
502
+ }
503
+
504
+ return this;
505
+ }
506
+ notToBeObject() {
507
+ if (isObject(this.value)) {
508
+ throw new Exception({
509
+ message: `${this.value} is object`,
510
+ code: 1054,
511
+ status: "not-is-object",
512
+ });
513
+ }
514
+
515
+ return this;
516
+ }
517
+ notToBeSomeObject() {
518
+ if (isSomeObject(this.value)) {
519
+ throw new Exception({
520
+ message: `${this.value} is some object`,
521
+ code: 1055,
522
+ status: "not-is-some-object",
523
+ });
524
+ }
525
+
526
+ return this;
527
+ }
528
+ notToBeFunction() {
529
+ if (isFunction(this.value)) {
530
+ throw new Exception({
531
+ message: `${this.value} is function`,
532
+ code: 1056,
533
+ status: "not-is-function",
534
+ });
535
+ }
536
+
537
+ return this;
538
+ }
539
+ notToBeNumeric() {
540
+ if (isNumeric(this.value)) {
541
+ throw new Exception({
542
+ message: `${this.value} is numeric`,
543
+ code: 1057,
544
+ status: "not-is-numeric",
545
+ });
546
+ }
547
+
548
+ return this;
549
+ }
550
+ notToBeArray() {
551
+ if (isArray(this.value)) {
552
+ throw new Exception({
553
+ message: `${this.value} is array`,
554
+ code: 1058,
555
+ status: "not-is-array",
556
+ });
557
+ }
558
+
559
+ return this;
560
+ }
561
+ toBeEmptyArray() {
562
+ if (isSomeArray(this.value)) {
563
+ throw new Exception({
564
+ message: `${this.value} is some array`,
565
+ code: 1059,
566
+ status: "to-be-empty-array",
567
+ });
568
+ }
569
+
570
+ return this;
571
+ }
572
+ notToBeIterable() {
573
+ if (isIterable(this.value)) {
574
+ throw new Exception({
575
+ message: `${this.value} is iterable`,
576
+ code: 1060,
577
+ status: "not-iterable",
578
+ });
579
+ }
580
+
581
+ return this;
582
+ }
583
+ notToBeSubClassOf(type) {
584
+ if (isSubClassOf(this.value, type)) {
585
+ throw new Exception({
586
+ message: `${this.value} is subclass of ${type}`,
587
+ code: 1061,
588
+ status: "not-subclassof",
589
+ });
590
+ }
591
+
592
+ return this;
593
+ }
594
+ notToBeInstanceOf(type) {
595
+ if (this.value instanceof type) {
596
+ throw new Exception({
597
+ message: `${this.value} is instance of ${type}`,
598
+ code: 1062,
599
+ status: "not-instanceof",
600
+ });
601
+ }
602
+
603
+ return this;
604
+ }
605
+ doesNotMatch(pattern, flags) {
606
+ const r = new RegExp(pattern, flags);
607
+
608
+ if (r.test(this.value)) {
609
+ throw new Exception({
610
+ message: `${this.value} matches ${pattern}`,
611
+ code: 1063,
612
+ status: "not-match",
613
+ });
614
+ }
615
+
616
+ return this;
617
+ }
618
+ toBeValid(fnValidation) {
619
+ if (!isFunction(fnValidation)) {
620
+ throw new Exception({
621
+ message: `fnValidation is not function`,
622
+ code: 1064,
623
+ status: "to-be-valid",
624
+ });
625
+ }
626
+ if (!fnValidation(this.value)) {
627
+ throw new Exception({
628
+ message: `${this.value} is not valid`,
629
+ code: 1065,
630
+ status: "to-be-valid",
631
+ });
632
+ }
633
+
634
+ return this;
635
+ }
636
+ notToBeValid(fnValidation) {
637
+ if (!isFunction(fnValidation)) {
638
+ throw new Exception({
639
+ message: `fnValidation is not function`,
640
+ code: 1066,
641
+ status: "not-to-be-valid",
642
+ });
643
+ }
644
+ if (fnValidation(this.value)) {
645
+ throw new Exception({
646
+ message: `${this.value} is valid`,
647
+ code: 1067,
648
+ status: "not-to-be-valid",
649
+ });
650
+ }
651
+
652
+ return this;
653
+ }
654
+ toThrow(ex, shape = false, strict = false) {
655
+ if (!isFunction(this.value)) {
656
+ throw new Exception({
657
+ message: `given argument is not a function.`,
658
+ code: 1012,
659
+ status: "not-func",
660
+ });
661
+ }
662
+
663
+ let ok = false;
664
+
665
+ try {
666
+ this.value();
667
+
668
+ ok = true;
669
+ } catch (e) {
670
+ if (ex !== undefined) {
671
+ if (isPrimitive(ex)) {
672
+ if (e !== ex) {
673
+ throw new Exception({
674
+ message: `given function threw incorrect error.`,
675
+ code: 1018,
676
+ status: "incorrect-throw-error",
677
+ });
678
+ }
679
+ } else if (isFunction(ex)) {
680
+ if (!(e instanceof ex)) {
681
+ throw new Exception({
682
+ message: `given function threw incorrect instance.`,
683
+ code: 1019,
684
+ status: "incorrect-throw-instance",
685
+ });
686
+ }
687
+ } else if (isObject(ex)) {
688
+ if (shape) {
689
+ if (!equals(e, ex, strict)) {
690
+ throw new Exception({
691
+ message: `given function threw incorrect object shape.`,
692
+ code: 1020,
693
+ status: "incorrect-throw-shape",
694
+ });
695
+ }
696
+ } else {
697
+ if (e !== ex) {
698
+ throw new Exception({
699
+ message: `given function threw incorrect object.`,
700
+ code: 1021,
701
+ status: "incorrect-throw-object",
702
+ });
703
+ }
704
+ }
705
+ } else {
706
+ if (e !== ex) {
707
+ throw new Exception({
708
+ message: `given function threw incorrect value.`,
709
+ code: 1022,
710
+ status: "incorrect-throw-value",
711
+ });
712
+ }
713
+ }
714
+ }
715
+ }
716
+
717
+ if (ok) {
718
+ throw new Exception({
719
+ message: `given function ran without throwing any errors.`,
720
+ code: 1013,
721
+ status: "ran-to-completion",
722
+ });
723
+ }
724
+
725
+ return this;
726
+ }
727
+ async toThrowAsync(ex, shape = false, strict = false) {
728
+ if (!isFunction(this.value)) {
729
+ throw new Exception({
730
+ message: `given argument is not a function.`,
731
+ code: 1012,
732
+ status: "not-func",
733
+ });
734
+ }
735
+
736
+ let ok = false;
737
+
738
+ try {
739
+ await this.value();
740
+
741
+ ok = true;
742
+ } catch (e) {
743
+ if (ex !== undefined) {
744
+ if (isPrimitive(ex)) {
745
+ if (e !== ex) {
746
+ throw new Exception({
747
+ message: `given function threw incorrect error.`,
748
+ code: 1018,
749
+ status: "incorrect-throw-error",
750
+ });
751
+ }
752
+ } else if (isFunction(ex)) {
753
+ if (!(e instanceof ex)) {
754
+ throw new Exception({
755
+ message: `given function threw incorrect instance.`,
756
+ code: 1019,
757
+ status: "incorrect-throw-instance",
758
+ });
759
+ }
760
+ } else if (isObject(ex)) {
761
+ if (shape) {
762
+ if (!equals(e, ex, strict)) {
763
+ throw new Exception({
764
+ message: `given function threw incorrect object shape.`,
765
+ code: 1020,
766
+ status: "incorrect-throw-shape",
767
+ });
768
+ }
769
+ } else {
770
+ if (e !== ex) {
771
+ throw new Exception({
772
+ message: `given function threw incorrect object.`,
773
+ code: 1021,
774
+ status: "incorrect-throw-object",
775
+ });
776
+ }
777
+ }
778
+ } else {
779
+ if (e !== ex) {
780
+ throw new Exception({
781
+ message: `given function threw incorrect value.`,
782
+ code: 1022,
783
+ status: "incorrect-throw-value",
784
+ });
785
+ }
786
+ }
787
+ }
788
+ }
789
+
790
+ if (ok) {
791
+ throw new Exception({
792
+ message: `given function ran without throwing any errors.`,
793
+ code: 1013,
794
+ status: "ran-to-completion",
795
+ });
796
+ }
797
+
798
+ return this;
799
+ }
800
+ notToThrow(ex, shape = false, strict = false) {
801
+ if (!isFunction(this.value)) {
802
+ throw new Exception({
803
+ message: `given argument is not a function.`,
804
+ code: 1012,
805
+ status: "not-func",
806
+ });
807
+ }
808
+
809
+ let ok = true;
810
+ let error;
811
+
812
+ try {
813
+ this.value();
814
+
815
+ ok = false;
816
+ } catch (e) {
817
+ error = e;
818
+
819
+ if (ex !== undefined) {
820
+ if (isPrimitive(ex)) {
821
+ if (e === ex) {
822
+ throw new Exception({
823
+ message: `given function threw incorrect error.`,
824
+ code: 1018,
825
+ status: "incorrect-throw-error",
826
+ });
827
+ }
828
+ } else if (isFunction(ex)) {
829
+ if (e instanceof ex) {
830
+ throw new Exception({
831
+ message: `given function threw incorrect instance.`,
832
+ code: 1019,
833
+ status: "incorrect-throw-instance",
834
+ });
835
+ }
836
+ } else if (isObject(ex)) {
837
+ if (shape) {
838
+ if (equals(e, ex, strict)) {
839
+ throw new Exception({
840
+ message: `given function threw incorrect object shape.`,
841
+ code: 1020,
842
+ status: "incorrect-throw-shape",
843
+ });
844
+ }
845
+ } else {
846
+ if (e === ex) {
847
+ throw new Exception({
848
+ message: `given function threw incorrect object.`,
849
+ code: 1021,
850
+ status: "incorrect-throw-object",
851
+ });
852
+ }
853
+ }
854
+ } else {
855
+ if (e === ex) {
856
+ throw new Exception({
857
+ message: `given function threw incorrect value.`,
858
+ code: 1022,
859
+ status: "incorrect-throw-value",
860
+ });
861
+ }
862
+ }
863
+ }
864
+ }
865
+
866
+ if (ok) {
867
+ throw new Exception({
868
+ message: `given function threw an error.`,
869
+ code: 1014,
870
+ status: "ran-to-error",
871
+ innerException: error,
872
+ });
873
+ }
874
+
875
+ return this;
876
+ }
877
+ async notToThrowAsync(ex, shape = false, strict = false) {
878
+ if (!isFunction(this.value)) {
879
+ throw new Exception({
880
+ message: `given argument is not a function.`,
881
+ code: 1012,
882
+ status: "not-func",
883
+ });
884
+ }
885
+
886
+ let ok = true;
887
+ let error;
888
+
889
+ try {
890
+ await this.value();
891
+
892
+ ok = false;
893
+ } catch (e) {
894
+ error = e;
895
+
896
+ if (ex !== undefined) {
897
+ if (isPrimitive(ex)) {
898
+ if (e === ex) {
899
+ throw new Exception({
900
+ message: `given function threw incorrect error.`,
901
+ code: 1018,
902
+ status: "incorrect-throw-error",
903
+ });
904
+ }
905
+ } else if (isFunction(ex)) {
906
+ if (e instanceof ex) {
907
+ throw new Exception({
908
+ message: `given function threw incorrect instance.`,
909
+ code: 1019,
910
+ status: "incorrect-throw-instance",
911
+ });
912
+ }
913
+ } else if (isObject(ex)) {
914
+ if (shape) {
915
+ if (equals(e, ex, strict)) {
916
+ throw new Exception({
917
+ message: `given function threw incorrect object shape.`,
918
+ code: 1020,
919
+ status: "incorrect-throw-shape",
920
+ });
921
+ }
922
+ } else {
923
+ if (e === ex) {
924
+ throw new Exception({
925
+ message: `given function threw incorrect object.`,
926
+ code: 1021,
927
+ status: "incorrect-throw-object",
928
+ });
929
+ }
930
+ }
931
+ } else {
932
+ if (e === ex) {
933
+ throw new Exception({
934
+ message: `given function threw incorrect value.`,
935
+ code: 1022,
936
+ status: "incorrect-throw-value",
937
+ });
938
+ }
939
+ }
940
+ }
941
+ }
942
+
943
+ if (ok) {
944
+ throw new Exception({
945
+ message: `given function threw an error.`,
946
+ code: 1014,
947
+ status: "ran-to-error",
948
+ innerException: error,
949
+ });
950
+ }
951
+
952
+ return this;
953
+ }
954
+ toBeTruthy() {
955
+ if (this.value) {
956
+ } else {
957
+ throw new Exception({
958
+ message: `${this.value} is not truthy`,
959
+ code: 1015,
960
+ status: "not-truthy",
961
+ });
962
+ }
963
+
964
+ return this;
965
+ }
966
+ toBeTrue() {
967
+ return this.toBeTruthy();
968
+ }
969
+ toBeFalsy() {
970
+ if (!this.value) {
971
+ } else {
972
+ throw new Exception({
973
+ message: `${this.value} is not falsy`,
974
+ code: 1016,
975
+ status: "not-falsy",
976
+ });
977
+ }
978
+
979
+ return this;
980
+ }
981
+ toBeFalse() {
982
+ return this.toBeFalsy();
983
+ }
984
+ toBeNaN() {
985
+ if (isNaN(this.value)) {
986
+ } else {
987
+ throw new Exception({
988
+ message: `${this.value} is not NaN`,
989
+ code: 1017,
990
+ status: "not-nan",
991
+ });
992
+ }
993
+
994
+ return this;
995
+ }
996
+ notToBeNaN() {
997
+ if (!isNaN(this.value)) {
998
+ } else {
999
+ throw new Exception({
1000
+ message: `${this.value} is NaN`,
1001
+ code: 1023,
1002
+ status: "is-nan",
1003
+ });
1004
+ }
1005
+
1006
+ return this;
1007
+ }
636
1008
  }
637
1009
 
638
1010
  const expect = (x) => new Expect(x);
639
1011
 
640
1012
  class Test {
641
- constructor(name, fn) {
642
- this.name = name;
643
- this.fn = fn;
644
- }
645
- run() {
646
- return new Promise(res => {
647
- const start = new Date();
648
-
649
- if (isFunction(this.fn)) {
650
- try {
651
- const _result = this.fn(expect);
652
-
653
- if (_result && isFunction(_result.then)) {
654
- _result.then(result => {
655
- res({ success: true, test: this.name, result, time: new Date() - start });
656
- }).catch(ex => {
657
- res({ success: false, test: this.name, time: new Date() - start, err: new Exception({ message: `test '${this.name}' failed.`, code: 501, status: 'failed', innerException: ex }) })
658
- });
659
- } else {
660
- res({ success: true, test: this.name, time: new Date() - start, result: _result });
661
- }
662
- } catch (ex) {
663
- res({ success: false, test: this.name, time: new Date() - start, err: new Exception({ message: `test '${this.name}' failed.`, code: 501, status: 'failed', innerException: ex }) })
664
- }
665
- } else {
666
- res({ success: false, test: this.name, time: new Date() - start, err: new Exception({ message: `test '${this.name}' does not have a function to be called.`, code: 500, status: 'no-func' }) })
667
- }
668
- });
669
- }
1013
+ constructor(name, fn) {
1014
+ this.name = name;
1015
+ this.fn = fn;
1016
+ }
1017
+ run() {
1018
+ return new Promise((res) => {
1019
+ const start = new Date();
1020
+
1021
+ if (isFunction(this.fn)) {
1022
+ try {
1023
+ const _result = this.fn(expect);
1024
+
1025
+ if (_result && isFunction(_result.then)) {
1026
+ _result
1027
+ .then((result) => {
1028
+ res({
1029
+ success: true,
1030
+ test: this.name,
1031
+ result,
1032
+ time: new Date() - start,
1033
+ });
1034
+ })
1035
+ .catch((ex) => {
1036
+ res({
1037
+ success: false,
1038
+ test: this.name,
1039
+ time: new Date() - start,
1040
+ err: new Exception({
1041
+ message: `test '${this.name}' failed.`,
1042
+ code: 501,
1043
+ status: "failed",
1044
+ innerException: ex,
1045
+ }),
1046
+ });
1047
+ });
1048
+ } else {
1049
+ res({
1050
+ success: true,
1051
+ test: this.name,
1052
+ time: new Date() - start,
1053
+ result: _result,
1054
+ });
1055
+ }
1056
+ } catch (ex) {
1057
+ res({
1058
+ success: false,
1059
+ test: this.name,
1060
+ time: new Date() - start,
1061
+ err: new Exception({
1062
+ message: `test '${this.name}' failed.`,
1063
+ code: 501,
1064
+ status: "failed",
1065
+ innerException: ex,
1066
+ }),
1067
+ });
1068
+ }
1069
+ } else {
1070
+ res({
1071
+ success: false,
1072
+ test: this.name,
1073
+ time: new Date() - start,
1074
+ err: new Exception({
1075
+ message: `test '${this.name}' does not have a function to be called.`,
1076
+ code: 500,
1077
+ status: "no-func",
1078
+ }),
1079
+ });
1080
+ }
1081
+ });
1082
+ }
670
1083
  }
671
1084
 
672
- const ConsoleColors = {
673
- BackColor: {
674
- Black: 40,
675
- },
676
- ForeColor: {
677
- Red: 31,
678
- Green: 32,
679
- White: 37,
680
- Gray: 90,
681
- },
682
- Modifier: {
683
- Reset: "\x1b[0m",
684
- },
685
- };
1085
+ const reset = "\x1b[0m"
1086
+ const bright = "\x1b[1m"
1087
+ const dim = "\x1b[2m"
1088
+ const underscore = "\x1b[4m"
1089
+ const blink = "\x1b[5m"
1090
+ const reverse = "\x1b[7m"
1091
+ const hidden = "\x1b[8m"
1092
+
1093
+ const fgBlack = "\x1b[30m"
1094
+ const fgRed = "\x1b[31m"
1095
+ const fgGreen = "\x1b[32m"
1096
+ const fgYellow = "\x1b[33m"
1097
+ const fgBlue = "\x1b[34m"
1098
+ const fgMagenta = "\x1b[35m"
1099
+ const fgCyan = "\x1b[36m"
1100
+ const fgWhite = "\x1b[37m"
1101
+ const fgGray = "\x1b[90m"
1102
+
1103
+ const bgBlack = "\x1b[40m"
1104
+ const bgRed = "\x1b[41m"
1105
+ const bgGreen = "\x1b[42m"
1106
+ const bgYellow = "\x1b[43m"
1107
+ const bgBlue = "\x1b[44m"
1108
+ const bgMagenta = "\x1b[45m"
1109
+ const bgCyan = "\x1b[46m"
1110
+ const bgWhite = "\x1b[47m"
1111
+ const bgGray = "\x1b[100m"
686
1112
 
687
1113
  class TestRunner {
688
- constructor() {
689
- this._passed = 0;
690
- this._failed = 0;
691
- this._results = []
692
- this._errors = []
693
- }
694
- async _runSingle(test, onProgress, i) {
695
- if (isFunction(onProgress)) {
696
- try {
697
- onProgress(i, test);
698
- } catch (ex) {
699
- this._errors.push({ index: i, test: test.name, err: new Exception({ message: `onProgress failed for test '${test.name} at index ${i}'.`, code: 1500, status: 'progress-failed', innerException: ex }) })
700
- }
701
- }
702
-
703
- const tr = await test.run();
704
-
705
- this._results.push(tr);
706
-
707
- if (tr.success) {
708
- this._passed++;
709
- } else {
710
- this._failed++;
711
- }
712
- }
713
- get result() {
714
- return {
715
- passed: this._passed,
716
- failed: this._failed,
717
- results: this._results,
718
- errors: this._errors
719
- }
720
- }
721
- run(tests, onProgress) {
722
- this._passed = 0;
723
- this._failed = 0;
724
- this._results = [];
725
- this._errors = [];
726
-
727
- return new Promise(res => {
728
- if (tests) {
729
- if (tests instanceof Test) {
730
- tests = [tests]
731
- }
732
-
733
- if (isArray(tests)) {
734
- const _tests = tests
735
- .map(test => {
736
- let _test = test;
737
-
738
- if (isArray(test)) {
739
- if (test.length == 2) {
740
- _test = new Test(test[0], test[1]);
741
- }
742
- }
743
-
744
- return _test;
745
- })
746
- .filter(test => (test instanceof Test))
747
- .map((test, i) => this._runSingle(test, onProgress, i));
748
-
749
- Promise.all(_tests).then(_ => res(this.result)).catch(ex => {
750
- this._errors.push({ err: new Exception({ message: `not all tests succeeded. check errors.`, code: 1503, status: 'partial-finished', innerException: ex }) });
751
-
752
- res(this.result);
753
- });
754
- } else {
755
- this._errors.push({ err: new Exception({ message: `invalid tests. expected array or a single test.`, code: 1502, status: 'invalid-tests' }) });
756
-
757
- res(this.result);
758
- }
759
- } else {
760
- this._errors.push({ err: new Exception({ message: `no tests given to be ran.`, code: 1501, status: 'no-tests' }) });
761
-
762
- res(this.result);
763
- }
764
- })
765
- }
766
- _getTime(time) {
767
- return `${time / 1000} sec`
768
- }
769
- report(detailed) {
770
- let time = 0;
771
-
772
- console.log('Finished.\n');
773
-
774
- for (let i = 0; i < this._results.length; i++) {
775
- const result = this._results[i];
776
-
777
- if (detailed) {
778
- let message = '\n';
779
-
780
- if (result.success) {
781
- message += `${i + 1}. \x1b[${ConsoleColors.ForeColor.White}m${result.test}: \x1b[${ConsoleColors.ForeColor.Green}m passed ${ConsoleColors.Modifier.Reset} (${this._getTime(result.time)})`
782
- } else {
783
- message += `${i + 1}. \x1b[${ConsoleColors.ForeColor.White}m${result.test}: \x1b[${ConsoleColors.ForeColor.Red}m failed ${ConsoleColors.Modifier.Reset} (${this._getTime(result.time)})`;
784
- message += '\n';
785
- message += `\x1b[${ConsoleColors.ForeColor.White}m${result.err.code}: ${result.err.toString()} ${ConsoleColors.Modifier.Reset}`;
786
- }
787
-
788
- console.log(message);
789
- }
790
-
791
- time += result.time;
792
- }
793
-
794
- if (detailed && this._errors.length) {
795
- console.log('Errors:');
796
-
797
- for (let error of this._errors) {
798
- if (error.index !== undefined) {
799
- console.log(`${error.index}. ${error.test}: ${error.err.innerException.toString()}`);
800
- } else {
801
- console.log(`${error.err.toString()}`);
802
- }
803
- }
804
- }
805
-
806
- const text = (detailed ? '\n': '') +
807
- `Tests: ${this._passed + this._failed}` +
808
- '\n' +
809
- `Time: ${time / 1000} sec` +
810
- '\n\n' +
811
- (this._passed > 0 ? `\x1b[${ConsoleColors.ForeColor.Green}m ${this._passed} tests passed${ConsoleColors.Modifier.Reset}` : '0 tests passed') +
812
- ', ' +
813
- (this._failed > 0 ? `\x1b[${ConsoleColors.ForeColor.Red}m ${this._failed} tests failed${ConsoleColors.Modifier.Reset}` : '0 tests failed') +
814
- '\n';
815
-
816
- console.log(text);
817
- }
818
- log(filename) {
819
- const content = JSON.stringify({
820
- results: this._results,
821
- errors: this._errors
822
- }, null, '\t');
823
-
824
- if (filename == null) {
825
- const d = new Date();
826
-
827
- const year = d.getFullYear().toString().padStart(4, '0');
828
- const month = (d.getMonth() + 1).toString().padStart(2, '0');
829
- const day = d.getDate().toString().padStart(2, '0');
830
- const hours = d.getHours().toString().padStart(2, '0');
831
- const minutes = d.getMinutes().toString().padStart(2, '0');
832
- const seconds = d.getSeconds().toString().padStart(2, '0');
833
-
834
- filename = `test-${year}-${month}-${day}-${hours}${minutes}${seconds}.json`;
835
- }
836
-
837
- const filepath = path.join(process.cwd(), filename);
838
-
839
- try {
840
- fs.writeFileSync(filepath, content);
841
-
842
- console.log(`tests outcome wrote in ${filename}.`);
843
- } catch (ex) {
844
- console.log('writing tests outcome failed.\n' + ex);
845
- }
846
- }
1114
+ constructor() {
1115
+ this._passed = 0;
1116
+ this._failed = 0;
1117
+ this._results = [];
1118
+ this._errors = [];
1119
+ }
1120
+ async _runSingle(test, onProgress, i) {
1121
+ if (isFunction(onProgress)) {
1122
+ try {
1123
+ onProgress(i, test);
1124
+ } catch (ex) {
1125
+ this._errors.push({
1126
+ index: i,
1127
+ test: test.name,
1128
+ err: new Exception({
1129
+ message: `onProgress failed for test '${test.name} at index ${i}'.`,
1130
+ code: 1500,
1131
+ status: "progress-failed",
1132
+ innerException: ex,
1133
+ }),
1134
+ });
1135
+ }
1136
+ }
1137
+
1138
+ const tr = await test.run();
1139
+
1140
+ this._results.push(tr);
1141
+
1142
+ if (tr.success) {
1143
+ this._passed++;
1144
+ } else {
1145
+ this._failed++;
1146
+ }
1147
+ }
1148
+ get result() {
1149
+ return {
1150
+ passed: this._passed,
1151
+ failed: this._failed,
1152
+ results: this._results,
1153
+ errors: this._errors,
1154
+ };
1155
+ }
1156
+ run(tests, onProgress) {
1157
+ this._passed = 0;
1158
+ this._failed = 0;
1159
+ this._results = [];
1160
+ this._errors = [];
1161
+
1162
+ return new Promise((res) => {
1163
+ if (tests) {
1164
+ if (tests instanceof Test) {
1165
+ tests = [tests];
1166
+ }
1167
+
1168
+ if (isArray(tests)) {
1169
+ const _tests = tests
1170
+ .map((test) => {
1171
+ let _test = test;
1172
+
1173
+ if (isArray(test)) {
1174
+ if (test.length == 2) {
1175
+ _test = new Test(test[0], test[1]);
1176
+ }
1177
+ }
1178
+
1179
+ return _test;
1180
+ })
1181
+ .filter((test) => test instanceof Test)
1182
+ .map((test, i) => this._runSingle(test, onProgress, i));
1183
+
1184
+ Promise.all(_tests)
1185
+ .then((_) => res(this.result))
1186
+ .catch((ex) => {
1187
+ this._errors.push({
1188
+ err: new Exception({
1189
+ message: `not all tests succeeded. check errors.`,
1190
+ code: 1503,
1191
+ status: "partial-finished",
1192
+ innerException: ex,
1193
+ }),
1194
+ });
1195
+
1196
+ res(this.result);
1197
+ });
1198
+ } else {
1199
+ this._errors.push({
1200
+ err: new Exception({
1201
+ message: `invalid tests. expected array or a single test.`,
1202
+ code: 1502,
1203
+ status: "invalid-tests",
1204
+ }),
1205
+ });
1206
+
1207
+ res(this.result);
1208
+ }
1209
+ } else {
1210
+ this._errors.push({
1211
+ err: new Exception({
1212
+ message: `no tests given to be ran.`,
1213
+ code: 1501,
1214
+ status: "no-tests",
1215
+ }),
1216
+ });
1217
+
1218
+ res(this.result);
1219
+ }
1220
+ });
1221
+ }
1222
+ _getTime(time) {
1223
+ return `${time / 1000} sec`;
1224
+ }
1225
+ report(detailed) {
1226
+ let time = 0;
1227
+
1228
+ console.log("Finished.\n");
1229
+
1230
+ for (let i = 0; i < this._results.length; i++) {
1231
+ const result = this._results[i];
1232
+ const t = `(${this._getTime(result.time)})`;
1233
+
1234
+ if (detailed) {
1235
+ let message = "\n" + (i + 1) + ". ";
1236
+
1237
+ if (result.success) {
1238
+ message += `${fgWhite}${result.test}: ${fgGreen}passed${reset} ${t}`;
1239
+ } else {
1240
+ message += `${bright}${fgWhite}${result.test}: ${fgRed}failed${reset} ${t}`;
1241
+ message += "\n";
1242
+
1243
+ let err = result.err.toString().split("\n");
1244
+
1245
+ err = err
1246
+ .map(
1247
+ (msg, i) =>
1248
+ `\t${
1249
+ i == err.length - 1
1250
+ ? `${fgYellow}`
1251
+ : `${fgGray}error ${result.err.code}: `
1252
+ }${msg}${reset}`
1253
+ )
1254
+ .join("\n");
1255
+
1256
+ message += `${fgGray}${err} ${reset}`;
1257
+ }
1258
+
1259
+ console.log(message);
1260
+ }
1261
+
1262
+ time += result.time;
1263
+ }
1264
+
1265
+ if (detailed && this._errors.length) {
1266
+ console.log("Errors:");
1267
+
1268
+ for (let error of this._errors) {
1269
+ if (error.index !== undefined) {
1270
+ console.log(
1271
+ `${error.index}. ${
1272
+ error.test
1273
+ }: ${error.err.innerException.toString()}`
1274
+ );
1275
+ } else {
1276
+ console.log(`${error.err.toString()}`);
1277
+ }
1278
+ }
1279
+ }
1280
+
1281
+ const text =
1282
+ (detailed ? "\n" : "") +
1283
+ `${bright}Number of tests: ${reset}${this._passed + this._failed}` +
1284
+ "\n" +
1285
+ `${bright}Total Time: ${reset}${time / 1000} sec` +
1286
+ "\n\n" +
1287
+ (this._passed > 0
1288
+ ? `${fgGreen} ${this._passed} test(s) passed${reset}`
1289
+ : `0 tests passed${reset}`) +
1290
+ ", " +
1291
+ (this._failed > 0
1292
+ ? `${fgRed} ${this._failed} test(s) failed${reset}`
1293
+ : `0 tests failed${reset}`) +
1294
+ "\n";
1295
+
1296
+ console.log(text);
1297
+ }
1298
+ log(filename) {
1299
+ const content = JSON.stringify(
1300
+ {
1301
+ results: this._results,
1302
+ errors: this._errors,
1303
+ },
1304
+ null,
1305
+ "\t"
1306
+ );
1307
+
1308
+ if (filename == null) {
1309
+ const d = new Date();
1310
+
1311
+ const year = d.getFullYear().toString().padStart(4, "0");
1312
+ const month = (d.getMonth() + 1).toString().padStart(2, "0");
1313
+ const day = d.getDate().toString().padStart(2, "0");
1314
+ const hours = d.getHours().toString().padStart(2, "0");
1315
+ const minutes = d.getMinutes().toString().padStart(2, "0");
1316
+ const seconds = d.getSeconds().toString().padStart(2, "0");
1317
+
1318
+ filename = `test-${year}-${month}-${day}-${hours}${minutes}${seconds}.json`;
1319
+ }
1320
+
1321
+ const filepath = path.join(process.cwd(), filename);
1322
+
1323
+ try {
1324
+ fs.writeFileSync(filepath, content);
1325
+
1326
+ console.log(`tests outcome wrote in ${filename}.`);
1327
+ } catch (ex) {
1328
+ console.log("writing tests outcome failed.\n" + ex);
1329
+ }
1330
+ }
1331
+ static start(tests) {
1332
+ const tr = new TestRunner();
1333
+
1334
+ tr.run(tests).then((result) => tr.report(result.failed > 0));
1335
+ }
847
1336
  }
848
1337
 
849
1338
  export default TestRunner;
850
- export {
851
- Test,
852
- Expect,
853
- expect
854
- }
1339
+ export { Test, Expect, expect };