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