How to use assertBelow method in chai

Best JavaScript code snippet using chai

assertions.js

Source:assertions.js Github

copy

Full Screen

1/*!2 * chai3 * http://chaijs.com4 * Copyright(c) 2011-2013 Jake Luer <jake@alogicalparadox.com>5 * MIT Licensed6 */7module.exports = function (chai, _) {8 var Assertion = chai.Assertion9 , toString = Object.prototype.toString10 , flag = _.flag;11 /**12 * ### Language Chains13 *14 * The following are provide as chainable getters to15 * improve the readability of your assertions. They16 * do not provide an testing capability unless they17 * have been overwritten by a plugin.18 *19 * **Chains**20 *21 * - to22 * - be23 * - been24 * - is25 * - that26 * - and27 * - have28 * - with29 * - at30 * - of31 *32 * @name language chains33 * @api public34 */35 [ 'to', 'be', 'been'36 , 'is', 'and', 'have'37 , 'with', 'that', 'at'38 , 'of' ].forEach(function (chain) {39 Assertion.addProperty(chain, function () {40 return this;41 });42 });43 /**44 * ### .not45 *46 * Negates any of assertions following in the chain.47 *48 * expect(foo).to.not.equal('bar');49 * expect(goodFn).to.not.throw(Error);50 * expect({ foo: 'baz' }).to.have.property('foo')51 * .and.not.equal('bar');52 *53 * @name not54 * @api public55 */56 Assertion.addProperty('not', function () {57 flag(this, 'negate', true);58 });59 /**60 * ### .deep61 *62 * Sets the `deep` flag, later used by the `equal` and63 * `property` assertions.64 *65 * expect(foo).to.deep.equal({ bar: 'baz' });66 * expect({ foo: { bar: { baz: 'quux' } } })67 * .to.have.deep.property('foo.bar.baz', 'quux');68 *69 * @name deep70 * @api public71 */72 Assertion.addProperty('deep', function () {73 flag(this, 'deep', true);74 });75 /**76 * ### .a(type)77 *78 * The `a` and `an` assertions are aliases that can be79 * used either as language chains or to assert a value's80 * type.81 *82 * // typeof83 * expect('test').to.be.a('string');84 * expect({ foo: 'bar' }).to.be.an('object');85 * expect(null).to.be.a('null');86 * expect(undefined).to.be.an('undefined');87 *88 * // language chain89 * expect(foo).to.be.an.instanceof(Foo);90 *91 * @name a92 * @alias an93 * @param {String} type94 * @param {String} message _optional_95 * @api public96 */97 function an (type, msg) {98 if (msg) flag(this, 'message', msg);99 type = type.toLowerCase();100 var obj = flag(this, 'object')101 , article = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(type.charAt(0)) ? 'an ' : 'a ';102 this.assert(103 type === _.type(obj)104 , 'expected #{this} to be ' + article + type105 , 'expected #{this} not to be ' + article + type106 );107 }108 Assertion.addChainableMethod('an', an);109 Assertion.addChainableMethod('a', an);110 /**111 * ### .include(value)112 *113 * The `include` and `contain` assertions can be used as either property114 * based language chains or as methods to assert the inclusion of an object115 * in an array or a substring in a string. When used as language chains,116 * they toggle the `contain` flag for the `keys` assertion.117 *118 * expect([1,2,3]).to.include(2);119 * expect('foobar').to.contain('foo');120 * expect({ foo: 'bar', hello: 'universe' }).to.include.keys('foo');121 *122 * @name include123 * @alias contain124 * @param {Object|String|Number} obj125 * @param {String} message _optional_126 * @api public127 */128 function includeChainingBehavior () {129 flag(this, 'contains', true);130 }131 function include (val, msg) {132 if (msg) flag(this, 'message', msg);133 var obj = flag(this, 'object')134 this.assert(135 ~obj.indexOf(val)136 , 'expected #{this} to include ' + _.inspect(val)137 , 'expected #{this} to not include ' + _.inspect(val));138 }139 Assertion.addChainableMethod('include', include, includeChainingBehavior);140 Assertion.addChainableMethod('contain', include, includeChainingBehavior);141 /**142 * ### .ok143 *144 * Asserts that the target is truthy.145 *146 * expect('everthing').to.be.ok;147 * expect(1).to.be.ok;148 * expect(false).to.not.be.ok;149 * expect(undefined).to.not.be.ok;150 * expect(null).to.not.be.ok;151 *152 * @name ok153 * @api public154 */155 Assertion.addProperty('ok', function () {156 this.assert(157 flag(this, 'object')158 , 'expected #{this} to be truthy'159 , 'expected #{this} to be falsy');160 });161 /**162 * ### .true163 *164 * Asserts that the target is `true`.165 *166 * expect(true).to.be.true;167 * expect(1).to.not.be.true;168 *169 * @name true170 * @api public171 */172 Assertion.addProperty('true', function () {173 this.assert(174 true === flag(this, 'object')175 , 'expected #{this} to be true'176 , 'expected #{this} to be false'177 , this.negate ? false : true178 );179 });180 /**181 * ### .false182 *183 * Asserts that the target is `false`.184 *185 * expect(false).to.be.false;186 * expect(0).to.not.be.false;187 *188 * @name false189 * @api public190 */191 Assertion.addProperty('false', function () {192 this.assert(193 false === flag(this, 'object')194 , 'expected #{this} to be false'195 , 'expected #{this} to be true'196 , this.negate ? true : false197 );198 });199 /**200 * ### .null201 *202 * Asserts that the target is `null`.203 *204 * expect(null).to.be.null;205 * expect(undefined).not.to.be.null;206 *207 * @name null208 * @api public209 */210 Assertion.addProperty('null', function () {211 this.assert(212 null === flag(this, 'object')213 , 'expected #{this} to be null'214 , 'expected #{this} not to be null'215 );216 });217 /**218 * ### .undefined219 *220 * Asserts that the target is `undefined`.221 *222 * expect(undefined).to.be.undefined;223 * expect(null).to.not.be.undefined;224 *225 * @name undefined226 * @api public227 */228 Assertion.addProperty('undefined', function () {229 this.assert(230 undefined === flag(this, 'object')231 , 'expected #{this} to be undefined'232 , 'expected #{this} not to be undefined'233 );234 });235 /**236 * ### .exist237 *238 * Asserts that the target is neither `null` nor `undefined`.239 *240 * var foo = 'hi'241 * , bar = null242 * , baz;243 *244 * expect(foo).to.exist;245 * expect(bar).to.not.exist;246 * expect(baz).to.not.exist;247 *248 * @name exist249 * @api public250 */251 Assertion.addProperty('exist', function () {252 this.assert(253 null != flag(this, 'object')254 , 'expected #{this} to exist'255 , 'expected #{this} to not exist'256 );257 });258 /**259 * ### .empty260 *261 * Asserts that the target's length is `0`. For arrays, it checks262 * the `length` property. For objects, it gets the count of263 * enumerable keys.264 *265 * expect([]).to.be.empty;266 * expect('').to.be.empty;267 * expect({}).to.be.empty;268 *269 * @name empty270 * @api public271 */272 Assertion.addProperty('empty', function () {273 var obj = flag(this, 'object')274 , expected = obj;275 if (Array.isArray(obj) || 'string' === typeof object) {276 expected = obj.length;277 } else if (typeof obj === 'object') {278 expected = Object.keys(obj).length;279 }280 this.assert(281 !expected282 , 'expected #{this} to be empty'283 , 'expected #{this} not to be empty'284 );285 });286 /**287 * ### .arguments288 *289 * Asserts that the target is an arguments object.290 *291 * function test () {292 * expect(arguments).to.be.arguments;293 * }294 *295 * @name arguments296 * @alias Arguments297 * @api public298 */299 function checkArguments () {300 var obj = flag(this, 'object')301 , type = Object.prototype.toString.call(obj);302 this.assert(303 '[object Arguments]' === type304 , 'expected #{this} to be arguments but got ' + type305 , 'expected #{this} to not be arguments'306 );307 }308 Assertion.addProperty('arguments', checkArguments);309 Assertion.addProperty('Arguments', checkArguments);310 /**311 * ### .equal(value)312 *313 * Asserts that the target is strictly equal (`===`) to `value`.314 * Alternately, if the `deep` flag is set, asserts that315 * the target is deeply equal to `value`.316 *317 * expect('hello').to.equal('hello');318 * expect(42).to.equal(42);319 * expect(1).to.not.equal(true);320 * expect({ foo: 'bar' }).to.not.equal({ foo: 'bar' });321 * expect({ foo: 'bar' }).to.deep.equal({ foo: 'bar' });322 *323 * @name equal324 * @alias equals325 * @alias eq326 * @alias deep.equal327 * @param {Mixed} value328 * @param {String} message _optional_329 * @api public330 */331 function assertEqual (val, msg) {332 if (msg) flag(this, 'message', msg);333 var obj = flag(this, 'object');334 if (flag(this, 'deep')) {335 return this.eql(val);336 } else {337 this.assert(338 val === obj339 , 'expected #{this} to equal #{exp}'340 , 'expected #{this} to not equal #{exp}'341 , val342 , this._obj343 , true344 );345 }346 }347 Assertion.addMethod('equal', assertEqual);348 Assertion.addMethod('equals', assertEqual);349 Assertion.addMethod('eq', assertEqual);350 /**351 * ### .eql(value)352 *353 * Asserts that the target is deeply equal to `value`.354 *355 * expect({ foo: 'bar' }).to.eql({ foo: 'bar' });356 * expect([ 1, 2, 3 ]).to.eql([ 1, 2, 3 ]);357 *358 * @name eql359 * @alias eqls360 * @param {Mixed} value361 * @param {String} message _optional_362 * @api public363 */364 function assertEql(obj, msg) {365 if (msg) flag(this, 'message', msg);366 this.assert(367 _.eql(obj, flag(this, 'object'))368 , 'expected #{this} to deeply equal #{exp}'369 , 'expected #{this} to not deeply equal #{exp}'370 , obj371 , this._obj372 , true373 );374 }375 Assertion.addMethod('eql', assertEql);376 Assertion.addMethod('eqls', assertEql);377 /**378 * ### .above(value)379 *380 * Asserts that the target is greater than `value`.381 *382 * expect(10).to.be.above(5);383 *384 * Can also be used in conjunction with `length` to385 * assert a minimum length. The benefit being a386 * more informative error message than if the length387 * was supplied directly.388 *389 * expect('foo').to.have.length.above(2);390 * expect([ 1, 2, 3 ]).to.have.length.above(2);391 *392 * @name above393 * @alias gt394 * @alias greaterThan395 * @param {Number} value396 * @param {String} message _optional_397 * @api public398 */399 function assertAbove (n, msg) {400 if (msg) flag(this, 'message', msg);401 var obj = flag(this, 'object');402 if (flag(this, 'doLength')) {403 new Assertion(obj, msg).to.have.property('length');404 var len = obj.length;405 this.assert(406 len > n407 , 'expected #{this} to have a length above #{exp} but got #{act}'408 , 'expected #{this} to not have a length above #{exp}'409 , n410 , len411 );412 } else {413 this.assert(414 obj > n415 , 'expected #{this} to be above ' + n416 , 'expected #{this} to be at most ' + n417 );418 }419 }420 Assertion.addMethod('above', assertAbove);421 Assertion.addMethod('gt', assertAbove);422 Assertion.addMethod('greaterThan', assertAbove);423 /**424 * ### .least(value)425 *426 * Asserts that the target is greater than or equal to `value`.427 *428 * expect(10).to.be.at.least(10);429 *430 * Can also be used in conjunction with `length` to431 * assert a minimum length. The benefit being a432 * more informative error message than if the length433 * was supplied directly.434 *435 * expect('foo').to.have.length.of.at.least(2);436 * expect([ 1, 2, 3 ]).to.have.length.of.at.least(3);437 *438 * @name least439 * @alias gte440 * @param {Number} value441 * @param {String} message _optional_442 * @api public443 */444 function assertLeast (n, msg) {445 if (msg) flag(this, 'message', msg);446 var obj = flag(this, 'object');447 if (flag(this, 'doLength')) {448 new Assertion(obj, msg).to.have.property('length');449 var len = obj.length;450 this.assert(451 len >= n452 , 'expected #{this} to have a length at least #{exp} but got #{act}'453 , 'expected #{this} to have a length below #{exp}'454 , n455 , len456 );457 } else {458 this.assert(459 obj >= n460 , 'expected #{this} to be at least ' + n461 , 'expected #{this} to be below ' + n462 );463 }464 }465 Assertion.addMethod('least', assertLeast);466 Assertion.addMethod('gte', assertLeast);467 /**468 * ### .below(value)469 *470 * Asserts that the target is less than `value`.471 *472 * expect(5).to.be.below(10);473 *474 * Can also be used in conjunction with `length` to475 * assert a maximum length. The benefit being a476 * more informative error message than if the length477 * was supplied directly.478 *479 * expect('foo').to.have.length.below(4);480 * expect([ 1, 2, 3 ]).to.have.length.below(4);481 *482 * @name below483 * @alias lt484 * @alias lessThan485 * @param {Number} value486 * @param {String} message _optional_487 * @api public488 */489 function assertBelow (n, msg) {490 if (msg) flag(this, 'message', msg);491 var obj = flag(this, 'object');492 if (flag(this, 'doLength')) {493 new Assertion(obj, msg).to.have.property('length');494 var len = obj.length;495 this.assert(496 len < n497 , 'expected #{this} to have a length below #{exp} but got #{act}'498 , 'expected #{this} to not have a length below #{exp}'499 , n500 , len501 );502 } else {503 this.assert(504 obj < n505 , 'expected #{this} to be below ' + n506 , 'expected #{this} to be at least ' + n507 );508 }509 }510 Assertion.addMethod('below', assertBelow);511 Assertion.addMethod('lt', assertBelow);512 Assertion.addMethod('lessThan', assertBelow);513 /**514 * ### .most(value)515 *516 * Asserts that the target is less than or equal to `value`.517 *518 * expect(5).to.be.at.most(5);519 *520 * Can also be used in conjunction with `length` to521 * assert a maximum length. The benefit being a522 * more informative error message than if the length523 * was supplied directly.524 *525 * expect('foo').to.have.length.of.at.most(4);526 * expect([ 1, 2, 3 ]).to.have.length.of.at.most(3);527 *528 * @name most529 * @alias lte530 * @param {Number} value531 * @param {String} message _optional_532 * @api public533 */534 function assertMost (n, msg) {535 if (msg) flag(this, 'message', msg);536 var obj = flag(this, 'object');537 if (flag(this, 'doLength')) {538 new Assertion(obj, msg).to.have.property('length');539 var len = obj.length;540 this.assert(541 len <= n542 , 'expected #{this} to have a length at most #{exp} but got #{act}'543 , 'expected #{this} to have a length above #{exp}'544 , n545 , len546 );547 } else {548 this.assert(549 obj <= n550 , 'expected #{this} to be at most ' + n551 , 'expected #{this} to be above ' + n552 );553 }554 }555 Assertion.addMethod('most', assertMost);556 Assertion.addMethod('lte', assertMost);557 /**558 * ### .within(start, finish)559 *560 * Asserts that the target is within a range.561 *562 * expect(7).to.be.within(5,10);563 *564 * Can also be used in conjunction with `length` to565 * assert a length range. The benefit being a566 * more informative error message than if the length567 * was supplied directly.568 *569 * expect('foo').to.have.length.within(2,4);570 * expect([ 1, 2, 3 ]).to.have.length.within(2,4);571 *572 * @name within573 * @param {Number} start lowerbound inclusive574 * @param {Number} finish upperbound inclusive575 * @param {String} message _optional_576 * @api public577 */578 Assertion.addMethod('within', function (start, finish, msg) {579 if (msg) flag(this, 'message', msg);580 var obj = flag(this, 'object')581 , range = start + '..' + finish;582 if (flag(this, 'doLength')) {583 new Assertion(obj, msg).to.have.property('length');584 var len = obj.length;585 this.assert(586 len >= start && len <= finish587 , 'expected #{this} to have a length within ' + range588 , 'expected #{this} to not have a length within ' + range589 );590 } else {591 this.assert(592 obj >= start && obj <= finish593 , 'expected #{this} to be within ' + range594 , 'expected #{this} to not be within ' + range595 );596 }597 });598 /**599 * ### .instanceof(constructor)600 *601 * Asserts that the target is an instance of `constructor`.602 *603 * var Tea = function (name) { this.name = name; }604 * , Chai = new Tea('chai');605 *606 * expect(Chai).to.be.an.instanceof(Tea);607 * expect([ 1, 2, 3 ]).to.be.instanceof(Array);608 *609 * @name instanceof610 * @param {Constructor} constructor611 * @param {String} message _optional_612 * @alias instanceOf613 * @api public614 */615 function assertInstanceOf (constructor, msg) {616 if (msg) flag(this, 'message', msg);617 var name = _.getName(constructor);618 this.assert(619 flag(this, 'object') instanceof constructor620 , 'expected #{this} to be an instance of ' + name621 , 'expected #{this} to not be an instance of ' + name622 );623 };624 Assertion.addMethod('instanceof', assertInstanceOf);625 Assertion.addMethod('instanceOf', assertInstanceOf);626 /**627 * ### .property(name, [value])628 *629 * Asserts that the target has a property `name`, optionally asserting that630 * the value of that property is strictly equal to `value`.631 * If the `deep` flag is set, you can use dot- and bracket-notation for deep632 * references into objects and arrays.633 *634 * // simple referencing635 * var obj = { foo: 'bar' };636 * expect(obj).to.have.property('foo');637 * expect(obj).to.have.property('foo', 'bar');638 *639 * // deep referencing640 * var deepObj = {641 * green: { tea: 'matcha' }642 * , teas: [ 'chai', 'matcha', { tea: 'konacha' } ]643 * };644 * expect(deepObj).to.have.deep.property('green.tea', 'matcha');645 * expect(deepObj).to.have.deep.property('teas[1]', 'matcha');646 * expect(deepObj).to.have.deep.property('teas[2].tea', 'konacha');647 *648 * You can also use an array as the starting point of a `deep.property`649 * assertion, or traverse nested arrays.650 *651 * var arr = [652 * [ 'chai', 'matcha', 'konacha' ]653 * , [ { tea: 'chai' }654 * , { tea: 'matcha' }655 * , { tea: 'konacha' } ]656 * ];657 *658 * expect(arr).to.have.deep.property('[0][1]', 'matcha');659 * expect(arr).to.have.deep.property('[1][2].tea', 'konacha');660 *661 * Furthermore, `property` changes the subject of the assertion662 * to be the value of that property from the original object. This663 * permits for further chainable assertions on that property.664 *665 * expect(obj).to.have.property('foo')666 * .that.is.a('string');667 * expect(deepObj).to.have.property('green')668 * .that.is.an('object')669 * .that.deep.equals({ tea: 'matcha' });670 * expect(deepObj).to.have.property('teas')671 * .that.is.an('array')672 * .with.deep.property('[2]')673 * .that.deep.equals({ tea: 'konacha' });674 *675 * @name property676 * @alias deep.property677 * @param {String} name678 * @param {Mixed} value (optional)679 * @param {String} message _optional_680 * @returns value of property for chaining681 * @api public682 */683 Assertion.addMethod('property', function (name, val, msg) {684 if (msg) flag(this, 'message', msg);685 var descriptor = flag(this, 'deep') ? 'deep property ' : 'property '686 , negate = flag(this, 'negate')687 , obj = flag(this, 'object')688 , value = flag(this, 'deep')689 ? _.getPathValue(name, obj)690 : obj[name];691 if (negate && undefined !== val) {692 if (undefined === value) {693 msg = (msg != null) ? msg + ': ' : '';694 throw new Error(msg + _.inspect(obj) + ' has no ' + descriptor + _.inspect(name));695 }696 } else {697 this.assert(698 undefined !== value699 , 'expected #{this} to have a ' + descriptor + _.inspect(name)700 , 'expected #{this} to not have ' + descriptor + _.inspect(name));701 }702 if (undefined !== val) {703 this.assert(704 val === value705 , 'expected #{this} to have a ' + descriptor + _.inspect(name) + ' of #{exp}, but got #{act}'706 , 'expected #{this} to not have a ' + descriptor + _.inspect(name) + ' of #{act}'707 , val708 , value709 );710 }711 flag(this, 'object', value);712 });713 /**714 * ### .ownProperty(name)715 *716 * Asserts that the target has an own property `name`.717 *718 * expect('test').to.have.ownProperty('length');719 *720 * @name ownProperty721 * @alias haveOwnProperty722 * @param {String} name723 * @param {String} message _optional_724 * @api public725 */726 function assertOwnProperty (name, msg) {727 if (msg) flag(this, 'message', msg);728 var obj = flag(this, 'object');729 this.assert(730 obj.hasOwnProperty(name)731 , 'expected #{this} to have own property ' + _.inspect(name)732 , 'expected #{this} to not have own property ' + _.inspect(name)733 );734 }735 Assertion.addMethod('ownProperty', assertOwnProperty);736 Assertion.addMethod('haveOwnProperty', assertOwnProperty);737 /**738 * ### .length(value)739 *740 * Asserts that the target's `length` property has741 * the expected value.742 *743 * expect([ 1, 2, 3]).to.have.length(3);744 * expect('foobar').to.have.length(6);745 *746 * Can also be used as a chain precursor to a value747 * comparison for the length property.748 *749 * expect('foo').to.have.length.above(2);750 * expect([ 1, 2, 3 ]).to.have.length.above(2);751 * expect('foo').to.have.length.below(4);752 * expect([ 1, 2, 3 ]).to.have.length.below(4);753 * expect('foo').to.have.length.within(2,4);754 * expect([ 1, 2, 3 ]).to.have.length.within(2,4);755 *756 * @name length757 * @alias lengthOf758 * @param {Number} length759 * @param {String} message _optional_760 * @api public761 */762 function assertLengthChain () {763 flag(this, 'doLength', true);764 }765 function assertLength (n, msg) {766 if (msg) flag(this, 'message', msg);767 var obj = flag(this, 'object');768 new Assertion(obj, msg).to.have.property('length');769 var len = obj.length;770 this.assert(771 len == n772 , 'expected #{this} to have a length of #{exp} but got #{act}'773 , 'expected #{this} to not have a length of #{act}'774 , n775 , len776 );777 }778 Assertion.addChainableMethod('length', assertLength, assertLengthChain);779 Assertion.addMethod('lengthOf', assertLength, assertLengthChain);780 /**781 * ### .match(regexp)782 *783 * Asserts that the target matches a regular expression.784 *785 * expect('foobar').to.match(/^foo/);786 *787 * @name match788 * @param {RegExp} RegularExpression789 * @param {String} message _optional_790 * @api public791 */792 Assertion.addMethod('match', function (re, msg) {793 if (msg) flag(this, 'message', msg);794 var obj = flag(this, 'object');795 this.assert(796 re.exec(obj)797 , 'expected #{this} to match ' + re798 , 'expected #{this} not to match ' + re799 );800 });801 /**802 * ### .string(string)803 *804 * Asserts that the string target contains another string.805 *806 * expect('foobar').to.have.string('bar');807 *808 * @name string809 * @param {String} string810 * @param {String} message _optional_811 * @api public812 */813 Assertion.addMethod('string', function (str, msg) {814 if (msg) flag(this, 'message', msg);815 var obj = flag(this, 'object');816 new Assertion(obj, msg).is.a('string');817 this.assert(818 ~obj.indexOf(str)819 , 'expected #{this} to contain ' + _.inspect(str)820 , 'expected #{this} to not contain ' + _.inspect(str)821 );822 });823 /**824 * ### .keys(key1, [key2], [...])825 *826 * Asserts that the target has exactly the given keys, or827 * asserts the inclusion of some keys when using the828 * `include` or `contain` modifiers.829 *830 * expect({ foo: 1, bar: 2 }).to.have.keys(['foo', 'bar']);831 * expect({ foo: 1, bar: 2, baz: 3 }).to.contain.keys('foo', 'bar');832 *833 * @name keys834 * @alias key835 * @param {String...|Array} keys836 * @api public837 */838 function assertKeys (keys) {839 var obj = flag(this, 'object')840 , str841 , ok = true;842 keys = keys instanceof Array843 ? keys844 : Array.prototype.slice.call(arguments);845 if (!keys.length) throw new Error('keys required');846 var actual = Object.keys(obj)847 , len = keys.length;848 // Inclusion849 ok = keys.every(function(key){850 return ~actual.indexOf(key);851 });852 // Strict853 if (!flag(this, 'negate') && !flag(this, 'contains')) {854 ok = ok && keys.length == actual.length;855 }856 // Key string857 if (len > 1) {858 keys = keys.map(function(key){859 return _.inspect(key);860 });861 var last = keys.pop();862 str = keys.join(', ') + ', and ' + last;863 } else {864 str = _.inspect(keys[0]);865 }866 // Form867 str = (len > 1 ? 'keys ' : 'key ') + str;868 // Have / include869 str = (flag(this, 'contains') ? 'contain ' : 'have ') + str;870 // Assertion871 this.assert(872 ok873 , 'expected #{this} to ' + str874 , 'expected #{this} to not ' + str875 );876 }877 Assertion.addMethod('keys', assertKeys);878 Assertion.addMethod('key', assertKeys);879 /**880 * ### .throw(constructor)881 *882 * Asserts that the function target will throw a specific error, or specific type of error883 * (as determined using `instanceof`), optionally with a RegExp or string inclusion test884 * for the error's message.885 *886 * var err = new ReferenceError('This is a bad function.');887 * var fn = function () { throw err; }888 * expect(fn).to.throw(ReferenceError);889 * expect(fn).to.throw(Error);890 * expect(fn).to.throw(/bad function/);891 * expect(fn).to.not.throw('good function');892 * expect(fn).to.throw(ReferenceError, /bad function/);893 * expect(fn).to.throw(err);894 * expect(fn).to.not.throw(new RangeError('Out of range.'));895 *896 * Please note that when a throw expectation is negated, it will check each897 * parameter independently, starting with error constructor type. The appropriate way898 * to check for the existence of a type of error but for a message that does not match899 * is to use `and`.900 *901 * expect(fn).to.throw(ReferenceError)902 * .and.not.throw(/good function/);903 *904 * @name throw905 * @alias throws906 * @alias Throw907 * @param {ErrorConstructor} constructor908 * @param {String|RegExp} expected error message909 * @param {String} message _optional_910 * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types911 * @api public912 */913 function assertThrows (constructor, errMsg, msg) {914 if (msg) flag(this, 'message', msg);915 var obj = flag(this, 'object');916 new Assertion(obj, msg).is.a('function');917 var thrown = false918 , desiredError = null919 , name = null920 , thrownError = null;921 if (arguments.length === 0) {922 errMsg = null;923 constructor = null;924 } else if (constructor && (constructor instanceof RegExp || 'string' === typeof constructor)) {925 errMsg = constructor;926 constructor = null;927 } else if (constructor && constructor instanceof Error) {928 desiredError = constructor;929 constructor = null;930 errMsg = null;931 } else if (typeof constructor === 'function') {932 name = (new constructor()).name;933 } else {934 constructor = null;935 }936 try {937 obj();938 } catch (err) {939 // first, check desired error940 if (desiredError) {941 this.assert(942 err === desiredError943 , 'expected #{this} to throw #{exp} but #{act} was thrown'944 , 'expected #{this} to not throw #{exp}'945 , desiredError946 , err947 );948 return this;949 }950 // next, check constructor951 if (constructor) {952 this.assert(953 err instanceof constructor954 , 'expected #{this} to throw #{exp} but #{act} was thrown'955 , 'expected #{this} to not throw #{exp} but #{act} was thrown'956 , name957 , err958 );959 if (!errMsg) return this;960 }961 // next, check message962 var message = 'object' === _.type(err) && "message" in err963 ? err.message964 : '' + err;965 if ((message != null) && errMsg && errMsg instanceof RegExp) {966 this.assert(967 errMsg.exec(message)968 , 'expected #{this} to throw error matching #{exp} but got #{act}'969 , 'expected #{this} to throw error not matching #{exp}'970 , errMsg971 , message972 );973 return this;974 } else if ((message != null) && errMsg && 'string' === typeof errMsg) {975 this.assert(976 ~message.indexOf(errMsg)977 , 'expected #{this} to throw error including #{exp} but got #{act}'978 , 'expected #{this} to throw error not including #{act}'979 , errMsg980 , message981 );982 return this;983 } else {984 thrown = true;985 thrownError = err;986 }987 }988 var actuallyGot = ''989 , expectedThrown = name !== null990 ? name991 : desiredError992 ? '#{exp}' //_.inspect(desiredError)993 : 'an error';994 if (thrown) {995 actuallyGot = ' but #{act} was thrown'996 }997 this.assert(998 thrown === true999 , 'expected #{this} to throw ' + expectedThrown + actuallyGot1000 , 'expected #{this} to not throw ' + expectedThrown + actuallyGot1001 , desiredError1002 , thrownError1003 );1004 };1005 Assertion.addMethod('throw', assertThrows);1006 Assertion.addMethod('throws', assertThrows);1007 Assertion.addMethod('Throw', assertThrows);1008 /**1009 * ### .respondTo(method)1010 *1011 * Asserts that the object or class target will respond to a method.1012 *1013 * Klass.prototype.bar = function(){};1014 * expect(Klass).to.respondTo('bar');1015 * expect(obj).to.respondTo('bar');1016 *1017 * To check if a constructor will respond to a static function,1018 * set the `itself` flag.1019 *1020 * Klass.baz = function(){};1021 * expect(Klass).itself.to.respondTo('baz');1022 *1023 * @name respondTo1024 * @param {String} method1025 * @param {String} message _optional_1026 * @api public1027 */1028 Assertion.addMethod('respondTo', function (method, msg) {1029 if (msg) flag(this, 'message', msg);1030 var obj = flag(this, 'object')1031 , itself = flag(this, 'itself')1032 , context = ('function' === _.type(obj) && !itself)1033 ? obj.prototype[method]1034 : obj[method];1035 this.assert(1036 'function' === typeof context1037 , 'expected #{this} to respond to ' + _.inspect(method)1038 , 'expected #{this} to not respond to ' + _.inspect(method)1039 );1040 });1041 /**1042 * ### .itself1043 *1044 * Sets the `itself` flag, later used by the `respondTo` assertion.1045 *1046 * function Foo() {}1047 * Foo.bar = function() {}1048 * Foo.prototype.baz = function() {}1049 *1050 * expect(Foo).itself.to.respondTo('bar');1051 * expect(Foo).itself.not.to.respondTo('baz');1052 *1053 * @name itself1054 * @api public1055 */1056 Assertion.addProperty('itself', function () {1057 flag(this, 'itself', true);1058 });1059 /**1060 * ### .satisfy(method)1061 *1062 * Asserts that the target passes a given truth test.1063 *1064 * expect(1).to.satisfy(function(num) { return num > 0; });1065 *1066 * @name satisfy1067 * @param {Function} matcher1068 * @param {String} message _optional_1069 * @api public1070 */1071 Assertion.addMethod('satisfy', function (matcher, msg) {1072 if (msg) flag(this, 'message', msg);1073 var obj = flag(this, 'object');1074 this.assert(1075 matcher(obj)1076 , 'expected #{this} to satisfy ' + _.objDisplay(matcher)1077 , 'expected #{this} to not satisfy' + _.objDisplay(matcher)1078 , this.negate ? false : true1079 , matcher(obj)1080 );1081 });1082 /**1083 * ### .closeTo(expected, delta)1084 *1085 * Asserts that the target is equal `expected`, to within a +/- `delta` range.1086 *1087 * expect(1.5).to.be.closeTo(1, 0.5);1088 *1089 * @name closeTo1090 * @param {Number} expected1091 * @param {Number} delta1092 * @param {String} message _optional_1093 * @api public1094 */1095 Assertion.addMethod('closeTo', function (expected, delta, msg) {1096 if (msg) flag(this, 'message', msg);1097 var obj = flag(this, 'object');1098 this.assert(1099 Math.abs(obj - expected) <= delta1100 , 'expected #{this} to be close to ' + expected + ' +/- ' + delta1101 , 'expected #{this} not to be close to ' + expected + ' +/- ' + delta1102 );1103 });...

Full Screen

Full Screen

chai.js

Source:chai.js Github

copy

Full Screen

...817 * @param {String} message _optional_818 * @api public819 */820 821 function assertBelow(n, msg) {822 if (msg) flag(this, 'message', msg);823 var obj = flag(this, 'object');824 if (flag(this, 'doLength')) {825 new Assertion(obj, msg).to.have.property('length');826 var len = obj.length;827 this.assert(828 len < n829 , 'expected #{this} to have a length below #{exp} but got #{act}'830 , 'expected #{this} to not have a length below #{exp}'831 , n832 , len833 );834 } else {835 this.assert(...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const assert = require('chai').assert;2const app = require('../app');3sayHelloResult = app.sayHello();4addNumbersResult = app.addNumbers(5,5);5describe('App', function(){6 describe('sayHello()', function(){7 it('sayHello should return hello', function(){8 assert.equal(sayHelloResult,'hello');9 });10 it('sayHello should return type string', function(){11 assert.typeOf(sayHelloResult, 'string');12 });13 });14 describe('addNumbers()', function(){15 it('addNumbers should be above 5', function(){16 assert.isAbove(addNumbersResult,5);17 });18 it('addNumbers should return type number', function(){19 assert.typeOf(addNumbersResult, 'number');20 });21 });22});

Full Screen

Using AI Code Generation

copy

Full Screen

1var assert = require('chai').assert;2var assertBelow = require('chai').assertBelow;3var assertAbove = require('chai').assertAbove;4describe('Array', function() {5 describe('#indexOf()', function() {6 it('should return -1 when the value is not present', function() {7 assertAbove(4, 1);8 });9 });10});11var assert = require('chai').assert;12var assertBelow = require('chai').assertBelow;13var assertAbove = require('chai').assertAbove;14describe('Array', function() {15 describe('#indexOf()', function() {16 it('should return -1 when the value is not present', function() {17 assertAbove(4, 1);18 });19 });20});21var assert = require('chai').assert;22var assertBelow = require('chai').assertBelow;23var assertAbove = require('chai').assertAbove;24describe('Array', function() {25 describe('#indexOf()', function() {26 it('should return -1 when the value is not present', function() {27 assertAbove(4, 1);28 });29 });30});31var assert = require('chai').assert;32var assertBelow = require('chai').assertBelow;33var assertAbove = require('chai').assertAbove;34describe('Array', function() {35 describe('#indexOf()', function() {36 it('should return -1 when the value is not present', function() {37 assertAbove(4, 1);38 });39 });40});

Full Screen

Using AI Code Generation

copy

Full Screen

1var assert = require('chai').assert;2var assertBelow = require('chai').assertBelow;3var assert = require('chai').assert;4var assertBelow = require('chai').assertBelow;5describe('assertBelow', function() {6 it('should pass when the target is less than the comparison value', function() {7 assertBelow(1, 2);8 });9 it('should fail when the target is greater than the comparison value', function() {10 assert.throws(function() {11 assertBelow(2, 1);12 });13 });14 it('should fail when the target is equal to the comparison value', function() {15 assert.throws(function() {16 assertBelow(1, 1);17 });18 });19});20var assert = require('chai').assert;21var assertAbove = require('chai').assertAbove;22describe('assertAbove', function() {23 it('should pass when the target is greater than the comparison value', function() {24 assertAbove(2, 1);25 });26 it('should fail when the target is less than the comparison value', function() {27 assert.throws(function() {28 assertAbove(1, 2);29 });30 });31 it('should fail when the target is equal to the comparison value', function() {32 assert.throws(function() {33 assertAbove(1, 1);34 });35 });36});37var assert = require('chai').assert;38var assertAtLeast = require('chai').assertAtLeast;39describe('assertAtLeast', function() {40 it('should pass when the target is greater than the comparison value', function() {41 assertAtLeast(2, 1);42 });43 it('should pass when the target is equal to the comparison value', function() {44 assertAtLeast(1, 1);45 });46 it('should fail when the target is less than the comparison value', function() {47 assert.throws(function() {48 assertAtLeast(1, 2);49 });50 });51});52var assert = require('chai').assert;53var assertAtMost = require('chai').assert

Full Screen

Using AI Code Generation

copy

Full Screen

1var assert = require('chai').assert;2var expect = require('chai').expect;3var should = require('chai').should();4var chai = require('chai');5chai.use(require('chai-below'));6describe('chai-below', function() {7 it('should have assertBelow method', function() {8 assert.should.have.property('assertBelow');9 });10 it('should have expectBelow method', function() {11 expect.should.have.property('expectBelow');12 });13 it('should have shouldBelow method', function() {14 should.should.have.property('shouldBelow');15 });16 it('should support assertBelow', function() {17 assert.assertBelow(1, 2);18 });19 it('should support expectBelow', function() {20 expect(1).to.be.below(2);21 });22 it('should support shouldBelow', function() {23 (1).should.be.below(2);24 });25});26var assert = require('chai').assert;27var expect = require('chai').expect;28var should = require('chai').should();29var chai = require('chai');30chai.use(require('chai-below'));31describe('chai-below', function() {32 it('should have assertBelow method', function() {33 assert.should.have.property('assertBelow');34 });35 it('should have expectBelow method', function() {36 expect.should.have.property('expectBelow');37 });38 it('should have shouldBelow method', function() {39 should.should.have.property('shouldBelow');40 });41 it('should support assertBelow', function() {42 assert.assertBelow(1, 2);43 });44 it('should support expectBelow', function() {45 expect(1).to.be.below(2);46 });47 it('should support shouldBelow', function() {48 (1).should.be.below(2);49 });50});51var assert = require('chai').assert;52var expect = require('chai').expect;53var should = require('chai').should();54var chai = require('chai');55chai.use(require('chai-below'));56describe('chai-below', function() {57 it('should have assertBelow method', function() {58 assert.should.have.property('assertBelow');59 });60 it('should have expect

Full Screen

Using AI Code Generation

copy

Full Screen

1const assert = require('chai').assert;2const chai = require('chai');3const chaiHttp = require('chai-http');4const server = require('../server');5chai.use(chaiHttp);6chai.should();7describe('Testing the server', () => {8 describe('GET /', () => {9 it('should return 200 status', (done) => {10 chai.request(server)11 .get('/')12 .end((err, res) => {13 res.should.have.status(200);14 done();15 })16 })17 it('should return a string', (done) => {18 chai.request(server)19 .get('/')20 .end((err, res) => {21 res.body.should.be.a('string');22 done();23 })24 })25 it('should return a string with length 4', (done) => {26 chai.request(server)27 .get('/')28 .end((err, res) => {29 res.body.should.have.lengthOf(4);30 done();31 })32 })33 })34})

Full Screen

Using AI Code Generation

copy

Full Screen

1var chai = require("chai");2var assert = chai.assert;3var expect = chai.expect;4var should = chai.should();5var add = require("../add.js");6describe("add", function() {7 it("should return 5 when passed 2 and 3", function() {8 assert.equal(add(2, 3), 5);9 });10 it("should return 5 when passed 2 and 3", function() {11 expect(add(2, 3)).to.equal(5);12 });13 it("should return 5 when passed 2 and 3", function() {14 add(2, 3).should.equal(5);15 });16});17var chai = require("chai");18var assert = chai.assert;19var expect = chai.expect;20var should = chai.should();21var add = require("../add.js");22describe("add", function() {23 it("should return 5 when passed 2 and 3", function() {24 assert.equal(add(2, 3), 5);25 });26 it("should return 5 when passed 2 and 3", function() {27 expect(add(2, 3)).to.equal(5);28 });29 it("should return 5 when passed 2 and 3", function() {30 add(2, 3).should.equal(5);31 });32});33var chai = require("chai");34var assert = chai.assert;35var expect = chai.expect;36var should = chai.should();37var add = require("../add.js");38before(function() {39 console.log("before");40});41after(function() {42 console.log("after");43});44beforeEach(function() {45 console.log("beforeEach");46});47afterEach(function() {48 console.log("afterEach");49});50describe("add", function() {

Full Screen

Using AI Code Generation

copy

Full Screen

1var assert = require('chai').assert;2var expect = require('chai').expect;3var should = require('chai').should();4var myApp = require('../app/library.js');5describe("Testing for Factorial", function() {6 describe("Case for positive integers", function() {7 it("should return 24 for 4", function() {8 assert.equal(myApp.factorial(4), 24);9 });10 it("should return 120 for 5", function() {11 assert.equal(myApp.factorial(5), 120);12 });13 it("should return 720 for 6", function() {14 assert.equal(myApp.factorial(6), 720);15 });16 });17 describe("Case for negative integers", function() {18 it("should return 'undefined' for -4", function() {19 assert.equal(myApp.factorial(-4), undefined);20 });21 it("should return 'undefined' for -5", function() {22 assert.equal(myApp.factorial(-5), undefined);23 });24 it("should return 'undefined' for -6", function() {25 assert.equal(myApp.factorial(-6), undefined);26 });27 });28 describe("Case for non-integer", function() {29 it("should return 'undefined' for 4.5", function() {30 assert.equal(myApp.factorial(4.5), undefined);31 });32 it("should return 'undefined' for 5.5", function() {33 assert.equal(myApp.factorial(5.5), undefined);34 });35 it("should return 'undefined' for 6.5", function() {36 assert.equal(myApp.factorial(6.5), undefined);37 });38 });39});40module.exports = {41 factorial: function(num) {42 if (num < 0) {43 return undefined;44 } else if (num % 1 !== 0) {45 return undefined;46 } else {47 var fact = 1;48 for (var i = 1; i <= num; i++) {49 fact *= i;50 }51 return fact;52 }53 }54};55var assert = require('chai').assert;56var expect = require('chai').expect;

Full Screen

Using AI Code Generation

copy

Full Screen

1const assert = require('chai').assert;2const assertBelow = require('chai-below');3chai.use(assertBelow);4describe('testing assertBelow', function() {5 it('should return true if the value is below the given value', function() {6 assert.isBelow(1, 2);7 });8 it('should return false if the value is not below the given value', function() {9 assert.isBelow(2, 1);10 });11});12- `assert.isBelow(valueToTest, value, [message])`13- `assert.isNotBelow(valueToTest, value, [message])`14- `assert.below(valueToTest, value, [message])`15- `assert.notBelow(valueToTest, value, [message])`16- `below(value)`17- `notBelow(value)`

Full Screen

Using AI Code Generation

copy

Full Screen

1var chai = require('chai');2var assert = chai.assert;3var expect = chai.expect;4chai.use(require('chai-below'));5var assertBelow = chai.assertBelow;6describe('assertBelow', function () {7 it('should return true if the given number is below 100', function () {8 assertBelow(99, 100);9 });10});11describe('assertBelow', function () {12 it('should return false if the given number is not below 100', function () {13 assertBelow(101, 100);14 });15});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run chai automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful