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