/
unexpected.spec.js
1502 lines (1305 loc) · 65.6 KB
/
unexpected.spec.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
/*global describe, it, expect, beforeEach, setTimeout, Uint8Array, Uint16Array*/
// use this instead of Object.create in order to make the tests run in
// browsers that are not es5 compatible.
function create(o) {
function F() {}
F.prototype = o;
return new F();
}
function itSkipIf(condition) {
(condition ? it.skip : it).apply(it, Array.prototype.slice.call(arguments, 1));
}
describe('unexpected', function () {
describe('argument validation', function () {
it('fails when given no parameters', function () {
expect(function () {
expect();
}, 'to throw', 'The expect functions requires at least two parameters.');
});
it('fails when given only one parameter', function () {
expect(function () {
expect({});
}, 'to throw', 'The expect functions requires at least two parameters.');
});
it('fails when the second parameter is not a string', function () {
expect(function () {
expect({}, {});
}, 'to throw', 'The expect functions requires second parameter to be a string.');
});
});
describe('ok/truthy/falsy assertion', function () {
it('assert that the value is truthy or not', function () {
expect(1, 'to be ok');
expect(true, 'to be ok');
expect(true, 'not to be falsy');
expect({}, 'to be truthy');
expect(0, 'not to be ok');
expect(0, 'to be falsy');
expect(null, 'to be falsy');
expect(undefined, 'to be falsy');
});
it('throws when the assertion fails', function () {
expect(function () {
expect(0, 'to be ok');
}, 'to throw exception', 'expected 0 to be ok');
});
it('throws with message when the assertion fails', function () {
expect(function () {
expect(4 < 4, 'to be ok', '4 < 4');
}, "to throw exception", "expected false to be ok '4 < 4'");
});
it('formats Error instances correctly when an assertion fails', function () {
expect(function () {
expect(new Error('error message'), 'to be a number');
}, 'to throw', "expected [Error: error message] to be a number");
});
it('throws with a stack trace that has the calling function as the top frame when the assertion fails (if the environment supports it)', function () {
if (Error.captureStackTrace || 'stack' in new Error()) {
expect(function TheCallingFunction() {
expect(4 < 4, 'to be ok');
}, 'to throw exception', function (err) {
expect(err.stack.split('\n')[1], 'to match', /TheCallingFunction/);
});
}
});
});
describe('be assertion', function () {
it('assert === equality', function () {
var obj = {};
expect(obj, 'to be', obj);
expect(obj, 'not to be', {});
expect(1, 'to be', 1);
expect(1, 'not to be', true);
expect('1', 'not to be', 1);
expect(null, 'not to be', undefined);
expect(null, 'to be null');
expect(0, 'not to be null');
expect(undefined, 'not to be null');
expect(true, 'to be true');
expect(false, 'not to be true');
expect(false, 'to be false');
expect(true, 'not to be false');
expect(undefined, 'to be undefined');
});
itSkipIf(typeof Buffer === 'undefined', 'asserts === equality for Buffers', function () {
var buffer = new Buffer([0x45, 0x59]);
expect(buffer, 'to be', buffer);
});
itSkipIf(typeof Uint8Array === 'undefined', 'asserts === equality for Uint8Array', function () {
var uint8Array = new Uint8Array([0x45, 0x59]);
expect(uint8Array, 'to be', uint8Array);
});
it('throws when the assertion fails', function () {
expect(function () {
expect('foo', 'to be', 'bar');
}, 'to throw exception', "expected 'foo' to be 'bar'");
expect(function () {
expect(true, 'not to be', true);
}, 'to throw exception', "expected true not to be true");
});
});
describe('a/an assertion', function () {
it('asserts typeof with support for array type and instanceof', function () {
expect(5, 'to be a', 'number');
expect(5, 'to be a number');
expect('abc', 'to be a', 'string');
expect('', 'to be a string');
expect('', 'to be the empty string');
expect('', 'to be an empty string');
expect('abc', 'to be a non-empty string');
expect([], 'to be an', 'array');
expect([], 'to be an array');
expect(['abc'], 'to be an array of strings');
expect([{}], 'to be an array of objects');
expect([{}], 'to be a non-empty array of objects');
expect([/foo/, /bar/], 'to be a non-empty array of regexps');
expect([[], [], []], 'to be an array of arrays');
expect(['abc'], 'to be a non-empty array of strings');
expect([], 'to be an empty array');
expect({}, 'to be an', Object);
expect({}, 'to be an empty object');
expect({foo: 123}, 'to be a non-empty object');
expect([123], 'to be a non-empty array');
expect([], 'to be an', 'object');
expect([], 'to be an object');
expect([], 'to be an empty object');
expect([], 'to be an', Array);
expect(/ab/, 'to be a', RegExp);
expect(/ab/, 'to be a regexp');
expect(123, 'not to be a regex');
expect(/ab/, 'to be a regex');
expect(/ab/, 'to be a regular expression');
expect(123, 'not to be a regular expression');
expect(null, 'not to be an', 'object');
expect(null, 'not to be an object');
expect(true, 'to be a', 'boolean');
expect(true, 'to be a boolean');
expect(expect, 'to be a', 'function');
expect(expect, 'to be a function');
});
it('throws when the assertion fails', function () {
expect(function () {
expect(5, 'to be a', Array);
}, 'to throw exception', /expected 5 to be a \[Function(: Array)?\]/);
expect(function () {
expect([], 'not to be an', 'array');
}, 'to throw exception', "expected [] not to be an 'array'");
});
});
describe('equal assertion', function () {
it('asserts deep equality that works with objects', function () {
expect({ a: 'b' }, 'to equal', { a: 'b' });
expect(1, 'not to equal', '1');
expect({ foo: 1 }, 'not to equal', { foo: '1' });
expect(1, 'to equal', 1);
expect(null, 'not to equal', '1');
var now = new Date();
expect(now, 'to equal', now);
expect(now, 'to equal', new Date(now.getTime()));
expect({ now: now }, 'to equal', { now: now });
expect(null, 'to equal', null);
expect(null, 'not to equal', undefined);
expect(undefined, 'to equal', undefined);
expect(true, 'to equal', true);
expect(false, 'to equal', false);
expect({ a: { b: 'c' } }, 'to equal', { a: { b: 'c' } });
expect({ a: { b: 'c' } }, 'not to equal', { a: { b: 'd' } });
expect(/foo/, 'to equal', /foo/);
expect(/foo/i, 'not to equal', /foo/);
expect(/foo/gm, 'to equal', /foo/gm);
expect(/foo/m, 'not to equal', /foo/i);
expect(/foo/m, 'to equal', new RegExp('foo', 'm'));
});
itSkipIf(typeof Buffer === 'undefined', 'asserts equality for Buffer instances', function () {
expect(new Buffer([0x45, 0x59]), 'to equal', new Buffer([0x45, 0x59]));
});
itSkipIf(typeof Uint8Array === 'undefined', 'asserts equality for Uint8Array', function () {
expect(new Uint8Array([0x45, 0x59]), 'to equal', new Uint8Array([0x45, 0x59]));
});
it('fails gracefully when comparing circular structures', function () {
var foo = {},
bar = {};
foo.foo = foo;
bar.foo = bar;
expect(function () {
expect(foo, 'not to equal', bar);
}, 'to throw', 'Cannot compare circular structures');
});
it('throws when the assertion fails', function () {
expect(function () {
expect({ a: { b: 'c'} }, 'to equal', { a: { b: 'd'} });
}, 'to throw exception', "expected { a: { b: 'c' } } to equal { a: { b: 'd' } }");
expect(function () {
expect({ a: 'b' }, 'not to equal', { a: 'b' });
}, 'to throw exception', "expected { a: 'b' } not to equal { a: 'b' }");
});
it("throws an error with 'expected' and 'actual' properties when not negated", function () {
var expected = 123,
actual = 456;
expect(function () {
expect(actual, 'to equal', expected);
}, 'to throw exception', function (e) {
expect(e.expected, 'to equal', expected);
expect(e.actual, 'to equal', actual);
});
});
it("throws an error without 'expected' and 'actual' properties when negated", function () {
expect(function () {
expect(123, 'not to equal', 123);
}, 'to throw exception', function (e) {
expect(e.expected, 'not to be ok');
expect(e.actual, 'not to be ok');
});
});
it("throws an error with showDiff:true when comparing arrays and not negated", function () {
expect(function () {
expect([1], 'to equal', [2]);
}, 'to throw exception', function (e) {
expect(e.showDiff, 'to be ok');
});
});
it("throws an error with showDiff:true when comparing objects and not negated", function () {
expect(function () {
expect({foo: 1}, 'to equal', {foo: 2});
}, 'to throw exception', function (e) {
expect(e.showDiff, 'to be ok');
});
});
it("throws an error with showDiff:true when comparing an object to an array", function () {
expect(function () {
expect({foo: 1}, 'to equal', []);
}, 'to throw exception', function (e) {
expect(e.showDiff, 'not to be ok');
});
});
it("throws an error without showDiff:true when negated", function () {
expect(function () {
expect({foo: 1}, 'not to equal', {foo: 1});
}, 'to throw exception', function (e) {
expect(e.showDiff, 'not to be ok');
});
});
itSkipIf(typeof Buffer === 'undefined', 'produces a hex-diff in JSON when Buffers differ', function () {
expect(function () {
expect(
new Buffer('\x00\x01\x02Here is the thing I was talking about', 'utf-8'),
'to equal',
new Buffer('\x00\x01\x02Here is the thing I was quuxing about', 'utf-8')
);
}, 'to throw', function (err) {
expect(err, 'to have properties', {
showDiff: true,
actual: {
$Buffer: [
'00 01 02 48 65 72 65 20 69 73 20 74 68 65 20 74 |...Here is the t|',
'68 69 6E 67 20 49 20 77 61 73 20 74 61 6C 6B 69 |hing I was talki|',
'6E 67 20 61 62 6F 75 74 |ng about|'
]
},
expected: {
$Buffer: [
'00 01 02 48 65 72 65 20 69 73 20 74 68 65 20 74 |...Here is the t|',
'68 69 6E 67 20 49 20 77 61 73 20 71 75 75 78 69 |hing I was quuxi|',
'6E 67 20 61 62 6F 75 74 |ng about|'
]
}
});
});
});
itSkipIf(typeof Uint8Array === 'undefined', 'produces a hex-diff in JSON when Uint8Arrays differ', function () {
expect(function () {
expect(
new Uint8Array([
0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74,
0x68, 0x69, 0x6E, 0x67, 0x20, 0x49, 0x20, 0x77, 0x61, 0x73, 0x20, 0x74, 0x61, 0x6C, 0x6B, 0x69,
0x6E, 0x67, 0x20, 0x61, 0x62, 0x6F, 0x75, 0x74
]),
'to equal',
new Uint8Array([
0x00, 0x01, 0x02, 0x48, 0x65, 0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74,
0x68, 0x69, 0x6E, 0x67, 0x20, 0x49, 0x20, 0x77, 0x61, 0x73, 0x20, 0x71, 0x75, 0x75, 0x78, 0x69,
0x6E, 0x67, 0x20, 0x61, 0x62, 0x6F, 0x75, 0x74
])
);
}, 'to throw', function (err) {
expect(err, 'to have properties', {
showDiff: true,
actual: {
$Uint8Array: [
'00 01 02 48 65 72 65 20 69 73 20 74 68 65 20 74 |...Here is the t|',
'68 69 6E 67 20 49 20 77 61 73 20 74 61 6C 6B 69 |hing I was talki|',
'6E 67 20 61 62 6F 75 74 |ng about|'
]
},
expected: {
$Uint8Array: [
'00 01 02 48 65 72 65 20 69 73 20 74 68 65 20 74 |...Here is the t|',
'68 69 6E 67 20 49 20 77 61 73 20 71 75 75 78 69 |hing I was quuxi|',
'6E 67 20 61 62 6F 75 74 |ng about|'
]
}
});
});
});
itSkipIf(typeof Uint16Array === 'undefined', 'produces a hex-diff in JSON when Uint16Arrays differ', function () {
expect(function () {
expect(
new Uint16Array([
0x0001, 0x0248, 0x6572, 0x6520, 0x6973, 0x2074, 0x6865, 0x2074,
0x6869, 0x6E67, 0x2049, 0x2077, 0x6173, 0x2074, 0x616C, 0x6B69,
0x6E67, 0x2061, 0x626F, 0x7574
]),
'to equal',
new Uint16Array([
0x0001, 0x0248, 0x6572, 0x6520, 0x6973, 0x2074, 0x6865, 0x2074,
0x6869, 0x6E67, 0x2049, 0x2077, 0x6173, 0x2071, 0x7575, 0x7869,
0x6E67, 0x2061, 0x626F, 0x7574
])
);
}, 'to throw', function (err) {
expect(err, 'to have properties', {
showDiff: true,
actual: {
$Uint16Array: [
'0001 0248 6572 6520 6973 2074 6865 2074',
'6869 6E67 2049 2077 6173 2074 616C 6B69',
'6E67 2061 626F 7574'
]
},
expected: {
$Uint16Array: [
'0001 0248 6572 6520 6973 2074 6865 2074',
'6869 6E67 2049 2077 6173 2071 7575 7869',
'6E67 2061 626F 7574'
]
}
});
});
});
});
describe('exception assertion', function () {
it('fails if no exception is thrown', function () {
expect(function () {
expect(function () {
// Don't throw
}, 'to throw exception');
}, 'to throw', 'expected [Function] to throw exception');
});
it('fails if expection is throw', function () {
expect(function () {
expect(function () {
throw new Error('The exception message');
}, 'not to throw exception');
}, 'to throw');
expect(function () {
expect(function () {
throw new Error('The exception message');
}, 'not to throw exception');
}, 'to throw', 'expected [Function] not to throw exception');
});
it('fails if the argument is not a function', function () {
expect(function () {
expect(1, 'to throw exception');
}, 'to throw exception', "Assertion 'to throw exception' only supports functions");
});
it('given a function the function is called with the exception', function () {
expect(function () {
throw new SyntaxError();
}, 'to throw exception', function (e) {
expect(e, 'to be a', SyntaxError);
});
});
it('matches the message against the given regular expression', function () {
expect(function () {
throw new Error('matches the exception message');
}, 'to throw exception', /matches the exception message/);
expect(function () {
throw new Error('Other error');
}, 'not to throw exception', /matches the exception message/);
});
it('exactly matches the message against the given string', function () {
expect(function () {
throw new Error('matches the exception message');
}, 'to throw exception', 'matches the exception message');
expect(function () {
throw new Error('matches the exception message');
}, 'not to throw exception', 'the exception message');
});
});
describe('match assertion', function () {
it('tests that the subject matches the given regular expression', function () {
expect('test', 'to match', /.*st/);
expect('test', 'not to match', /foo/);
expect(null, 'not to match', /foo/);
});
it('throws when the assertion fails', function () {
expect(function () {
expect('test', 'to match', /foo/);
}, 'to throw exception', "expected 'test' to match /foo/");
});
});
describe('contain assertion', function () {
it('asserts indexOf for an array or string', function () {
expect([1, 2], 'to contain', 1);
expect([1, 2], 'to contain', 2, 1);
expect('hello world', 'to contain', 'world');
expect(null, 'not to contain', 'world');
});
it('throws when the assertion fails', function () {
expect(function () {
expect('hello world', 'to contain', 'foo');
}, 'to throw exception', "expected 'hello world' to contain 'foo'");
expect(function () {
expect('hello world', 'to contain', 'hello', 'foo');
}, 'to throw exception', "expected 'hello world' to contain 'hello', 'foo'");
expect(function () {
expect([1, 2], 'to contain', 2, 3);
}, 'to throw exception', "expected [ 1, 2 ] to contain 2, 3");
expect(function () {
expect(1, 'to contain', 1);
}, 'to throw exception', "Assertion 'to contain' only supports strings and arrays");
});
});
describe('length assertion', function () {
it('asserts array .length', function () {
expect([], 'to have length', 0);
expect([1, 2, 3], 'to have length', 3);
expect([1, 2, 3], 'not to have length', 4);
expect({ length: 4 }, 'to have length', 4);
});
it('throws when the assertion fails', function () {
expect(function () {
expect([1, 2], 'to have length', 3);
}, 'to throw exception', "expected [ 1, 2 ] to have length 3");
expect(function () {
expect(null, 'to have length', 4);
}, 'to throw exception', "Assertion 'to have length' only supports array like objects");
expect(function () {
expect({ length: 'foo' }, 'to have length', 4);
}, 'to throw exception', "Assertion 'to have length' only supports array like objects");
});
});
describe('property assertion', function () {
it('asserts presence of an own property (and value optionally)', function () {
expect([1, 2], 'to have property', 'length');
expect([1, 2], 'to have property', 'length', 2);
expect({a: 'b'}, 'to have property', 'a');
expect({a: 'b'}, 'to have property', 'a', 'b');
expect({a: 'b'}, 'to have property', 'toString');
expect({a: 'b'}, 'not to have property', 'b');
expect({a: 'b'}, 'to have own property', 'a');
expect(create({a: 'b'}), 'not to have own property', 'a');
expect(1, 'not to have property', 'a');
expect(null, 'not to have property', 'a');
expect(undefined, 'not to have property', 'a');
expect(true, 'not to have property', 'a');
expect(false, 'not to have property', 'a');
});
it('throws when the assertion fails', function () {
expect(function () {
expect({a: 'b'}, 'to have property', 'b');
}, 'to throw exception', "expected { a: 'b' } to have property 'b'");
expect(function () {
expect(null, 'to have property', 'b');
}, 'to throw exception', "expected null to have property 'b'");
expect(function () {
expect({a: 'b'}, 'to have property', 'a', 'c');
}, 'to throw exception', "expected { a: 'b' } to have property 'a', 'c'");
expect(function () {
// property expectations ignores value if property
expect(null, 'not to have property', 'a', 'b');
}, 'to throw exception', "expected null not to have property 'a', 'b'");
expect(function () {
// property expectations on value expects the property to be present
expect(null, 'not to have own property', 'a', 'b');
}, 'to throw exception', "expected null not to have own property 'a', 'b'");
});
});
describe('properties assertion', function () {
it('asserts presence of a list of properties', function () {
expect({a: 'foo', b: 'bar'}, 'to have properties', ['a', 'b']);
});
it('asserts presence of a list of own properties', function () {
expect({a: 'foo', b: 'bar'}, 'to have own properties', ['a', 'b']);
expect(function () {
var obj = create({a: 'foo', b: 'bar'});
expect(obj, 'to have properties', ['a', 'b']); // should not fail
expect(obj, 'to have own properties', ['a', 'b']); // should fail
}, 'to throw', "expected {} to have own properties [ 'a', 'b' ]");
});
it('asserts absence of a list of properties', function () {
expect({a: 'foo', b: 'bar'}, 'not to have properties', ['c', 'd']);
expect(function () {
expect({a: 'foo', b: 'bar'}, 'not to have properties', ['a', 'd']);
}, 'to throw', "expected { a: 'foo', b: 'bar' } not to have properties [ 'a', 'd' ]");
});
it('asserts absence of a list of own properties', function () {
var obj = create({a: 'foo', b: 'bar'});
expect(obj, 'to have properties', ['a', 'b']);
expect(obj, 'not to have own properties', ['a', 'b']);
expect(function () {
expect({a: 'foo', b: 'bar'}, 'not to have own properties', ['a', 'b']); // should fail
}, 'to throw', "expected { a: 'foo', b: 'bar' } not to have own properties [ 'a', 'b' ]");
});
it('asserts presence and values of an object of properties', function () {
expect({a: 'foo', b: 'bar', c: 'baz', d: { qux: 'qux', quux: 'quux'}}, 'to have properties', {
a: 'foo',
b: 'bar',
d: { qux: 'qux', quux: 'quux'}
});
expect(function () {
expect({a: 'foo', b: 'bar'}, 'to have properties', {c: 'baz'});
}, 'to throw', "expected { a: 'foo', b: 'bar' } to have properties { c: 'baz' }");
expect(function () {
expect({a: 'foo', b: 'bar'}, 'to have properties', {b: 'baz'});
}, 'to throw', "expected { a: 'foo', b: 'bar' } to have properties { b: 'baz' }");
});
it('asserts presence and values of an object of own properties', function () {
expect({a: 'foo', b: 'bar'}, 'to have own properties', {a: 'foo', b: 'bar'});
expect(function () {
var obj = create({a: 'foo', b: 'bar'});
expect(obj, 'to have properties', {a: 'foo', b: 'bar'}); // should not fail
expect(obj, 'to have own properties', {a: 'foo', b: 'bar'}); // should fail
}, 'to throw', "expected {} to have own properties { a: 'foo', b: 'bar' }");
});
it('asserts absence and values of an object of properties', function () {
expect({a: 'foo', b: 'bar'}, 'not to have properties', {c: 'baz', d: 'qux'});
expect(function () {
expect({a: 'foo', b: 'bar'}, 'not to have properties', {a: 'foo', c: 'baz'});
}, 'to throw', "expected { a: 'foo', b: 'bar' } not to have properties { a: 'foo', c: 'baz' }");
});
it('asserts absence and values of an object of own properties', function () {
var obj = create({a: 'foo', b: 'bar'});
expect(obj, 'to have properties', {a: 'foo', b: 'bar'});
expect(obj, 'not to have own properties', {a: 'foo', b: 'bar'});
expect(function () {
expect({a: 'foo', b: 'bar'}, 'not to have own properties', {a: 'foo', b: 'bar'}); // should fail
}, 'to throw', "expected { a: 'foo', b: 'bar' } not to have own properties { a: 'foo', b: 'bar' }");
});
it('should add showDiff:true and diffable actual and expected properties to the error instance', function () {
expect(function () {
expect({a: 123, b: 456, c: 789}, 'to have properties', {a: 123, b: 987});
}, 'to throw', function (e) {
expect(e.showDiff, 'to be true');
expect(e.actual, 'to equal', {a: 123, b: 456, c: 789});
expect(e.expected, 'to equal', {a: 123, b: 987, c: 789});
});
});
it('throws when the assertion fails', function () {
expect(function () {
expect({a: 'foo', b: 'bar'}, 'to have properties', ['c', 'd']);
}, 'to throw', "expected { a: 'foo', b: 'bar' } to have properties [ 'c', 'd' ]");
});
it('throws when given invalid input', function () {
expect(function () {
expect({a: 'foo', b: 'bar'}, 'to have properties', 'a', 'b');
}, 'to throw', "Assertion 'to have properties' only supports input in the form of an Array or an Object.");
});
});
describe('empty assertion', function () {
it('asserts presence of an own property (and value optionally)', function () {
expect([], 'to be empty');
expect('', 'to be empty');
expect({}, 'to be empty');
expect({ length: 0, duck: 'typing' }, 'to be empty');
expect({ my: 'object' }, 'not to be empty');
expect({ my: 'object' }, 'to be non-empty');
expect([1, 2, 3], 'not to be empty');
expect([1, 2, 3], 'to be non-empty');
});
it('throws when the assertion fails', function () {
expect(function () {
expect([1, 2, 3], 'to be empty');
}, 'to throw exception', "expected [ 1, 2, 3 ] to be empty");
expect(function () {
expect('', 'to be non-empty');
}, 'to throw exception', "expected '' to be non-empty");
expect(function () {
expect(null, 'to be empty');
}, 'to throw exception', "Assertion 'to be empty' only supports strings, arrays and objects");
});
});
describe('key assertion', function () {
it('asserts the presence of a key', function () {
expect({ a: 'b' }, 'to have key', 'a');
expect({ a: 'b' }, 'not to have key', 'b');
expect({ a: 'b', c: 'd' }, 'not to have key', 'b');
expect({ a: 'b', c: 'd' }, 'to not only have key', 'a');
expect({ a: 'b', c: 'd' }, 'to only have keys', 'a', 'c');
expect({ a: 'b', c: 'd' }, 'to only have keys', ['a', 'c']);
expect({ a: 'b', c: 'd', e: 'f' }, 'to not only have keys', ['a', 'c']);
expect(null, 'not to have key', 'a');
expect(undefined, 'not to have key', 'a');
expect(true, 'not to have key', 'a');
expect(false, 'not to have key', 'a');
});
it('throws when the assertion fails', function () {
expect(function () {
expect({ a: 'b', b: 'c' }, 'to have key', 'e');
}, 'to throw exception', "expected { a: 'b', b: 'c' } to have key 'e'");
expect(function () {
expect({ a: 'b', b: 'c' }, 'to only have key', 'b');
}, 'to throw exception', "expected { a: 'b', b: 'c' } to only have key 'b'");
expect(function () {
expect({ a: 'b', b: 'c' }, 'not to have key', 'b');
}, 'to throw exception', "expected { a: 'b', b: 'c' } not to have key 'b'");
expect(function () {
expect({ a: 'b', c: 'd' }, 'to not only have keys', ['a', 'c']);
}, 'to throw exception', "expected { a: 'b', c: 'd' } to not only have keys [ 'a', 'c' ]");
expect(function () {
expect({ a: 'b', c: 'd' }, 'to only have keys', 'a', 'd');
}, 'to throw exception', "expected { a: 'b', c: 'd' } to only have keys 'a', 'd'");
expect(function () {
expect({ a: 'b', c: 'd' }, 'to not only have keys', 'a', 'c');
}, 'to throw exception', "expected { a: 'b', c: 'd' } to not only have keys 'a', 'c'");
});
});
describe('finite assertion', function () {
it('asserts a finite number', function () {
expect(123, 'to be finite');
expect(0, 'to be finite');
expect(Infinity, 'not to be finite');
expect(-Infinity, 'not to be finite');
expect(NaN, 'not to be finite');
expect(null, 'not to be finite');
expect({}, 'not to be finite');
it('throws when the assertion fails', function () {
expect(function () {
expect(Infinity, 'to be finite');
}, 'to throw exception', 'expected Infinity to be finite');
});
});
});
describe('finite assertion', function () {
it('asserts a infinite number', function () {
expect(123, 'not to be infinite');
expect(0, 'not to be infinite');
expect(Infinity, 'to be infinite');
expect(-Infinity, 'to be infinite');
expect(NaN, 'not to be infinite');
expect(null, 'not to be infinite');
expect({}, 'not to be infinite');
it('throws when the assertion fails', function () {
expect(function () {
expect(123, 'to be finite');
}, 'to throw exception', 'expected 123 to be infinite');
});
});
});
describe('within assertion', function () {
it('asserts a number within a range', function () {
expect(0, 'to be within', 0, 4);
expect(1, 'to be within', 0, 4);
expect(4, 'to be within', 0, 4);
expect(-1, 'not to be within', 0, 4);
expect(5, 'not to be within', 0, 4);
});
it('throws when the assertion fails', function () {
expect(function () {
expect(4, 'not to be within', 0, 4);
}, 'to throw exception', "expected 4 not to be within '0..4'");
expect(function () {
expect(null, 'not to be within', 0, 4);
}, 'to throw exception', "expected null not to be within '0..4'");
});
});
describe('less than assertion', function () {
it('asserts <', function () {
expect(0, 'to be less than', 4);
expect(0, 'to be below', 1);
expect(3, 'to be <', 4);
expect(3, '<', 4);
});
it('throws when the assertion fails', function () {
expect(function () {
expect(0, 'to be less than', 0);
}, 'to throw exception', "expected 0 to be less than 0");
});
});
describe('less than or equal assertion', function () {
it('asserts <=', function () {
expect(0, 'to be less than or equal to', 4);
expect(4, 'to be <=', 4);
expect(3, '<=', 4);
});
it('throws when the assertion fails', function () {
expect(function () {
expect(0, 'to be less than or equal to', -1);
}, 'to throw exception', "expected 0 to be less than or equal to -1");
});
});
describe('greater than assertion', function () {
it('assert >', function () {
expect(3, 'to be greater than', 2);
expect(1, 'to be above', 0);
expect(4, 'to be >', 3);
expect(4, '>', 3);
});
it('throws when the assertion fails', function () {
expect(function () {
expect(0, 'to be greater than', 0);
}, 'to throw exception', "expected 0 to be greater than 0");
});
});
describe('greater than or equal assertion', function () {
it('assert >=', function () {
expect(3, 'to be greater than or equal to', 2);
expect(3, 'to be >=', 3);
expect(3, '>=', 3);
});
it('throws when the assertion fails', function () {
expect(function () {
expect(-1, 'to be greater than or equal to', 0);
}, 'to throw exception', "expected -1 to be greater than or equal to 0");
});
});
describe('positive assertion', function () {
it('assert that a number is positive', function () {
expect(3, 'to be positive');
});
it('throws when the assertion fails', function () {
expect(function () {
expect(0, 'to be positive');
}, 'to throw exception', "expected 0 to be positive");
});
});
describe('negative assertion', function () {
it('assert that a number is negative', function () {
expect(-1, 'to be negative');
});
it('throws when the assertion fails', function () {
expect(function () {
expect(0, 'to be negative');
}, 'to throw exception', "expected 0 to be negative");
});
});
describe('to be NaN assertion', function () {
it('assert that the value is NaN or not', function () {
expect(NaN, 'to be NaN');
expect({}, 'to be NaN');
expect(2, 'not to be NaN');
expect(null, 'not to be NaN');
expect(undefined, 'to be NaN');
expect("String", 'to be NaN');
});
it('fails when the assertion fails', function () {
expect(function () {
expect(0, 'to be NaN');
}, 'to throw', "expected 0 to be NaN");
expect(function () {
expect(NaN, 'not to be NaN');
}, 'to throw', "expected NaN not to be NaN");
});
});
describe('fail assertion', function () {
it('throws an error', function () {
expect(function () {
expect.fail();
}, 'to throw exception', "explicit failure");
});
it('throws an error with a given message', function () {
expect(function () {
expect.fail('fail with error message');
}, 'to throw exception', "fail with error message");
});
it('supports placeholders', function () {
expect(function () {
expect.fail('{0} was expected to be {1}', 0, 'zero');
}, 'to throw exception', "0 was expected to be 'zero'");
expect(function () {
expect.fail('{0} was expected to be {1}', 0);
}, 'to throw exception', "0 was expected to be {1}");
});
});
describe('to be an array whose items satisfy assertion', function () {
it('requires a function or a string as the third argument', function () {
expect(function () {
expect([1, 2, 3], 'to be an array whose items satisfy');
}, 'to throw', 'Assertions "to be an array whose items satisfy" expects a functions as argument');
expect(function () {
expect([1, 2, 3], 'to be an array whose items satisfy', 42);
}, 'to throw', 'Assertions "to be an array whose items satisfy" expects a functions as argument');
});
it('only accepts arrays as the target object', function () {
expect(function () {
expect(42, 'to be an array whose items satisfy', function (item) {});
}, 'to throw', /expected 42 to be an array/);
});
it('supports the non-empty clause', function () {
expect([1], 'to be a non-empty array whose items satisfy', function (item) {
expect(item, 'to be a number');
});
});
it('asserts that the given callback does not throw for any items in the array', function () {
expect([0, 1, 2, 3], 'to be an array whose items satisfy', function (item, index) {
expect(item, 'to be a number');
expect(index, 'to be a number');
});
expect(['0', '1', '2', '3'], 'to be an array whose items satisfy', function (item, index) {
expect(item, 'not to be a number');
expect(index, 'to be a number');
});
expect([0, 1, 2, 3], 'to be an array whose items satisfy', 'to be a number');
expect(['0', '1', '2', '3'], 'to be an array whose items satisfy', 'not to be a number');
expect([[1], [2]], 'to be an array whose items satisfy', 'to be an array whose items satisfy', 'to be a number');
});
it('provides the item index to the callback function', function () {
var arr = ['0', '1', '2', '3'];
expect(arr, 'to be an array whose items satisfy', function (item, index) {
expect(index, 'to be a number');
expect(index, 'to be', parseInt(item, 10));
});
});
it('fails when the assertion fails', function () {
expect(function () {
expect(['0', 1, '2', '3'], 'to be an array whose items satisfy', function (item) {
expect(item, 'not to be a number');
});
}, 'to throw', /expected 1 not to be a number/);
expect(function () {
expect(['0', 1, '2', '3'], 'to be an array whose items satisfy', 'not to be a number');
}, 'to throw', /expected 1 not to be a number/);
expect(function () {
expect([], 'to be a non-empty array whose items satisfy', function (item) {
expect(item, 'not to be a number');
});
}, 'to throw', /expected \[\] to be non-empty/);
});
it('provides a detailed report of where failures occur', function () {
expect(function () {
expect([0, 1, '2', 3, 4], 'to be an array whose items satisfy', function (item) {
expect(item, 'to be a number');
expect(item, 'to be less than', 4);
});
}, 'to throw',
"failed expectation in [ 0, 1, '2', 3, 4 ]:\n" +
" 2: expected '2' to be a number\n" +
" 4: expected 4 to be less than 4");
});
it('indents failure reports of nested assertions correctly', function () {
expect(function () {
expect([[0, 1, 2], [4, '5', 6], [7, 8, '9']], 'to be an array whose items satisfy', function (arr) {
expect(arr, 'to be an array whose items satisfy', function (item) {
expect(item, 'to be a number');
});
});
}, 'to throw',
"failed expectation in [ [ 0, 1, 2 ], [ 4, '5', 6 ], [ 7, 8, '9' ] ]:\n" +
" 1: failed expectation in [ 4, '5', 6 ]:\n" +
" 1: expected '5' to be a number\n" +
" 2: failed expectation in [ 7, 8, '9' ]:\n" +
" 2: expected '9' to be a number");
});
});
describe('to be a map whose values satisfy assertion', function () {
it('requires a function or a string as the third argument', function () {
expect(function () {
expect([1, 2, 3], 'to be a map whose values satisfy');
}, 'to throw', 'Assertions "to be a map whose values satisfy" expects a functions as argument');
expect(function () {
expect([1, 2, 3], 'to be a map whose values satisfy', 42);
}, 'to throw', 'Assertions "to be a map whose values satisfy" expects a functions as argument');
});
it('only accepts objects as the target', function () {
expect(function () {
expect(42, 'to be a map whose values satisfy', function (value) {});
}, 'to throw', /expected 42 to be an object/);
});
it('asserts that the given callback does not throw for any values in the map', function () {
expect({ foo: 0, bar: 1, baz: 2, qux: 3 }, 'to be a map whose values satisfy', function (value) {
expect(value, 'to be a number');
});
expect({ foo: '0', bar: '1', baz: '2', qux: '3' }, 'to be a map whose values satisfy', function (value) {
expect(value, 'not to be a number');
});
expect({ foo: 0, bar: 1, baz: 2, qux: 3 }, 'to be a map whose values satisfy', 'to be a number');