UNPKG

17.8 kBJavaScriptView Raw
1
2/**
3 * Module dependencies.
4 */
5
6var should = require('../')
7 , assert = require('assert');
8
9function err(fn, msg) {
10 try {
11 fn();
12 should.fail('expected an error');
13 } catch (err) {
14 should.equal(msg, err.message);
15 }
16}
17
18module.exports = {
19 'test .version': function(){
20 should.version.should.match(/^\d+\.\d+\.\d+$/);
21 },
22
23 'test double require': function(){
24 require('../').should.equal(should);
25 },
26
27 'test assertion': function(){
28 'test'.should.be.a.string;
29 should.equal('foo', 'foo');
30 },
31
32 'test true': function(){
33 true.should.be.true;
34 false.should.not.be.true;
35 (1).should.not.be.true;
36
37 err(function(){
38 'test'.should.be.true;
39 }, "expected 'test' to be true")
40 },
41
42 'test ok': function(){
43 true.should.be.ok;
44 false.should.not.be.ok;
45 (1).should.be.ok;
46 (0).should.not.be.ok;
47
48 err(function(){
49 ''.should.be.ok;
50 }, "expected '' to be truthy");
51
52 err(function(){
53 'test'.should.not.be.ok;
54 }, "expected 'test' to be falsey");
55 },
56
57 'test false': function(){
58 false.should.be.false;
59 true.should.not.be.false;
60 (0).should.not.be.false;
61
62 err(function(){
63 ''.should.be.false;
64 }, "expected '' to be false")
65 },
66
67 'test .expected and .actual': function(){
68 try {
69 'foo'.should.equal('bar');
70 } catch (err) {
71 assert('foo' == err.actual, 'err.actual');
72 assert('bar' == err.expected, 'err.expected');
73 }
74 },
75
76 'test arguments': function(){
77 var args = (function(){ return arguments; })(1,2,3);
78 args.should.be.arguments;
79 [].should.not.be.arguments;
80 },
81
82 'test .equal()': function(){
83 var foo;
84 should.equal(undefined, foo);
85 },
86
87 'test typeof': function(){
88 'test'.should.be.a('string');
89
90 err(function(){
91 'test'.should.not.be.a('string');
92 }, "expected 'test' not to be a string");
93
94 err(function(){
95 'test'.should.not.be.a('string', 'foo');
96 }, "expected 'test' not to be a string | foo");
97
98 (5).should.be.a('number');
99
100 err(function(){
101 (5).should.not.be.a('number');
102 }, "expected 5 not to be a number");
103
104 err(function(){
105 (5).should.not.be.a('number', 'foo');
106 }, "expected 5 not to be a number | foo");
107 },
108
109 'test instanceof': function(){
110 function Foo(){}
111 new Foo().should.be.an.instanceof(Foo);
112
113 err(function(){
114 (3).should.an.instanceof(Foo);
115 }, "expected 3 to be an instance of Foo");
116
117 err(function(){
118 (3).should.an.instanceof(Foo, 'foo');
119 }, "expected 3 to be an instance of Foo | foo");
120 },
121
122 'test instanceOf (non-reserved)': function(){
123 function Foo(){}
124 new Foo().should.be.an.instanceOf(Foo);
125
126 err(function(){
127 (9).should.an.instanceOf(Foo);
128 }, "expected 9 to be an instance of Foo");
129
130 err(function(){
131 (9).should.an.instanceOf(Foo, 'foo');
132 }, "expected 9 to be an instance of Foo | foo");
133 },
134
135 'test within(start, finish)': function(){
136 (5).should.be.within(5, 10);
137 (5).should.be.within(3,6);
138 (5).should.be.within(3,5);
139 (5).should.not.be.within(1,3);
140
141 err(function(){
142 (5).should.not.be.within(4,6);
143 }, "expected 5 to not be within 4..6");
144
145 err(function(){
146 (10).should.be.within(50,100);
147 }, "expected 10 to be within 50..100");
148
149 err(function(){
150 (5).should.not.be.within(4,6, 'foo');
151 }, "expected 5 to not be within 4..6 | foo");
152
153 err(function(){
154 (10).should.be.within(50,100, 'foo');
155 }, "expected 10 to be within 50..100 | foo");
156 },
157
158 'test above(n)': function(){
159 (5).should.be.above(2);
160 (5).should.be.greaterThan(2);
161 (5).should.not.be.above(5);
162 (5).should.not.be.above(6);
163
164 err(function(){
165 (5).should.be.above(6);
166 }, "expected 5 to be above 6");
167
168 err(function(){
169 (10).should.not.be.above(6);
170 }, "expected 10 to be below 6");
171
172 err(function(){
173 (5).should.be.above(6, 'foo');
174 }, "expected 5 to be above 6 | foo");
175
176 err(function(){
177 (10).should.not.be.above(6, 'foo');
178 }, "expected 10 to be below 6 | foo");
179 },
180
181 'test below(n)': function(){
182 (2).should.be.below(5);
183 (2).should.be.lessThan(5);
184 (5).should.not.be.below(5);
185 (6).should.not.be.below(5);
186
187 err(function(){
188 (6).should.be.below(5);
189 }, "expected 6 to be below 5");
190
191 err(function(){
192 (6).should.not.be.below(10);
193 }, "expected 6 to be above 10");
194
195 err(function(){
196 (6).should.be.below(5, 'foo');
197 }, "expected 6 to be below 5 | foo");
198
199 err(function(){
200 (6).should.not.be.below(10, 'foo');
201 }, "expected 6 to be above 10 | foo");
202 },
203
204 'test match(regexp)': function(){
205 'foobar'.should.match(/^foo/)
206 'foobar'.should.not.match(/^bar/)
207
208 err(function(){
209 'foobar'.should.match(/^bar/i)
210 }, "expected 'foobar' to match /^bar/i");
211
212 err(function(){
213 'foobar'.should.not.match(/^foo/i)
214 }, "expected 'foobar' not to match /^foo/i");
215
216 err(function(){
217 'foobar'.should.match(/^bar/i, 'foo')
218 }, "expected 'foobar' to match /^bar/i | foo");
219
220 err(function(){
221 'foobar'.should.not.match(/^foo/i, 'foo')
222 }, "expected 'foobar' not to match /^foo/i | foo");
223 },
224
225 'test length(n)': function(){
226 'test'.should.have.length(4);
227 'test'.should.not.have.length(3);
228 [1,2,3].should.have.length(3);
229
230 err(function(){
231 (4).should.have.length(3);
232 }, 'expected 4 to have a property \'length\'');
233
234 err(function(){
235 'asd'.should.not.have.length(3);
236 }, "expected 'asd' to not have a length of 3");
237
238 err(function(){
239 'asd'.should.have.length(4, 'foo');
240 }, "expected 'asd' to have a length of 4 but got 3 | foo");
241
242 err(function(){
243 'asd'.should.not.have.length(3, 'foo');
244 }, "expected 'asd' to not have a length of 3 | foo");
245
246 },
247
248 'test eql(val)': function(){
249 'test'.should.eql('test');
250 ({ foo: 'bar' }).should.eql({ foo: 'bar' });
251 (1).should.eql(1);
252 '4'.should.not.eql(4);
253
254 err(function(){
255 (4).should.eql(3);
256 }, 'expected 4 to equal 3');
257
258 err(function(){
259 (4).should.eql(3, "foo");
260 }, 'expected 4 to equal 3 | foo');
261
262 err(function(){
263 (3).should.not.eql(3, "foo");
264 }, 'expected 3 to not equal 3 | foo');
265 },
266
267 'test .json': function(){
268 var req = {
269 headers: {
270 'content-type': 'application/json'
271 }
272 };
273
274 req.should.be.json;
275
276 var req = {
277 headers: {
278 'content-type': 'application/json; charset=utf-8'
279 }
280 };
281
282 req.should.be.json;
283 },
284
285 'test equal(val)': function(){
286 'test'.should.equal('test');
287 (1).should.equal(1);
288
289 err(function(){
290 (4).should.equal(3);
291 }, 'expected 4 to equal 3');
292
293 err(function(){
294 '4'.should.equal(4);
295 }, "expected '4' to equal 4");
296
297 err(function(){
298 (3).should.equal(4, "foo");
299 }, "expected 3 to equal 4 | foo");
300
301 err(function(){
302 (4).should.not.equal(4, "foo");
303 }, "expected 4 to not equal 4 | foo");
304
305 var date = new Date;
306 date.should.equal(date);
307 },
308
309 'test empty': function(){
310 ''.should.be.empty;
311 [].should.be.empty;
312 ({ length: 0 }).should.be.empty;
313
314 err(function(){
315 ({}).should.be.empty;
316 }, 'expected {} to have a property \'length\'');
317
318 err(function(){
319 'asd'.should.be.empty;
320 }, "expected 'asd' to be empty");
321
322 err(function(){
323 ''.should.not.be.empty;
324 }, "expected '' not to be empty");
325 },
326
327 'test property(name)': function(){
328 'test'.should.have.property('length');
329 (4).should.not.have.property('length');
330
331 err(function(){
332 'asd'.should.have.property('foo');
333 }, "expected 'asd' to have a property 'foo'");
334
335 err(function(){
336 'asd'.should.have.property('foo', undefined, 'foo');
337 }, "expected 'asd' to have a property 'foo' | foo");
338
339 err(function(){
340 'asd'.should.not.have.property('length', undefined, 'foo');
341 }, "expected 'asd' to not have a property 'length' | foo");
342 },
343
344 'test property(name, val)': function(){
345 'test'.should.have.property('length', 4);
346 'asd'.should.have.property('constructor', String);
347
348 err(function(){
349 'asd'.should.have.property('length', 4);
350 }, "expected 'asd' to have a property 'length' of 4, but got 3");
351
352 err(function(){
353 'asd'.should.not.have.property('length', 3);
354 }, "expected 'asd' to not have a property 'length' of 3");
355
356 err(function(){
357 'asd'.should.not.have.property('foo', 3);
358 }, "'asd' has no property 'foo'");
359
360 err(function(){
361 'asd'.should.have.property('constructor', Number);
362 }, "expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String]");
363
364 err(function(){
365 'asd'.should.have.property('length', 4, 'foo');
366 }, "expected 'asd' to have a property 'length' of 4, but got 3 | foo");
367
368 err(function(){
369 'asd'.should.not.have.property('length', 3, 'foo');
370 }, "expected 'asd' to not have a property 'length' of 3 | foo");
371
372 err(function(){
373 'asd'.should.not.have.property('foo', 3, 'foo');
374 }, "'asd' has no property 'foo' | foo");
375
376 err(function(){
377 'asd'.should.have.property('constructor', Number, 'foo');
378 }, "expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String] | foo");
379 },
380
381 'test ownProperty(name)': function(){
382 'test'.should.have.ownProperty('length');
383 'test'.should.haveOwnProperty('length');
384 ({ length: 12 }).should.have.ownProperty('length');
385
386 err(function(){
387 ({ length: 12 }).should.not.have.ownProperty('length');
388 }, "expected { length: 12 } to not have own property 'length'");
389
390 err(function(){
391 ({ length: 12 }).should.not.have.ownProperty('length', 'foo');
392 }, "expected { length: 12 } to not have own property 'length' | foo");
393
394 err(function(){
395 ({ length: 12 }).should.have.ownProperty('foo', 'foo');
396 }, "expected { length: 12 } to have own property 'foo' | foo");
397 },
398
399 'test include() with string': function(){
400 'foobar'.should.include('bar');
401 'foobar'.should.include('foo');
402 'foobar'.should.not.include('baz');
403
404 err(function(){
405 'foobar'.should.include('baz');
406 }, "expected 'foobar' to include 'baz'");
407
408 err(function(){
409 'foobar'.should.not.include('bar');
410 }, "expected 'foobar' to not include 'bar'");
411
412 err(function(){
413 'foobar'.should.include('baz', 'foo');
414 }, "expected 'foobar' to include 'baz' | foo");
415
416 err(function(){
417 'foobar'.should.not.include('bar', 'foo');
418 }, "expected 'foobar' to not include 'bar' | foo");
419 },
420
421 'test include() with array': function(){
422 ['foo', 'bar'].should.include('foo');
423 ['foo', 'bar'].should.include('foo');
424 ['foo', 'bar'].should.include('bar');
425 [1,2].should.include(1);
426 ['foo', 'bar'].should.not.include('baz');
427 ['foo', 'bar'].should.not.include(1);
428
429 err(function(){
430 ['foo'].should.include('bar');
431 }, "expected [ 'foo' ] to include 'bar'");
432
433 err(function(){
434 ['bar', 'foo'].should.not.include('foo');
435 }, "expected [ 'bar', 'foo' ] to not include 'foo'");
436
437 err(function(){
438 ['foo'].should.include('bar', 'foo');
439 }, "expected [ 'foo' ] to include 'bar' | foo");
440
441 err(function(){
442 ['bar', 'foo'].should.not.include('foo', 'foo');
443 }, "expected [ 'bar', 'foo' ] to not include 'foo' | foo");
444 },
445
446 'test include() with object': function(){
447 var tobi = { name: 'Tobi', age: 2 };
448 var jane = { name: 'Jane', age: 2 };
449
450 var user = { name: 'TJ', pet: tobi, age: 24 };
451
452 user.should.include({ pet: tobi });
453 user.should.include({ pet: tobi, name: 'TJ' });
454 user.should.not.include({ pet: tobi, name: 'Someone else' });
455 user.should.not.include({ pet: jane });
456 user.should.not.include({ pet: jane, name: 'TJ' });
457
458 err(function(){
459 user.should.include({ pet: { name: 'Luna' } });
460 }, "expected { name: 'TJ', pet: { name: 'Tobi', age: 2 }, age: 24 } to include an object equal to { pet: { name: 'Luna' } }");
461 },
462
463 'test includeEql() with array': function(){
464 [['foo'], ['bar']].should.includeEql(['foo']);
465 [['foo'], ['bar']].should.includeEql(['bar']);
466 [['foo'], ['bar']].should.not.includeEql(['baz']);
467 [].should.not.includeEql(['baz']);
468
469 err(function(){
470 [['foo']].should.includeEql(['bar']);
471 }, "expected [ [ 'foo' ] ] to include an object equal to [ 'bar' ]");
472
473 err(function(){
474 [['foo']].should.not.includeEql(['foo']);
475 }, "expected [ [ 'foo' ] ] to not include an object equal to [ 'foo' ]");
476
477 err(function(){
478 [['foo']].should.includeEql(['bar'], 'foo');
479 }, "expected [ [ 'foo' ] ] to include an object equal to [ 'bar' ] | foo");
480
481 err(function(){
482 [['foo']].should.not.includeEql(['foo'], 'foo');
483 }, "expected [ [ 'foo' ] ] to not include an object equal to [ 'foo' ] | foo");
484 },
485
486 'test keys(array)': function(){
487 ({ foo: 1 }).should.have.keys(['foo']);
488 ({ foo: 1, bar: 2 }).should.have.keys(['foo', 'bar']);
489 ({ foo: 1, bar: 2 }).should.have.keys('foo', 'bar');
490
491 err(function(){
492 ({ foo: 1 }).should.have.keys();
493 }, "keys required");
494
495 err(function(){
496 ({ foo: 1 }).should.have.keys([]);
497 }, "keys required");
498
499 err(function(){
500 ({ foo: 1 }).should.not.have.keys([]);
501 }, "keys required");
502
503 err(function(){
504 ({ foo: 1 }).should.have.keys(['bar']);
505 }, "expected { foo: 1 } to have key 'bar'");
506
507 err(function(){
508 ({ foo: 1 }).should.have.keys(['bar', 'baz']);
509 }, "expected { foo: 1 } to have keys 'bar', and 'baz'");
510
511 err(function(){
512 ({ foo: 1 }).should.have.keys(['foo', 'bar', 'baz']);
513 }, "expected { foo: 1 } to have keys 'foo', 'bar', and 'baz'");
514
515 err(function(){
516 ({ foo: 1 }).should.not.have.keys(['foo']);
517 }, "expected { foo: 1 } to not have key 'foo'");
518
519 err(function(){
520 ({ foo: 1 }).should.not.have.keys(['foo']);
521 }, "expected { foo: 1 } to not have key 'foo'");
522
523 err(function(){
524 ({ foo: 1, bar: 2 }).should.not.have.keys(['foo', 'bar']);
525 }, "expected { foo: 1, bar: 2 } to not have keys 'foo', and 'bar'");
526 },
527
528 'test chaining': function(){
529 var user = { name: 'tj', pets: ['tobi', 'loki', 'jane', 'bandit'] };
530 user.should.have.property('pets').with.lengthOf(4);
531
532 err(function(){
533 user.should.have.property('pets').with.lengthOf(5);
534 }, "expected [ 'tobi', 'loki', 'jane', 'bandit' ] to have a length of 5 but got 4");
535
536 user.should.be.a('object').and.have.property('name', 'tj');
537 },
538
539 'test throw()': function(){
540 (function(){}).should.not.throw();
541 (function(){ throw new Error('fail') }).should.throw();
542
543 err(function(){
544 (function(){}).should.throw();
545 }, 'expected an exception to be thrown');
546
547 err(function(){
548 (function(){
549 throw new Error('fail');
550 }).should.not.throw();
551 }, 'expected no exception to be thrown, got "fail"');
552 },
553
554 'test throw() with regex message': function(){
555 (function(){ throw new Error('fail'); }).should.throw(/fail/);
556
557 err(function(){
558 (function(){}).should.throw(/fail/);
559 }, 'expected an exception to be thrown');
560
561 err(function(){
562 (function(){ throw new Error('error'); }).should.throw(/fail/);
563 }, "expected an exception to be thrown with a message matching /fail/, but got 'error'");
564 },
565
566 'test throw() with string message': function(){
567 (function(){ throw new Error('fail'); }).should.throw('fail');
568
569 err(function(){
570 (function(){}).should.throw('fail');
571 }, 'expected an exception to be thrown');
572
573 err(function(){
574 (function(){ throw new Error('error'); }).should.throw('fail');
575 }, "expected an exception to be thrown with a message matching 'fail', but got 'error'");
576 },
577
578 'test throw() with type': function(){
579 (function(){ throw new Error('fail'); }).should.throw(Error);
580
581 err(function(){
582 (function(){}).should.throw(Error);
583 }, 'expected an exception to be thrown');
584
585 err(function(){
586 (function(){ throw 'error'; }).should.throw(Error);
587 }, "expected an exception to be thrown of type Error, but got String");
588 },
589
590 'test throwError()': function(){
591 (function(){}).should.not.throwError();
592 (function(){ throw new Error('fail') }).should.throwError();
593
594 err(function(){
595 (function(){}).should.throwError();
596 }, 'expected an exception to be thrown');
597
598 err(function(){
599 (function(){
600 throw new Error('fail');
601 }).should.not.throwError();
602 }, 'expected no exception to be thrown, got "fail"');
603 },
604
605 'test throwError() with regex message': function(){
606 (function(){ throw new Error('fail'); }).should.throwError(/fail/);
607
608 err(function(){
609 (function(){}).should.throwError(/fail/);
610 }, 'expected an exception to be thrown');
611
612 err(function(){
613 (function(){ throw new Error('error'); }).should.throwError(/fail/);
614 }, "expected an exception to be thrown with a message matching /fail/, but got 'error'");
615 },
616
617 'test throwError() with string message': function(){
618 (function(){ throw new Error('fail'); }).should.throwError('fail');
619
620 err(function(){
621 (function(){}).should.throwError('fail');
622 }, 'expected an exception to be thrown');
623
624 err(function(){
625 (function(){ throw new Error('error'); }).should.throwError('fail');
626 }, "expected an exception to be thrown with a message matching 'fail', but got 'error'");
627 },
628
629 'test throwError() with type': function(){
630 (function(){ throw new Error('fail'); }).should.throw(Error);
631
632 err(function(){
633 (function(){}).should.throw(Error);
634 }, 'expected an exception to be thrown');
635
636 err(function(){
637 (function(){ throw 'error'; }).should.throw(Error);
638 }, "expected an exception to be thrown of type Error, but got String");
639 }
640
641};