@locustjs/test 1.1.6 → 1.2.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.cjs.js CHANGED
@@ -1,4 +1,4 @@
1
- import { isArray, isFunction, isObject, isPrimitive, equals } from '@locustjs/base';
1
+ import { equals, isString, isNumber, isDate, isBool, isBasic, isPrimitive, isEmpty, isSomeString, isObject, isSomeObject, isFunction, isNumeric, isArray, isIterable, isSomeArray, isSubClassOf } from '@locustjs/base';
2
2
  import { Exception } from '@locustjs/exception';
3
3
  import fs from 'fs';
4
4
  import path from 'path';
@@ -14,42 +14,248 @@ class Expect {
14
14
  status: 'not-eq'
15
15
  });
16
16
  }
17
+ return this;
17
18
  }
18
19
  toBeGt(value) {
19
20
  if (this.value <= value) {
20
21
  throw new Exception({
21
- message: `${this.value} is lower than or equal to ${value}`,
22
+ message: `${this.value} is not greater than ${value}`,
22
23
  code: 1001,
23
24
  status: 'lte'
24
25
  });
25
26
  }
27
+ return this;
26
28
  }
27
29
  toBeGte(value) {
28
30
  if (this.value < value) {
29
31
  throw new Exception({
30
- message: `${this.value} is lower than to ${value}`,
32
+ message: `${this.value} is not greater than or equal to ${value}`,
31
33
  code: 1002,
32
34
  status: 'lt'
33
35
  });
34
36
  }
37
+ return this;
35
38
  }
36
39
  toBeLt(value) {
37
40
  if (this.value >= value) {
38
41
  throw new Exception({
39
- message: `${this.value} is greater than or equal to ${value}`,
42
+ message: `${this.value} is not less than ${value}`,
40
43
  code: 1003,
41
44
  status: 'gte'
42
45
  });
43
46
  }
47
+ return this;
44
48
  }
45
49
  toBeLte(value) {
46
50
  if (this.value > value) {
47
51
  throw new Exception({
48
- message: `${this.value} is greater than ${value}`,
52
+ message: `${this.value} is not less than or equal to ${value}`,
49
53
  code: 1004,
50
54
  status: 'gt'
51
55
  });
52
56
  }
57
+ return this;
58
+ }
59
+ toBeBetween(n, m) {
60
+ if (!(this.value >= n && this.value < m)) {
61
+ throw new Exception({
62
+ message: `${this.value} is not between ${n} and ${m}`,
63
+ code: 1024,
64
+ status: 'between'
65
+ });
66
+ }
67
+ return this;
68
+ }
69
+ toBeOfType(type) {
70
+ if (typeof this.value !== type) {
71
+ throw new Exception({
72
+ message: `${this.value} is not of type ${type}`,
73
+ code: 1025,
74
+ status: 'of-type'
75
+ });
76
+ }
77
+ return this;
78
+ }
79
+ toBeString() {
80
+ if (!isString(this.value)) {
81
+ throw new Exception({
82
+ message: `${this.value} is not string`,
83
+ code: 1026,
84
+ status: 'is-string'
85
+ });
86
+ }
87
+ return this;
88
+ }
89
+ toBeSomeString() {
90
+ if (!isSomeString(this.value)) {
91
+ throw new Exception({
92
+ message: `${this.value} is not some string`,
93
+ code: 1027,
94
+ status: 'is-some-string'
95
+ });
96
+ }
97
+ return this;
98
+ }
99
+ toBeNumber() {
100
+ if (!isNumber(this.value)) {
101
+ throw new Exception({
102
+ message: `${this.value} is not number`,
103
+ code: 1028,
104
+ status: 'is-number'
105
+ });
106
+ }
107
+ return this;
108
+ }
109
+ toBeDate() {
110
+ if (!isDate(this.value)) {
111
+ throw new Exception({
112
+ message: `${this.value} is not date`,
113
+ code: 1029,
114
+ status: 'is-date'
115
+ });
116
+ }
117
+ return this;
118
+ }
119
+ toBeBool() {
120
+ if (!isBool(this.value)) {
121
+ throw new Exception({
122
+ message: `${this.value} is not bool`,
123
+ code: 1030,
124
+ status: 'is-bool'
125
+ });
126
+ }
127
+ return this;
128
+ }
129
+ toBeBasicType() {
130
+ if (!isBasic(this.value)) {
131
+ throw new Exception({
132
+ message: `${this.value} is not basic type`,
133
+ code: 1031,
134
+ status: 'is-basic-type'
135
+ });
136
+ }
137
+ return this;
138
+ }
139
+ toBePrimitive() {
140
+ if (!isPrimitive(this.value)) {
141
+ throw new Exception({
142
+ message: `${this.value} is not primitive type`,
143
+ code: 1032,
144
+ status: 'is-primitive'
145
+ });
146
+ }
147
+ return this;
148
+ }
149
+ toBeEmpty() {
150
+ if (!isEmpty(this.value)) {
151
+ throw new Exception({
152
+ message: `${this.value} is not empty`,
153
+ code: 1033,
154
+ status: 'is-empty'
155
+ });
156
+ }
157
+ return this;
158
+ }
159
+ toBeObject() {
160
+ if (!isObject(this.value)) {
161
+ throw new Exception({
162
+ message: `${this.value} is not object`,
163
+ code: 1034,
164
+ status: 'is-object'
165
+ });
166
+ }
167
+ return this;
168
+ }
169
+ toBeSomeObject() {
170
+ if (!isSomeObject(this.value)) {
171
+ throw new Exception({
172
+ message: `${this.value} is not some object`,
173
+ code: 1035,
174
+ status: 'is-some-object'
175
+ });
176
+ }
177
+ return this;
178
+ }
179
+ toBeFunction() {
180
+ if (!isFunction(this.value)) {
181
+ throw new Exception({
182
+ message: `${this.value} is not function`,
183
+ code: 1036,
184
+ status: 'is-function'
185
+ });
186
+ }
187
+ return this;
188
+ }
189
+ toBeNumeric() {
190
+ if (!isNumeric(this.value)) {
191
+ throw new Exception({
192
+ message: `${this.value} is not numeric`,
193
+ code: 1037,
194
+ status: 'is-numeric'
195
+ });
196
+ }
197
+ return this;
198
+ }
199
+ toBeArray() {
200
+ if (!isArray(this.value)) {
201
+ throw new Exception({
202
+ message: `${this.value} is not array`,
203
+ code: 1038,
204
+ status: 'is-array'
205
+ });
206
+ }
207
+ return this;
208
+ }
209
+ toBeSomeArray() {
210
+ if (!isSomeArray(this.value)) {
211
+ throw new Exception({
212
+ message: `${this.value} is not some array`,
213
+ code: 1039,
214
+ status: 'is-some-array'
215
+ });
216
+ }
217
+ return this;
218
+ }
219
+ toBeIterable() {
220
+ if (!isIterable(this.value)) {
221
+ throw new Exception({
222
+ message: `${this.value} is not iterable`,
223
+ code: 1040,
224
+ status: 'is-iterable'
225
+ });
226
+ }
227
+ return this;
228
+ }
229
+ toBeSubClassOf(type) {
230
+ if (!isSubClassOf(this.value, type)) {
231
+ throw new Exception({
232
+ message: `${this.value} is not subclass of ${type}`,
233
+ code: 1041,
234
+ status: 'is-subclass-of'
235
+ });
236
+ }
237
+ return this;
238
+ }
239
+ toBeInstanceOf(type) {
240
+ if (!(this.value instanceof type)) {
241
+ throw new Exception({
242
+ message: `${this.value} is not instance of ${type}`,
243
+ code: 1042,
244
+ status: 'instanceof'
245
+ });
246
+ }
247
+ return this;
248
+ }
249
+ toMatch(pattern, flags) {
250
+ const r = new RegExp(pattern, flags);
251
+ if (!r.test(this.value)) {
252
+ throw new Exception({
253
+ message: `${this.value} does not match ${pattern}`,
254
+ code: 1043,
255
+ status: 'match'
256
+ });
257
+ }
258
+ return this;
53
259
  }
54
260
  notToBe(value) {
55
261
  if (this.value === value) {
@@ -59,6 +265,7 @@ class Expect {
59
265
  status: 'eq'
60
266
  });
61
267
  }
