forked from dead-claudia/thallium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thallium.js
5743 lines (4907 loc) · 464 KB
/
thallium.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict"
/**
* Core TDD-style assertions. These are done by a composition of DSLs, since
* there is *so* much repetition. Also, this is split into several namespaces to
* keep the file size manageable.
*/
var Util = require("./lib/assert/util")
var Type = require("./lib/assert/type")
var Equal = require("./lib/assert/equal")
var Throws = require("./lib/assert/throws")
var Has = require("./lib/assert/has")
var Includes = require("./lib/assert/includes")
var HasKeys = require("./lib/assert/has-keys")
exports.AssertionError = Util.AssertionError
exports.assert = Util.assert
exports.fail = Util.fail
exports.format = Util.format
exports.escape = Util.escape
exports.ok = Type.ok
exports.notOk = Type.notOk
exports.isBoolean = Type.isBoolean
exports.notBoolean = Type.notBoolean
exports.isFunction = Type.isFunction
exports.notFunction = Type.notFunction
exports.isNumber = Type.isNumber
exports.notNumber = Type.notNumber
exports.isObject = Type.isObject
exports.notObject = Type.notObject
exports.isString = Type.isString
exports.notString = Type.notString
exports.isSymbol = Type.isSymbol
exports.notSymbol = Type.notSymbol
exports.exists = Type.exists
exports.notExists = Type.notExists
exports.isArray = Type.isArray
exports.notArray = Type.notArray
exports.is = Type.is
exports.not = Type.not
exports.equal = Equal.equal
exports.notEqual = Equal.notEqual
exports.equalLoose = Equal.equalLoose
exports.notEqualLoose = Equal.notEqualLoose
exports.deepEqual = Equal.deepEqual
exports.notDeepEqual = Equal.notDeepEqual
exports.match = Equal.match
exports.notMatch = Equal.notMatch
exports.atLeast = Equal.atLeast
exports.atMost = Equal.atMost
exports.above = Equal.above
exports.below = Equal.below
exports.between = Equal.between
exports.closeTo = Equal.closeTo
exports.notCloseTo = Equal.notCloseTo
exports.throws = Throws.throws
exports.throwsMatch = Throws.throwsMatch
exports.hasOwn = Has.hasOwn
exports.notHasOwn = Has.notHasOwn
exports.hasOwnLoose = Has.hasOwnLoose
exports.notHasOwnLoose = Has.notHasOwnLoose
exports.hasKey = Has.hasKey
exports.notHasKey = Has.notHasKey
exports.hasKeyLoose = Has.hasKeyLoose
exports.notHasKeyLoose = Has.notHasKeyLoose
exports.has = Has.has
exports.notHas = Has.notHas
exports.hasLoose = Has.hasLoose
exports.notHasLoose = Has.notHasLoose
/**
* There's 2 sets of 12 permutations here for `includes` and `hasKeys`, instead
* of N sets of 2 (which would fit the `foo`/`notFoo` idiom better), so it's
* easier to just make a couple separate DSLs and use that to define everything.
*
* Here's the top level:
*
* - shallow
* - strict deep
* - structural deep
*
* And the second level:
*
* - includes all/not missing some
* - includes some/not missing all
* - not including all/missing some
* - not including some/missing all
*
* Here's an example using the naming scheme for `hasKeys*`
*
* | shallow | strict deep | structural deep
* --------------|-----------------|---------------------|----------------------
* includes all | `hasKeys` | `hasKeysDeep` | `hasKeysMatch`
* includes some | `hasKeysAny` | `hasKeysAnyDeep` | `hasKeysAnyMatch`
* missing some | `notHasKeysAll` | `notHasKeysAllDeep` | `notHasKeysAllMatch`
* missing all | `notHasKeys` | `notHasKeysDeep` | `notHasKeysMatch`
*
* Note that the `hasKeys` shallow comparison variants are also overloaded to
* consume either an array (in which it simply checks against a list of keys) or
* an object (where it does a full deep comparison).
*/
exports.includes = Includes.includes
exports.includesDeep = Includes.includesDeep
exports.includesMatch = Includes.includesMatch
exports.includesAny = Includes.includesAny
exports.includesAnyDeep = Includes.includesAnyDeep
exports.includesAnyMatch = Includes.includesAnyMatch
exports.notIncludesAll = Includes.notIncludesAll
exports.notIncludesAllDeep = Includes.notIncludesAllDeep
exports.notIncludesAllMatch = Includes.notIncludesAllMatch
exports.notIncludes = Includes.notIncludes
exports.notIncludesDeep = Includes.notIncludesDeep
exports.notIncludesMatch = Includes.notIncludesMatch
exports.hasKeys = HasKeys.hasKeys
exports.hasKeysDeep = HasKeys.hasKeysDeep
exports.hasKeysMatch = HasKeys.hasKeysMatch
exports.hasKeysAny = HasKeys.hasKeysAny
exports.hasKeysAnyDeep = HasKeys.hasKeysAnyDeep
exports.hasKeysAnyMatch = HasKeys.hasKeysAnyMatch
exports.notHasKeysAll = HasKeys.notHasKeysAll
exports.notHasKeysAllDeep = HasKeys.notHasKeysAllDeep
exports.notHasKeysAllMatch = HasKeys.notHasKeysAllMatch
exports.notHasKeys = HasKeys.notHasKeys
exports.notHasKeysDeep = HasKeys.notHasKeysDeep
exports.notHasKeysMatch = HasKeys.notHasKeysMatch
},{"./lib/assert/equal":7,"./lib/assert/has":9,"./lib/assert/has-keys":8,"./lib/assert/includes":10,"./lib/assert/throws":11,"./lib/assert/type":12,"./lib/assert/util":13}],2:[function(require,module,exports){
"use strict"
/**
* Main entry point, for those wanting to use this framework with the core
* assertions.
*/
var Thallium = require("./lib/api/thallium")
module.exports = new Thallium()
},{"./lib/api/thallium":6}],3:[function(require,module,exports){
"use strict"
var Thallium = require("./lib/api/thallium")
var Reports = require("./lib/core/reports")
var Types = Reports.Types
exports.root = function () {
return new Thallium()
}
function d(duration) {
if (duration == null) return 10
if (typeof duration === "number") return duration|0
throw new TypeError("Expected `duration` to be a number if it exists")
}
function s(slow) {
if (slow == null) return 75
if (typeof slow === "number") return slow|0
throw new TypeError("Expected `slow` to be a number if it exists")
}
function p(path) {
if (Array.isArray(path)) return path
throw new TypeError("Expected `path` to be an array of locations")
}
function h(value) {
if (value != null && typeof value._ === "number") return value
throw new TypeError("Expected `value` to be a hook error")
}
/**
* Create a new report, mainly for testing reporters.
*/
exports.reports = {
start: function () {
return new Reports.Start()
},
enter: function (path, duration, slow) {
return new Reports.Enter(p(path), d(duration), s(slow))
},
leave: function (path) {
return new Reports.Leave(p(path))
},
pass: function (path, duration, slow) {
return new Reports.Pass(p(path), d(duration), s(slow))
},
fail: function (path, value, duration, slow) {
return new Reports.Fail(p(path), value, d(duration), s(slow))
},
skip: function (path) {
return new Reports.Skip(p(path))
},
end: function () {
return new Reports.End()
},
error: function (value) {
return new Reports.Error(value)
},
hook: function (path, rootPath, value) {
return new Reports.Hook(p(path), p(rootPath), h(value))
},
}
/**
* Create a new hook error, mainly for testing reporters.
*/
exports.hookErrors = {
beforeAll: function (func, value) {
return new Reports.HookError(Types.BeforeAll, func, value)
},
beforeEach: function (func, value) {
return new Reports.HookError(Types.BeforeEach, func, value)
},
afterEach: function (func, value) {
return new Reports.HookError(Types.AfterEach, func, value)
},
afterAll: function (func, value) {
return new Reports.HookError(Types.AfterAll, func, value)
},
}
/**
* Creates a new location, mainly for testing reporters.
*/
exports.location = function (name, index) {
if (typeof name !== "string") {
throw new TypeError("Expected `name` to be a string")
}
if (typeof index !== "number") {
throw new TypeError("Expected `index` to be a number")
}
return {name: name, index: index|0}
}
},{"./lib/api/thallium":6,"./lib/core/reports":15}],4:[function(require,module,exports){
"use strict"
exports.addHook = function (list, callback) {
if (list != null) {
list.push(callback)
return list
} else {
return [callback]
}
}
exports.removeHook = function (list, callback) {
if (list == null) return undefined
if (list.length === 1) {
if (list[0] === callback) return undefined
} else {
var index = list.indexOf(callback)
if (index >= 0) list.splice(index, 1)
}
return list
}
exports.hasHook = function (list, callback) {
if (list == null) return false
if (list.length > 1) return list.indexOf(callback) >= 0
return list[0] === callback
}
},{}],5:[function(require,module,exports){
"use strict"
var methods = require("../methods")
var Tests = require("../core/tests")
var Hooks = require("./hooks")
/**
* This contains the low level, more arcane things that are generally not
* interesting to anyone other than plugin developers.
*/
module.exports = Reflect
function Reflect(test) {
var reflect = test.reflect
if (reflect != null) return reflect
if (test.root !== test) return test.reflect = new ReflectChild(test)
return test.reflect = new ReflectRoot(test)
}
methods(Reflect, {
/**
* Get the currently executing test.
*/
get current() {
return new Reflect(this._.root.current)
},
/**
* Get the root test.
*/
get root() {
return new Reflect(this._.root)
},
/**
* Get the current total test count.
*/
get count() {
return this._.tests == null ? 0 : this._.tests.length
},
/**
* Get a copy of the current test list, as a Reflect collection. This is
* intentionally a slice, so you can't mutate the real children.
*/
get children() {
if (this._.tests == null) return []
return this._.tests.map(function (test) {
return new ReflectChild(test)
})
},
/**
* Is this test the root, i.e. top level?
*/
get isRoot() {
return this._.root === this._
},
/**
* Is this locked (i.e. unsafe to modify)?
*/
get isLocked() {
return !!this._.locked
},
/**
* Get the own, not necessarily active, timeout. 0 means inherit the
* parent's, and `Infinity` means it's disabled.
*/
get ownTimeout() {
return this._.timeout || 0
},
/**
* Get the active timeout in milliseconds, not necessarily own, or the
* framework default of 2000, if none was set.
*/
get timeout() {
return Tests.timeout(this._)
},
/**
* Get the own, not necessarily active, slow threshold. 0 means inherit the
* parent's, and `Infinity` means it's disabled.
*/
get ownSlow() {
return this._.slow || 0
},
/**
* Get the active slow threshold in milliseconds, not necessarily own, or
* the framework default of 75, if none was set.
*/
get slow() {
return Tests.slow(this._)
},
/**
* Add a hook to be run before each subtest, including their subtests and so
* on.
*/
before: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
this._.beforeEach = Hooks.addHook(this._.beforeEach, callback)
},
/**
* Add a hook to be run once before all subtests are run.
*/
beforeAll: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
this._.beforeAll = Hooks.addHook(this._.beforeAll, callback)
},
/**
* Add a hook to be run after each subtest, including their subtests and so
* on.
*/
after: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
this._.afterEach = Hooks.addHook(this._.afterEach, callback)
},
/**
* Add a hook to be run once after all subtests are run.
*/
afterAll: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
this._.afterAll = Hooks.addHook(this._.afterAll, callback)
},
/**
* Remove a hook previously added with `t.before` or `reflect.before`.
*/
hasBefore: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
return Hooks.hasHook(this._.beforeEach, callback)
},
/**
* Remove a hook previously added with `t.beforeAll` or `reflect.beforeAll`.
*/
hasBeforeAll: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
return Hooks.hasHook(this._.beforeAll, callback)
},
/**
* Remove a hook previously added with `t.after` or`reflect.after`.
*/
hasAfter: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
return Hooks.hasHook(this._.afterEach, callback)
},
/**
* Remove a hook previously added with `t.afterAll` or `reflect.afterAll`.
*/
hasAfterAll: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
return Hooks.hasHook(this._.afterAll, callback)
},
/**
* Remove a hook previously added with `t.before` or `reflect.before`.
*/
removeBefore: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
var beforeEach = Hooks.removeHook(this._.beforeEach, callback)
if (beforeEach == null) delete this._.beforeEach
else this._.beforeEach = beforeEach
},
/**
* Remove a hook previously added with `t.beforeAll` or `reflect.beforeAll`.
*/
removeBeforeAll: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
var beforeAll = Hooks.removeHook(this._.beforeAll, callback)
if (beforeAll == null) delete this._.beforeAll
else this._.beforeAll = beforeAll
},
/**
* Remove a hook previously added with `t.after` or`reflect.after`.
*/
removeAfter: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
var afterEach = Hooks.removeHook(this._.afterEach, callback)
if (afterEach == null) delete this._.afterEach
else this._.afterEach = afterEach
},
/**
* Remove a hook previously added with `t.afterAll` or `reflect.afterAll`.
*/
removeAfterAll: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
var afterAll = Hooks.removeHook(this._.afterAll, callback)
if (afterAll == null) delete this._.afterAll
else this._.afterAll = afterAll
},
/**
* Add a block or inline test.
*/
test: function (name, callback) {
if (typeof name !== "string") {
throw new TypeError("Expected `name` to be a string")
}
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
Tests.addNormal(this._.root.current, name, callback)
},
/**
* Add a skipped block or inline test.
*/
testSkip: function (name, callback) {
if (typeof name !== "string") {
throw new TypeError("Expected `name` to be a string")
}
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
Tests.addSkipped(this._.root.current, name)
},
})
function ReflectRoot(root) {
this._ = root
}
methods(ReflectRoot, Reflect, {
/**
* Whether a reporter was registered.
*/
hasReporter: function (reporter) {
if (typeof reporter !== "function") {
throw new TypeError("Expected `reporter` to be a function")
}
return this._.root.reporterIds.indexOf(reporter) >= 0
},
/**
* Add a reporter.
*/
reporter: function (reporter, arg) {
if (typeof reporter !== "function") {
throw new TypeError("Expected `reporter` to be a function")
}
var root = this._.root
if (root.current !== root) {
throw new Error("Reporters may only be added to the root")
}
if (root.reporterIds.indexOf(reporter) < 0) {
root.reporterIds.push(reporter)
root.reporters.push(reporter(arg))
}
},
/**
* Remove a reporter.
*/
removeReporter: function (reporter) {
if (typeof reporter !== "function") {
throw new TypeError("Expected `reporter` to be a function")
}
var root = this._.root
if (root.current !== root) {
throw new Error("Reporters may only be added to the root")
}
var index = root.reporterIds.indexOf(reporter)
if (index >= 0) {
root.reporterIds.splice(index, 1)
root.reporters.splice(index, 1)
}
},
})
function ReflectChild(root) {
this._ = root
}
methods(ReflectChild, Reflect, {
/**
* Get the test name, or `undefined` if it's the root test.
*/
get name() {
return this._.name
},
/**
* Get the test index, or `-1` if it's the root test.
*/
get index() {
return this._.index
},
/**
* Get the parent test as a Reflect.
*/
get parent() {
return new Reflect(this._.parent)
},
})
},{"../core/tests":16,"../methods":17,"./hooks":4}],6:[function(require,module,exports){
"use strict"
var methods = require("../methods")
var Tests = require("../core/tests")
var onlyAdd = require("../core/only").onlyAdd
var addHook = require("./hooks").addHook
var Reflect = require("./reflect")
module.exports = Thallium
function Thallium() {
this._ = Tests.createRoot(this)
// ES6 module transpiler compatibility.
this.default = this
}
methods(Thallium, {
/**
* Call a plugin and return the result. The plugin is called with a Reflect
* instance for access to plenty of potentially useful internal details.
*/
call: function (plugin, arg) {
var reflect = new Reflect(this._.root.current)
return plugin.call(reflect, reflect, arg)
},
/**
* Whitelist specific tests, using array-based selectors where each entry
* is either a string or regular expression.
*/
only: function (/* ...selectors */) {
onlyAdd.apply(this._.root.current, arguments)
},
/**
* Add a reporter.
*/
reporter: function (reporter, arg) {
if (typeof reporter !== "function") {
throw new TypeError("Expected `reporter` to be a function.")
}
var root = this._.root
if (root.current !== root) {
throw new Error("Reporters may only be added to the root.")
}
var result = reporter(arg)
// Don't assume it's a function. Verify it actually is, so we don't have
// inexplicable type errors internally after it's invoked, and so users
// won't get too confused.
if (typeof result !== "function") {
throw new TypeError(
"Expected `reporter` to return a function. Check with the " +
"reporter's author, and have them fix their reporter.")
}
root.reporter = result
},
/**
* Get the current timeout. 0 means inherit the parent's, and `Infinity`
* means it's disabled.
*/
get timeout() {
return Tests.timeout(this._.root.current)
},
/**
* Set the timeout in milliseconds, rounding negatives to 0. Setting the
* timeout to 0 means to inherit the parent timeout, and setting it to
* `Infinity` disables it.
*/
set timeout(timeout) {
var calculated = Math.floor(Math.max(+timeout, 0))
if (calculated === 0) delete this._.root.current.timeout
else this._.root.current.timeout = calculated
},
/**
* Get the current slow threshold. 0 means inherit the parent's, and
* `Infinity` means it's disabled.
*/
get slow() {
return Tests.slow(this._.root.current)
},
/**
* Set the slow threshold in milliseconds, rounding negatives to 0. Setting
* the timeout to 0 means to inherit the parent threshold, and setting it to
* `Infinity` disables it.
*/
set slow(slow) {
var calculated = Math.floor(Math.max(+slow, 0))
if (calculated === 0) delete this._.root.current.slow
else this._.root.current.slow = calculated
},
/**
* Run the tests (or the test's tests if it's not a base instance).
*/
run: function () {
if (this._.root !== this._) {
throw new Error(
"Only the root test can be run - If you only want to run a " +
"subtest, use `t.only([\"selector1\", ...])` instead.")
}
if (this._.root.locked) {
throw new Error("Can't run while tests are already running.")
}
return Tests.runTest(this._)
},
/**
* Add a test.
*/
test: function (name, callback) {
if (typeof name !== "string") {
throw new TypeError("Expected `name` to be a string")
}
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
Tests.addNormal(this._.root.current, name, callback)
},
/**
* Add a skipped test.
*/
testSkip: function (name, callback) {
if (typeof name !== "string") {
throw new TypeError("Expected `name` to be a string")
}
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
Tests.addSkipped(this._.root.current, name)
},
before: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
var test = this._.root.current
test.beforeEach = addHook(test.beforeEach, callback)
},
beforeAll: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
var test = this._.root.current
test.beforeAll = addHook(test.beforeAll, callback)
},
after: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
var test = this._.root.current
test.afterEach = addHook(test.afterEach, callback)
},
afterAll: function (callback) {
if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a function if passed")
}
var test = this._.root.current
test.afterAll = addHook(test.afterAll, callback)
},
})
},{"../core/only":14,"../core/tests":16,"../methods":17,"./hooks":4,"./reflect":5}],7:[function(require,module,exports){
"use strict"
var match = require("../../match")
var Util = require("./util")
function binary(numeric, comparator, message) {
return function (actual, expected) {
if (numeric) {
if (typeof actual !== "number") {
throw new TypeError("`actual` must be a number")
}
if (typeof expected !== "number") {
throw new TypeError("`expected` must be a number")
}
}
if (!comparator(actual, expected)) {
Util.fail(message, {actual: actual, expected: expected})
}
}
}
exports.equal = binary(false,
function (a, b) { return Util.strictIs(a, b) },
"Expected {actual} to equal {expected}")
exports.notEqual = binary(false,
function (a, b) { return !Util.strictIs(a, b) },
"Expected {actual} to not equal {expected}")
exports.equalLoose = binary(false,
function (a, b) { return Util.looseIs(a, b) },
"Expected {actual} to loosely equal {expected}")
exports.notEqualLoose = binary(false,
function (a, b) { return !Util.looseIs(a, b) },
"Expected {actual} to not loosely equal {expected}")
exports.atLeast = binary(true,
function (a, b) { return a >= b },
"Expected {actual} to be at least {expected}")
exports.atMost = binary(true,
function (a, b) { return a <= b },
"Expected {actual} to be at most {expected}")
exports.above = binary(true,
function (a, b) { return a > b },
"Expected {actual} to be above {expected}")
exports.below = binary(true,
function (a, b) { return a < b },
"Expected {actual} to be below {expected}")
exports.between = function (actual, lower, upper) {
if (typeof actual !== "number") {
throw new TypeError("`actual` must be a number")
}
if (typeof lower !== "number") {
throw new TypeError("`lower` must be a number")
}
if (typeof upper !== "number") {
throw new TypeError("`upper` must be a number")
}
// The negation is to address NaNs as well, without writing a ton of special
// case boilerplate
if (!(actual >= lower && actual <= upper)) {
Util.fail("Expected {actual} to be between {lower} and {upper}", {
actual: actual,
lower: lower,
upper: upper,
})
}
}
exports.deepEqual = binary(false,
function (a, b) { return match.strict(a, b) },
"Expected {actual} to deeply equal {expected}")
exports.notDeepEqual = binary(false,
function (a, b) { return !match.strict(a, b) },
"Expected {actual} to not deeply equal {expected}")
exports.match = binary(false,
function (a, b) { return match.match(a, b) },
"Expected {actual} to match {expected}")
exports.notMatch = binary(false,
function (a, b) { return !match.match(a, b) },
"Expected {actual} to not match {expected}")
// Uses division to allow for a more robust comparison of floats. Also, this
// handles near-zero comparisons correctly, as well as a zero tolerance (i.e.
// exact comparison).
function closeTo(expected, actual, tolerance) {
if (tolerance === Infinity || actual === expected) return true
if (tolerance === 0) return false
if (actual === 0) return Math.abs(expected) < tolerance
if (expected === 0) return Math.abs(actual) < tolerance
return Math.abs(expected / actual - 1) < tolerance
}
// Note: these two always fail when dealing with NaNs.
exports.closeTo = function (expected, actual, tolerance) {
if (typeof actual !== "number") {
throw new TypeError("`actual` must be a number")
}
if (typeof expected !== "number") {
throw new TypeError("`expected` must be a number")
}
if (tolerance == null) tolerance = 1e-10
if (typeof tolerance !== "number" || tolerance < 0) {
throw new TypeError(
"`tolerance` must be a non-negative number if given")
}
if (actual !== actual || expected !== expected || // eslint-disable-line no-self-compare, max-len
!closeTo(expected, actual, tolerance)) {
Util.fail("Expected {actual} to be close to {expected}", {
actual: actual,
expected: expected,
})
}
}
exports.notCloseTo = function (expected, actual, tolerance) {
if (typeof actual !== "number") {
throw new TypeError("`actual` must be a number")
}
if (typeof expected !== "number") {
throw new TypeError("`expected` must be a number")
}
if (tolerance == null) tolerance = 1e-10
if (typeof tolerance !== "number" || tolerance < 0) {
throw new TypeError(
"`tolerance` must be a non-negative number if given")
}
if (expected !== expected || actual !== actual || // eslint-disable-line no-self-compare, max-len
closeTo(expected, actual, tolerance)) {
Util.fail("Expected {actual} to not be close to {expected}", {
actual: actual,
expected: expected,
})
}
}
},{"../../match":26,"./util":13}],8:[function(require,module,exports){
"use strict"
var match = require("../../match")