268
+ return this;
62
269
  }
63
270
  toBeDefined() {
64
271
  if (this.value === undefined) {
@@ -68,6 +275,7 @@ class Expect {
68
275
  status: 'undefined'
69
276
  });
70
277
  }
278
+ return this;
71
279
  }
72
280
  toBeUndefined() {
73
281
  if (this.value !== undefined) {
@@ -77,6 +285,7 @@ class Expect {
77
285
  status: 'defined'
78
286
  });
79
287
  }
288
+ return this;
80
289
  }
81
290
  toBeNull() {
82
291
  if (this.value !== null) {
@@ -86,6 +295,7 @@ class Expect {
86
295
  status: 'not-null'
87
296
  });
88
297
  }
298
+ return this;
89
299
  }
90
300
  notToBeNull() {
91
301
  if (this.value === null) {
@@ -95,6 +305,7 @@ class Expect {
95
305
  status: 'null'
96
306
  });
97
307
  }
308
+ return this;
98
309
  }
99
310
  toBeNullOrUndefined() {
100
311
  if (this.value == null) {} else {
@@ -104,6 +315,7 @@ class Expect {
104
315
  status: 'not-null-or-undefined'
105
316
  });
106
317
  }
318
+ return this;
107
319
  }
108
320
  notToBeNullOrUndefined() {
109
321
  if (this.value == null) {
@@ -113,6 +325,208 @@ class Expect {
113
325
  status: 'null-or-undefined'
114
326
  });
115
327
  }
328
+ return this;
329
+ }
330
+ notToBeBetween(n, m) {
331
+ if (this.value >= n && this.value < m) {
332
+ throw new Exception({
333
+ message: `${this.value} is between ${n} and ${m}`,
334
+ code: 1044,
335
+ status: 'not-between'
336
+ });
337
+ }
338
+ return this;
339
+ }
340
+ notToBeOfType(type) {
341
+ if (typeof this.value === type) {
342
+ throw new Exception({
343
+ message: `${this.value} is of type ${type}`,
344
+ code: 1045,
345
+ status: 'not-oftype'
346
+ });
347
+ }
348
+ return this;
349
+ }
350
+ notToBeString() {
351
+ if (isString(this.value)) {
352
+ throw new Exception({
353
+ message: `${this.value} is string`,
354
+ code: 1046,
355
+ status: 'not-is-string'
356
+ });
357
+ }
358
+ return this;
359
+ }
360
+ notToBeSomeString() {
361
+ if (isSomeString(this.value)) {
362
+ throw new Exception({
363
+ message: `${this.value} is some string`,
364
+ code: 1047,
365
+ status: 'not-is-some-string'
366
+ });
367
+ }
368
+ return this;
369
+ }
370
+ notToBeNumber() {
371
+ if (isNumber(this.value)) {
372
+ throw new Exception({
373
+ message: `${this.value} is number`,
374
+ code: 1048,
375
+ status: 'not-is-number'
376
+ });
377
+ }
378
+ return this;
379
+ }
380
+ notToBeDate() {
381
+ if (isDate(this.value)) {
382
+ throw new Exception({
383
+ message: `${this.value} is date`,
384
+ code: 1049,
385
+ status: 'not-is-date'
386
+ });
387
+ }
388
+ return this;
389
+ }
390
+ notToBeBool() {
391
+ if (isBool(this.value)) {
392
+ throw new Exception({
393
+ message: `${this.value} is bool`,
394
+ code: 1050,
395
+ status: 'not-is-bool'
396
+ });
397
+ }
398
+ return this;
399
+ }
400
+ notToBeBasicType() {
401
+ if (isBasic(this.value)) {
402
+ throw new Exception({
403
+ message: `${this.value} is basic type`,
404
+ code: 1051,
405
+ status: 'not-is-basic-type'
406
+ });
407
+ }
408
+ return this;
409
+ }
410
+ notToBePrimitive() {
411
+ if (isPrimitive(this.value)) {
412
+ throw new Exception({
413
+ message: `${this.value} is primitive type`,
414
+ code: 1052,
415
+ status: 'not-is-primitive'
416
+ });
417
+ }
418
+ return this;
419
+ }
420
+ notToBeEmpty() {
421
+ if (isEmpty(this.value)) {
422
+ throw new Exception({
423
+ message: `${this.value} is empty`,
424
+ code: 1053,
425
+ status: 'not-is-empty'
426
+ });
427
+ }
428
+ return this;
429
+ }
430
+ notToBeObject() {
431
+ if (isObject(this.value)) {
432
+ throw new Exception({
433
+ message: `${this.value} is object`,
434
+ code: 1054,
435
+ status: 'not-is-object'
436
+ });
437
+ }
438
+ return this;
439
+ }
440
+ notToBeSomeObject() {
441
+ if (isSomeObject(this.value)) {
442
+ throw new Exception({
443
+ message: `${this.value} is some object`,
444
+ code: 1055,
445
+ status: 'not-is-some-object'
446
+ });
447
+ }
448
+ return this;
449
+ }
450
+ notToBeFunction() {
451
+ if (isFunction(this.value)) {
452
+ throw new Exception({
453
+ message: `${this.value} is function`,
454
+ code: 1056,
455
+ status: 'not-is-function'
456
+ });
457
+ }
458
+ return this;
459
+ }
460
+ notToBeNumeric() {
461
+ if (isNumeric(this.value)) {
462
+ throw new Exception({
463
+ message: `${this.value} is numeric`,
464
+ code: 1057,
465
+ status: 'not-is-numeric'
466
+ });
467
+ }
468
+ return this;
469
+ }
470
+ notToBeArray() {
471
+ if (isArray(this.value)) {
472
+ throw new Exception({
473
+ message: `${this.value} is array`,
474
+ code: 1058,
475
+ status: 'not-is-array'
476
+ });
477
+ }
478
+ return this;
479
+ }
480
+ toBeEmptyArray() {
481
+ if (isSomeArray(this.value)) {
482
+ throw new Exception({
483
+ message: `${this.value} is some array`,
484
+ code: 1059,
485
+ status: 'to-be-empty-array'
486
+ });
487
+ }
488
+ return this;
489
+ }
490
+ notToBeIterable() {
491
+ if (isIterable(this.value)) {
492
+ throw new Exception({
493
+ message: `${this.value} is iterable`,
494
+ code: 1060,
495
+ status: 'not-iterable'
496
+ });
497
+ }
498
+ return this;
499
+ }
500
+ notToBeSubClassOf(type) {
501
+ if (isSubClassOf(this.value, type)) {
502
+ throw new Exception({
503
+ message: `${this.value} is subclass of ${type}`,
504
+ code: 1061,
505
+ status: 'not-subclassof'
506
+ });
507
+ }
508
+ return this;
509
+ }
510
+ notToBeInstanceOf(type) {
511
+ if (this.value instanceof type) {
512
+ throw new Exception({
513
+ message: `${this.value} is instance of ${type}`,
514
+ code: 1062,
515
+ status: 'not-instanceof'
516
+ });
517
+ }
518
+ return this;
519
+ }
520
+ doesNotMatch(pattern, flags) {
521
+ const r = new RegExp(pattern, flags);
522
+ if (r.test(this.value)) {
523
+ throw new Exception({
524
+ message: `${this.value} matches ${pattern}`,
525
+ code: 1063,
526
+ status: 'not-match'
527
+ });
528
+ }
529
+ return this;
116
530
  }
117
531
  toThrow(ex, shape = false, strict = false) {
118
532
  if (!isFunction(this.value)) {
@@ -180,6 +594,7 @@ class Expect {
180
594
  status: 'ran-to-completion'
181
595
  });
182
596
  }
597
+ return this;
183
598
  }
184
599
  async toThrowAsync(ex, shape = false, strict = false) {
185
600
  if (!isFunction(this.value)) {
@@ -247,6 +662,7 @@ class Expect {
247
662
  status: 'ran-to-completion'
248
663
  });
249
664
  }
665
+ return this;
250
666
  }
251
667
  notToThrow(ex, shape = false, strict = false) {
252
668
  if (!isFunction(this.value)) {
@@ -317,6 +733,7 @@ class Expect {
317
733
  innerException: error
318
734
  });
319
735
  }
736
+ return this;
320
737
  }
321
738
  async notToThrowAsync(ex, shape = false, strict = false) {
322
739
  if (!isFunction(this.value)) {
@@ -387,6 +804,7 @@ class Expect {
387
804
  innerException: error
388
805
  });
389
806
  }
807
+ return this;
390
808
  }
391
809
  toBeTruthy() {
392
810
  if (this.value) {} else {
@@ -396,6 +814,7 @@ class Expect {
396
814
  status: 'not-truthy'
397
815
  });
398
816
  }
817
+ return this;
399
818
  }
400
819
  toBeFalsy() {
401
820
  if (!this.value) {} else {
@@ -405,6 +824,7 @@ class Expect {
405
824
  status: 'not-falsy'
406
825
  });
407
826
  }
827
+ return this;
408
828
  }
409
829
  toBeNaN() {
410
830
  if (isNaN(this.value)) {} else {
@@ -414,6 +834,7 @@ class Expect {
414
834
  status: 'not-nan'
415
835
  });
416
836
  }
837
+ return this;
417
838
  }
418
839
  notToBeNaN() {
419
840
  if (!isNaN(this.value)) {} else {
@@ -423,6 +844,7 @@ class Expect {
423
844
  status: 'is-nan'
424
845
  });
425
846
  }
847
+ return this;
426
848
  }
427
849
  }
428
850
  const expect = x => new Expect(x);
@@ -568,7 +990,7 @@ class TestRunner {
568
990
  }
569
991
  return _test;
570
992
  }).filter(test => test instanceof Test).map((test, i) => this._runSingle(test, onProgress, i));
571
- Promise.all(_tests).then(_ => res()).catch(ex => {
993
+ Promise.all(_tests).then(_ => res(this.result)).catch(ex => {
572
994
  this._errors.push({
573
995
  err: new Exception({
574
996
  message: `not all tests succeeded. check errors.`,
@@ -577,7 +999,7 @@ class TestRunner {
577
999
  innerException: ex
578
1000
  })
579
1001
  });
580
- res();
1002
+ res(this.result);
581
1003
  });
582
1004
  } else {
583
1005
  this._errors.push({
@@ -587,7 +1009,7 @@ class TestRunner {
587
1009
  status: 'invalid-tests'
588
1010
  })
589
1011
  });
590
- res();
1012
+ res(this.result);
591
1013
  }
592
1014
  } else {
593
1015
  this._errors.push({
@@ -597,7 +1019,7 @@ class TestRunner {
597
1019
  status: 'no-tests'
598
1020
  })
599
1021
  });
600
- res();
1022
+ res(this.result);
601
1023
  }
602
1024
  });
603
1025
  }
@@ -632,7 +1054,7 @@ class TestRunner {
632
1054
  }
633
1055
  }
634
1056
  }
635
- const text = (detailed ? '\n' : '') + `Tests: ${this._passed + this._failed}` + '\n' + `Time: ${time / 1000} sec` + '\n\n' + (this._passed > 0 ? `\x1b[${ConsoleColors.ForeColor.Green}m ${this._passed} tests passed ${ConsoleColors.Modifier.Reset}` : '0 tests passed') + ', ' + (this._failed > 0 ? `\x1b[${ConsoleColors.ForeColor.Red}m ${this._failed} tests failed ${ConsoleColors.Modifier.Reset}` : '0 tests failed') + '\n';
1057
+ const text = (detailed ? '\n' : '') + `Tests: ${this._passed + this._failed}` + '\n' + `Time: ${time / 1000} sec` + '\n\n' + (this._passed > 0 ? `\x1b[${ConsoleColors.ForeColor.Green}m ${this._passed} tests passed${ConsoleColors.Modifier.Reset}` : '0 tests passed') + ', ' + (this._failed > 0 ? `\x1b[${ConsoleColors.ForeColor.Red}m ${this._failed} tests failed${ConsoleColors.Modifier.Reset}` : '0 tests failed') + '\n';
636
1058
  console.log(text);
637
1059
  }
638
1060
  log(filename) {
package/index.esm.js CHANGED
@@ -1,4 +1,22 @@
1
- import { isArray, isFunction, isObject, isPrimitive, equals } from '@locustjs/base';
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';
2
20
  import { Exception } from '@locustjs/exception';
3
21
  import fs from 'fs';
4
22
  import path from 'path';
@@ -12,62 +30,370 @@ class Expect {
12
30
  } else {
13
31
  throw new Exception({ message: `${this.value} is not equal to ${value}`, code: 1000, status: 'not-eq' })
14
32
  }
33
+
34
+ return this;
15
35
  }
16
36
  toBeGt(value) {
17
37
  if (this.value <= value) {
18
- throw new Exception({ message: `${this.value} is lower than or equal to ${value}`, code: 1001, status: 'lte' })
38
+ throw new Exception({ message: `${this.value} is not greater than ${value}`, code: 1001, status: 'lte' })
19
39
  }
40
+
41
+ return this;
20
42
  }
21
43
  toBeGte(value) {
22
44
  if (this.value < value) {
23
- throw new Exception({ message: `${this.value} is lower than to ${value}`, code: 1002, status: 'lt' })
45
+ throw new Exception({ message: `${this.value} is not greater than or equal to ${value}`, code: 1002, status: 'lt' })
24
46
  }
47
+
48
+ return this;
25
49
  }
26
50
  toBeLt(value) {
27
51
  if (this.value >= value) {
28
- throw new Exception({ message: `${this.value} is greater than or equal to ${value}`, code: 1003, status: 'gte' })
52
+ throw new Exception({ message: `${this.value} is not less than ${value}`, code: 1003, status: 'gte' })
29
53
  }
54
+
55
+ return this;
30
56
  }
31
57
  toBeLte(value) {
32
58
  if (this.value > value) {
33
- throw new Exception({ message: `${this.value} is greater than ${value}`, code: 1004, status: 'gt' })
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' })
34
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;
35
205
  }
36
206
  notToBe(value) {
37
207
  if (this.value === value) {
38
208
  throw new Exception({ message: `${value} is equal to ${this.value}`, code: 1005, status: 'eq' })
39
209
  }
210
+
211
+ return this;
40
212
  }
41
213
  toBeDefined() {
42
214
  if (this.value === undefined) {
43
215
  throw new Exception({ message: `value is undefined`, code: 1006, status: 'undefined' })
44
216
  }
217
+
218
+ return this;
45
219
  }
46
220
  toBeUndefined() {
47
221
  if (this.value !== undefined) {
48
222
  throw new Exception({ message: `value is defined`, code: 1007, status: 'defined' })
49
223
  }
224
+
225
+ return this;
50
226
  }
51
227
  toBeNull() {
52
228
  if (this.value !== null) {
53
229
  throw new Exception({ message: `value is not null`, code: 1008, status: 'not-null' })
54
230
  }
231
+
232
+ return this;
55
233
  }
56
234
  notToBeNull() {
57
235
  if (this.value === null) {
58
236
  throw new Exception({ message: `value is null`, code: 1009, status: 'null' })
59
237
  }
238
+
239
+ return this;
60
240
  }
61
241
  toBeNullOrUndefined() {
62
242
  if (this.value == null) {
63
243
  } else {
64
244
  throw new Exception({ message: `value is not null/undefined`, code: 1010, status: 'not-null-or-undefined' })
65
245
  }
246
+
247
+ return this;
66
248
  }
67
249
  notToBeNullOrUndefined() {
68
250
  if (this.value == null) {
69
251
  throw new Exception({ message: `value is null/undefined`, code: 1011, status: 'null-or-undefined' })
70
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;
71
397
  }
72
398
  toThrow(ex, shape = false, strict = false) {
73
399
  if (!isFunction(this.value)) {
@@ -111,6 +437,8 @@ class Expect {
111
437
  if (ok) {
112
438
  throw new Exception({ message: `given function ran without throwing any errors.`, code: 1013, status: 'ran-to-completion' })
113
439
  }
440
+
441
+ return this;
114
442
  }
115
443
  async toThrowAsync(ex, shape = false, strict = false) {
116
444
  if (!isFunction(this.value)) {
@@ -154,6 +482,8 @@ class Expect {
154
482
  if (ok) {
155
483
  throw new Exception({ message: `given function ran without throwing any errors.`, code: 1013, status: 'ran-to-completion' })
156
484
  }
485
+
486
+ return this;
157
487
  }
158
488
  notToThrow(ex, shape = false, strict = false) {
159
489
  if (!isFunction(this.value)) {
@@ -200,6 +530,8 @@ class Expect {
200
530
  if (ok) {
201
531
  throw new Exception({ message: `given function threw an error.`, code: 1014, status: 'ran-to-error', innerException: error })
202
532
  }
533
+
534
+ return this;
203
535
  }
204
536
  async notToThrowAsync(ex, shape = false, strict = false) {
205
537
  if (!isFunction(this.value)) {
@@ -246,30 +578,40 @@ class Expect {
246
578
  if (ok) {
247
579
  throw new Exception({ message: `given function threw an error.`, code: 1014, status: 'ran-to-error', innerException: error })
248
580
  }
581
+
582
+ return this;
249
583
  }
250
584
  toBeTruthy() {
251
585
  if (this.value) {
252
586
  } else {
253
587
  throw new Exception({ message: `${this.value} is not truthy`, code: 1015, status: 'not-truthy' })
254
588
  }
589
+
590
+ return this;
255
591
  }
256
592
  toBeFalsy() {
257
593
  if (!this.value) {
258
594
  } else {
259
595
  throw new Exception({ message: `${this.value} is not falsy`, code: 1016, status: 'not-falsy' })
260
596
  }
597
+
598
+ return this;
261
599
  }
262
600
  toBeNaN() {
263
601
  if (isNaN(this.value)) {
264
602
  } else {
265
603
  throw new Exception({ message: `${this.value} is not NaN`, code: 1017, status: 'not-nan' })
266
604
  }
605
+
606
+ return this;
267
607
  }
268
608
  notToBeNaN() {
269
609
  if (!isNaN(this.value)) {
270
610
  } else {
271
611
  throw new Exception({ message: `${this.value} is NaN`, code: 1023, status: 'is-nan' })
272
612
  }
613
+
614
+ return this;
273
615
  }
274
616
  }
275
617
 
@@ -384,20 +726,20 @@ class TestRunner {
384
726
  .filter(test => (test instanceof Test))
385
727
  .map((test, i) => this._runSingle(test, onProgress, i));
386
728
 
387
- Promise.all(_tests).then(_ => res()).catch(ex => {
729
+ Promise.all(_tests).then(_ => res(this.result)).catch(ex => {
388
730
  this._errors.push({ err: new Exception({ message: `not all tests succeeded. check errors.`, code: 1503, status: 'partial-finished', innerException: ex }) });
389
731
 
390
- res();
732
+ res(this.result);
391
733
  });
392
734
  } else {
393
735
  this._errors.push({ err: new Exception({ message: `invalid tests. expected array or a single test.`, code: 1502, status: 'invalid-tests' }) });
394
736
 
395
- res();
737
+ res(this.result);
396
738
  }
397
739
  } else {
398
740
  this._errors.push({ err: new Exception({ message: `no tests given to be ran.`, code: 1501, status: 'no-tests' }) });
399
741
 
400
- res();
742
+ res(this.result);
401
743
  }
402
744
  })
403
745
  }
@@ -446,9 +788,9 @@ class TestRunner {
446
788
  '\n' +
447
789
  `Time: ${time / 1000} sec` +
448
790
  '\n\n' +
449
- (this._passed > 0 ? `\x1b[${ConsoleColors.ForeColor.Green}m ${this._passed} tests passed ${ConsoleColors.Modifier.Reset}` : '0 tests passed') +
791
+ (this._passed > 0 ? `\x1b[${ConsoleColors.ForeColor.Green}m ${this._passed} tests passed${ConsoleColors.Modifier.Reset}` : '0 tests passed') +
450
792
  ', ' +
451
- (this._failed > 0 ? `\x1b[${ConsoleColors.ForeColor.Red}m ${this._failed} tests failed ${ConsoleColors.Modifier.Reset}` : '0 tests failed') +
793
+ (this._failed > 0 ? `\x1b[${ConsoleColors.ForeColor.Red}m ${this._failed} tests failed${ConsoleColors.Modifier.Reset}` : '0 tests failed') +
452
794
  '\n';
453
795
 
454
796
  console.log(text);
package/package.json CHANGED
@@ -1,12 +1,9 @@
1
1
  {
2
2
  "name": "@locustjs/test",
3
- "version": "1.1.6",
3
+ "version": "1.2.0",
4
4
  "description": "This library provides a simple test runner.",
5
5
  "main": "index.cjs.js",
6
6
  "module": "index.esm.js",
7
- "scripts": {
8
- "build": "babel index.esm.js -o index.cjs.js"
9
- },
10
7
  "repository": {
11
8
  "type": "git",
12
9
  "url": "git+https://github.com/ironcodev/locustjs-test.git"
@@ -27,6 +24,11 @@
27
24
  },
28
25
  "devDependencies": {
29
26
  "@babel/cli": "^7.22.6",
30
- "@babel/core": "^7.22.8"
27
+ "@babel/core": "^7.22.8",
28
+ "esm": "^3.2.25"
29
+ },
30
+ "scripts": {
31
+ "build": "babel index.esm.js -o index.cjs.js",
32
+ "test": "node -r esm ./tests/index.js"
31
33
  }
32
- }
34
+ }
package/tests/index.js ADDED
@@ -0,0 +1,112 @@
1
+ import TestRunner from "../index.esm.js";
2
+
3
+ class Foo {}
4
+ class Bar extends Foo {}
5
+ class Buz {}
6
+
7
+ const tests = [
8
+ [
9
+ "Test 1: number",
10
+ function (expect) {
11
+ const n = 10;
12
+
13
+ expect(n)
14
+ .toBeDefined()
15
+ .toBeBasicType()
16
+ .toBePrimitive()
17
+ .toBeNumber()
18
+ .toBeNumeric()
19
+ .toBeGt(8)
20
+ .toBeGte(10)
21
+ .toBeLt(12)
22
+ .toBeLte(10)
23
+ .toBeBetween(5, 15)
24
+ .notToBeBetween(12, 20)
25
+ .toBeOfType("number");
26
+ },
27
+ ],
28
+ [
29
+ "Test 2: string",
30
+ function (expect) {
31
+ const n = "10";
32
+
33
+ expect(n)
34
+ .toBeDefined()
35
+ .notToBeNumber()
36
+ .toBeNumeric()
37
+ .toBeOfType("string")
38
+ .toBeString()
39
+ .toBeSomeString();
40
+ },
41
+ ],
42
+ [
43
+ "Test 3: empty array",
44
+ function (expect) {
45
+ const n = [];
46
+
47
+ expect(n)
48
+ .toBeDefined()
49
+ .toBeArray()
50
+ .toBeEmptyArray();
51
+ },
52
+ ],
53
+ [
54
+ "Test 4: array",
55
+ function (expect) {
56
+ const n = [10];
57
+
58
+ expect(n)
59
+ .toBeDefined()
60
+ .toBeArray()
61
+ .toBeSomeArray();
62
+ },
63
+ ],
64
+ [
65
+ "Test 3: empty object",
66
+ function (expect) {
67
+ const n = {};
68
+
69
+ expect(n)
70
+ .toBeDefined()
71
+ .notToBeBasicType()
72
+ .notToBePrimitive()
73
+ .notToBeNull()
74
+ .toBeObject()
75
+ .notToBeSomeObject();
76
+ },
77
+ ],
78
+ [
79
+ "Test 4: object",
80
+ function (expect) {
81
+ const n = {a:10};
82
+
83
+ expect(n)
84
+ .toBeDefined()
85
+ .notToBeBasicType()
86
+ .notToBePrimitive()
87
+ .notToBeNull()
88
+ .toBeObject()
89
+ .toBeSomeObject();
90
+ },
91
+ ],
92
+ [
93
+ "Test 4: class",
94
+ function (expect) {
95
+ const x = new Bar();
96
+
97
+ expect(x)
98
+ .toBeDefined()
99
+ .toBeObject()
100
+ .notToBeSomeObject()
101
+ .toBeInstanceOf(Bar)
102
+ .toBeInstanceOf(Foo)
103
+ .notToBeInstanceOf(Buz);
104
+ },
105
+ ]
106
+ ];
107
+
108
+ const runner = new TestRunner();
109
+
110
+ runner.run(tests).then((result) => {
111
+ runner.report(result.failed > 0);
112
+ });