UNPKG

chai

Version:

BDD/TDD assertion library for node.js and the browser. Test framework agnostic.

1,824 lines (1,735 loc) 90 kB
/*! * chai * Copyright(c) 2011-2014 Jake Luer <jake@alogicalparadox.com> * MIT Licensed */ import * as chai from '../../../index.js'; import {Assertion} from '../assertion.js'; import {flag, inspect} from '../utils/index.js'; import {AssertionError} from 'assertion-error'; /** * ### assert(expression, message) * * Write your own test expressions. * * assert('foo' !== 'bar', 'foo is not bar'); * assert(Array.isArray([]), 'empty arrays are arrays'); * * @param {unknown} express - expression to test for truthiness * @param {string} errmsg - message to display on error * @name assert * @namespace Assert * @public */ export function assert(express, errmsg) { let test = new Assertion(null, null, chai.assert, true); test.assert(express, errmsg, '[ negation message unavailable ]'); } /** * ### .fail([message]) * ### .fail(actual, expected, [message], [operator]) * * Throw a failure. Node.js `assert` module-compatible. * * assert.fail(); * assert.fail("custom error message"); * assert.fail(1, 2); * assert.fail(1, 2, "custom error message"); * assert.fail(1, 2, "custom error message", ">"); * assert.fail(1, 2, undefined, ">"); * * @name fail * @param {unknown} actual * @param {unknown} expected * @param {string} message * @param {string} operator * @namespace Assert * @public */ assert.fail = function (actual, expected, message, operator) { if (arguments.length < 2) { // Comply with Node's fail([message]) interface message = actual; actual = undefined; } message = message || 'assert.fail()'; throw new AssertionError( message, { actual: actual, expected: expected, operator: operator }, assert.fail ); }; /** * ### .isOk(object, [message]) * * Asserts that `object` is truthy. * * assert.isOk('everything', 'everything is ok'); * assert.isOk(false, 'this will fail'); * * @name isOk * @alias ok * @param {unknown} val object to test * @param {string} msg * @namespace Assert * @public */ assert.isOk = function (val, msg) { new Assertion(val, msg, assert.isOk, true).is.ok; }; /** * ### .isNotOk(object, [message]) * * Asserts that `object` is falsy. * * assert.isNotOk('everything', 'this will fail'); * assert.isNotOk(false, 'this will pass'); * * @name isNotOk * @alias notOk * @param {unknown} val object to test * @param {string} msg * @namespace Assert * @public */ assert.isNotOk = function (val, msg) { new Assertion(val, msg, assert.isNotOk, true).is.not.ok; }; /** * ### .equal(actual, expected, [message]) * * Asserts non-strict equality (`==`) of `actual` and `expected`. * * assert.equal(3, '3', '== coerces values to strings'); * * @name equal * @param {unknown} act * @param {unknown} exp * @param {string} msg * @namespace Assert * @public */ assert.equal = function (act, exp, msg) { let test = new Assertion(act, msg, assert.equal, true); test.assert( exp == flag(test, 'object'), 'expected #{this} to equal #{exp}', 'expected #{this} to not equal #{act}', exp, act, true ); }; /** * ### .notEqual(actual, expected, [message]) * * Asserts non-strict inequality (`!=`) of `actual` and `expected`. * * assert.notEqual(3, 4, 'these numbers are not equal'); * * @name notEqual * @param {unknown} act * @param {unknown} exp * @param {string} msg * @namespace Assert * @public */ assert.notEqual = function (act, exp, msg) { let test = new Assertion(act, msg, assert.notEqual, true); test.assert( exp != flag(test, 'object'), 'expected #{this} to not equal #{exp}', 'expected #{this} to equal #{act}', exp, act, true ); }; /** * ### .strictEqual(actual, expected, [message]) * * Asserts strict equality (`===`) of `actual` and `expected`. * * assert.strictEqual(true, true, 'these booleans are strictly equal'); * * @name strictEqual * @param {unknown} act * @param {unknown} exp * @param {string} msg * @namespace Assert * @public */ assert.strictEqual = function (act, exp, msg) { new Assertion(act, msg, assert.strictEqual, true).to.equal(exp); }; /** * ### .notStrictEqual(actual, expected, [message]) * * Asserts strict inequality (`!==`) of `actual` and `expected`. * * assert.notStrictEqual(3, '3', 'no coercion for strict equality'); * * @name notStrictEqual * @param {unknown} act * @param {unknown} exp * @param {string} msg * @namespace Assert * @public */ assert.notStrictEqual = function (act, exp, msg) { new Assertion(act, msg, assert.notStrictEqual, true).to.not.equal(exp); }; /** * ### .deepEqual(actual, expected, [message]) * * Asserts that `actual` is deeply equal to `expected`. * * assert.deepEqual({ tea: 'green' }, { tea: 'green' }); * * @name deepEqual * @param {unknown} act * @param {unknown} exp * @param {string} msg * @alias deepStrictEqual * @namespace Assert * @public */ assert.deepEqual = assert.deepStrictEqual = function (act, exp, msg) { new Assertion(act, msg, assert.deepEqual, true).to.eql(exp); }; /** * ### .notDeepEqual(actual, expected, [message]) * * Assert that `actual` is not deeply equal to `expected`. * * assert.notDeepEqual({ tea: 'green' }, { tea: 'jasmine' }); * * @name notDeepEqual * @param {unknown} act * @param {unknown} exp * @param {string} msg * @namespace Assert * @public */ assert.notDeepEqual = function (act, exp, msg) { new Assertion(act, msg, assert.notDeepEqual, true).to.not.eql(exp); }; /** * ### .isAbove(valueToCheck, valueToBeAbove, [message]) * * Asserts `valueToCheck` is strictly greater than (>) `valueToBeAbove`. * * assert.isAbove(5, 2, '5 is strictly greater than 2'); * * @name isAbove * @param {unknown} val * @param {unknown} abv * @param {string} msg * @namespace Assert * @public */ assert.isAbove = function (val, abv, msg) { new Assertion(val, msg, assert.isAbove, true).to.be.above(abv); }; /** * ### .isAtLeast(valueToCheck, valueToBeAtLeast, [message]) * * Asserts `valueToCheck` is greater than or equal to (>=) `valueToBeAtLeast`. * * assert.isAtLeast(5, 2, '5 is greater or equal to 2'); * assert.isAtLeast(3, 3, '3 is greater or equal to 3'); * * @name isAtLeast * @param {unknown} val * @param {unknown} atlst * @param {string} msg * @namespace Assert * @public */ assert.isAtLeast = function (val, atlst, msg) { new Assertion(val, msg, assert.isAtLeast, true).to.be.least(atlst); }; /** * ### .isBelow(valueToCheck, valueToBeBelow, [message]) * * Asserts `valueToCheck` is strictly less than (<) `valueToBeBelow`. * * assert.isBelow(3, 6, '3 is strictly less than 6'); * * @name isBelow * @param {unknown} val * @param {unknown} blw * @param {string} msg * @namespace Assert * @public */ assert.isBelow = function (val, blw, msg) { new Assertion(val, msg, assert.isBelow, true).to.be.below(blw); }; /** * ### .isAtMost(valueToCheck, valueToBeAtMost, [message]) * * Asserts `valueToCheck` is less than or equal to (<=) `valueToBeAtMost`. * * assert.isAtMost(3, 6, '3 is less than or equal to 6'); * assert.isAtMost(4, 4, '4 is less than or equal to 4'); * * @name isAtMost * @param {unknown} val * @param {unknown} atmst * @param {string} msg * @namespace Assert * @public */ assert.isAtMost = function (val, atmst, msg) { new Assertion(val, msg, assert.isAtMost, true).to.be.most(atmst); }; /** * ### .isTrue(value, [message]) * * Asserts that `value` is true. * * var teaServed = true; * assert.isTrue(teaServed, 'the tea has been served'); * * @name isTrue * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isTrue = function (val, msg) { new Assertion(val, msg, assert.isTrue, true).is['true']; }; /** * ### .isNotTrue(value, [message]) * * Asserts that `value` is not true. * * var tea = 'tasty chai'; * assert.isNotTrue(tea, 'great, time for tea!'); * * @name isNotTrue * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNotTrue = function (val, msg) { new Assertion(val, msg, assert.isNotTrue, true).to.not.equal(true); }; /** * ### .isFalse(value, [message]) * * Asserts that `value` is false. * * var teaServed = false; * assert.isFalse(teaServed, 'no tea yet? hmm...'); * * @name isFalse * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isFalse = function (val, msg) { new Assertion(val, msg, assert.isFalse, true).is['false']; }; /** * ### .isNotFalse(value, [message]) * * Asserts that `value` is not false. * * var tea = 'tasty chai'; * assert.isNotFalse(tea, 'great, time for tea!'); * * @name isNotFalse * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNotFalse = function (val, msg) { new Assertion(val, msg, assert.isNotFalse, true).to.not.equal(false); }; /** * ### .isNull(value, [message]) * * Asserts that `value` is null. * * assert.isNull(err, 'there was no error'); * * @name isNull * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNull = function (val, msg) { new Assertion(val, msg, assert.isNull, true).to.equal(null); }; /** * ### .isNotNull(value, [message]) * * Asserts that `value` is not null. * * var tea = 'tasty chai'; * assert.isNotNull(tea, 'great, time for tea!'); * * @name isNotNull * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNotNull = function (val, msg) { new Assertion(val, msg, assert.isNotNull, true).to.not.equal(null); }; /** * ### .isNaN * * Asserts that value is NaN. * * assert.isNaN(NaN, 'NaN is NaN'); * * @name isNaN * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNaN = function (val, msg) { new Assertion(val, msg, assert.isNaN, true).to.be.NaN; }; /** * ### .isNotNaN * * Asserts that value is not NaN. * * assert.isNotNaN(4, '4 is not NaN'); * * @name isNotNaN * @param {unknown} value * @param {string} message * @namespace Assert * @public */ assert.isNotNaN = function (value, message) { new Assertion(value, message, assert.isNotNaN, true).not.to.be.NaN; }; /** * ### .exists * * Asserts that the target is neither `null` nor `undefined`. * * var foo = 'hi'; * assert.exists(foo, 'foo is neither `null` nor `undefined`'); * * @name exists * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.exists = function (val, msg) { new Assertion(val, msg, assert.exists, true).to.exist; }; /** * ### .notExists * * Asserts that the target is either `null` or `undefined`. * * var bar = null * , baz; * * assert.notExists(bar); * assert.notExists(baz, 'baz is either null or undefined'); * * @name notExists * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.notExists = function (val, msg) { new Assertion(val, msg, assert.notExists, true).to.not.exist; }; /** * ### .isUndefined(value, [message]) * * Asserts that `value` is `undefined`. * * var tea; * assert.isUndefined(tea, 'no tea defined'); * * @name isUndefined * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isUndefined = function (val, msg) { new Assertion(val, msg, assert.isUndefined, true).to.equal(undefined); }; /** * ### .isDefined(value, [message]) * * Asserts that `value` is not `undefined`. * * var tea = 'cup of chai'; * assert.isDefined(tea, 'tea has been defined'); * * @name isDefined * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isDefined = function (val, msg) { new Assertion(val, msg, assert.isDefined, true).to.not.equal(undefined); }; /** * ### .isCallable(value, [message]) * * Asserts that `value` is a callable function. * * function serveTea() { return 'cup of tea'; }; * assert.isCallable(serveTea, 'great, we can have tea now'); * * @name isCallable * @param {unknown} value * @param {string} message * @namespace Assert * @public */ assert.isCallable = function (value, message) { new Assertion(value, message, assert.isCallable, true).is.callable; }; /** * ### .isNotCallable(value, [message]) * * Asserts that `value` is _not_ a callable function. * * var serveTea = [ 'heat', 'pour', 'sip' ]; * assert.isNotCallable(serveTea, 'great, we have listed the steps'); * * @name isNotCallable * @param {unknown} value * @param {string} message * @namespace Assert * @public */ assert.isNotCallable = function (value, message) { new Assertion(value, message, assert.isNotCallable, true).is.not.callable; }; /** * ### .isObject(value, [message]) * * Asserts that `value` is an object of type 'Object' (as revealed by `Object.prototype.toString`). * _The assertion does not match subclassed objects._ * * var selection = { name: 'Chai', serve: 'with spices' }; * assert.isObject(selection, 'tea selection is an object'); * * @name isObject * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isObject = function (val, msg) { new Assertion(val, msg, assert.isObject, true).to.be.a('object'); }; /** * ### .isNotObject(value, [message]) * * Asserts that `value` is _not_ an object of type 'Object' (as revealed by `Object.prototype.toString`). * * var selection = 'chai' * assert.isNotObject(selection, 'tea selection is not an object'); * assert.isNotObject(null, 'null is not an object'); * * @name isNotObject * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNotObject = function (val, msg) { new Assertion(val, msg, assert.isNotObject, true).to.not.be.a('object'); }; /** * ### .isArray(value, [message]) * * Asserts that `value` is an array. * * var menu = [ 'green', 'chai', 'oolong' ]; * assert.isArray(menu, 'what kind of tea do we want?'); * * @name isArray * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isArray = function (val, msg) { new Assertion(val, msg, assert.isArray, true).to.be.an('array'); }; /** * ### .isNotArray(value, [message]) * * Asserts that `value` is _not_ an array. * * var menu = 'green|chai|oolong'; * assert.isNotArray(menu, 'what kind of tea do we want?'); * * @name isNotArray * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNotArray = function (val, msg) { new Assertion(val, msg, assert.isNotArray, true).to.not.be.an('array'); }; /** * ### .isString(value, [message]) * * Asserts that `value` is a string. * * var teaOrder = 'chai'; * assert.isString(teaOrder, 'order placed'); * * @name isString * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isString = function (val, msg) { new Assertion(val, msg, assert.isString, true).to.be.a('string'); }; /** * ### .isNotString(value, [message]) * * Asserts that `value` is _not_ a string. * * var teaOrder = 4; * assert.isNotString(teaOrder, 'order placed'); * * @name isNotString * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNotString = function (val, msg) { new Assertion(val, msg, assert.isNotString, true).to.not.be.a('string'); }; /** * ### .isNumber(value, [message]) * * Asserts that `value` is a number. * * var cups = 2; * assert.isNumber(cups, 'how many cups'); * * @name isNumber * @param {number} val * @param {string} msg * @namespace Assert * @public */ assert.isNumber = function (val, msg) { new Assertion(val, msg, assert.isNumber, true).to.be.a('number'); }; /** * ### .isNotNumber(value, [message]) * * Asserts that `value` is _not_ a number. * * var cups = '2 cups please'; * assert.isNotNumber(cups, 'how many cups'); * * @name isNotNumber * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNotNumber = function (val, msg) { new Assertion(val, msg, assert.isNotNumber, true).to.not.be.a('number'); }; /** * ### .isNumeric(value, [message]) * * Asserts that `value` is a number or BigInt. * * var cups = 2; * assert.isNumeric(cups, 'how many cups'); * * var cups = 10n; * assert.isNumeric(cups, 'how many cups'); * * @name isNumeric * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNumeric = function (val, msg) { new Assertion(val, msg, assert.isNumeric, true).is.numeric; }; /** * ### .isNotNumeric(value, [message]) * * Asserts that `value` is _not_ a number or BigInt. * * var cups = '2 cups please'; * assert.isNotNumeric(cups, 'how many cups'); * * @name isNotNumeric * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNotNumeric = function (val, msg) { new Assertion(val, msg, assert.isNotNumeric, true).is.not.numeric; }; /** * ### .isFinite(value, [message]) * * Asserts that `value` is a finite number. Unlike `.isNumber`, this will fail for `NaN` and `Infinity`. * * var cups = 2; * assert.isFinite(cups, 'how many cups'); * assert.isFinite(NaN); // throws * * @name isFinite * @param {number} val * @param {string} msg * @namespace Assert * @public */ assert.isFinite = function (val, msg) { new Assertion(val, msg, assert.isFinite, true).to.be.finite; }; /** * ### .isBoolean(value, [message]) * * Asserts that `value` is a boolean. * * var teaReady = true * , teaServed = false; * * assert.isBoolean(teaReady, 'is the tea ready'); * assert.isBoolean(teaServed, 'has tea been served'); * * @name isBoolean * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isBoolean = function (val, msg) { new Assertion(val, msg, assert.isBoolean, true).to.be.a('boolean'); }; /** * ### .isNotBoolean(value, [message]) * * Asserts that `value` is _not_ a boolean. * * var teaReady = 'yep' * , teaServed = 'nope'; * * assert.isNotBoolean(teaReady, 'is the tea ready'); * assert.isNotBoolean(teaServed, 'has tea been served'); * * @name isNotBoolean * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.isNotBoolean = function (val, msg) { new Assertion(val, msg, assert.isNotBoolean, true).to.not.be.a('boolean'); }; /** * ### .typeOf(value, name, [message]) * * Asserts that `value`'s type is `name`, as determined by * `Object.prototype.toString`. * * assert.typeOf({ tea: 'chai' }, 'object', 'we have an object'); * assert.typeOf(['chai', 'jasmine'], 'array', 'we have an array'); * assert.typeOf('tea', 'string', 'we have a string'); * assert.typeOf(/tea/, 'regexp', 'we have a regular expression'); * assert.typeOf(null, 'null', 'we have a null'); * assert.typeOf(undefined, 'undefined', 'we have an undefined'); * * @name typeOf * @param {unknown} val * @param {string} type * @param {string} msg * @namespace Assert * @public */ assert.typeOf = function (val, type, msg) { new Assertion(val, msg, assert.typeOf, true).to.be.a(type); }; /** * ### .notTypeOf(value, name, [message]) * * Asserts that `value`'s type is _not_ `name`, as determined by * `Object.prototype.toString`. * * assert.notTypeOf('tea', 'number', 'strings are not numbers'); * * @name notTypeOf * @param {unknown} value * @param {string} type * @param {string} message * @namespace Assert * @public */ assert.notTypeOf = function (value, type, message) { new Assertion(value, message, assert.notTypeOf, true).to.not.be.a(type); }; /** * ### .instanceOf(object, constructor, [message]) * * Asserts that `value` is an instance of `constructor`. * * var Tea = function (name) { this.name = name; } * , chai = new Tea('chai'); * * assert.instanceOf(chai, Tea, 'chai is an instance of tea'); * * @name instanceOf * @param {object} val * @param {object} type * @param {string} msg * @namespace Assert * @public */ assert.instanceOf = function (val, type, msg) { new Assertion(val, msg, assert.instanceOf, true).to.be.instanceOf(type); }; /** * ### .notInstanceOf(object, constructor, [message]) * * Asserts `value` is not an instance of `constructor`. * * var Tea = function (name) { this.name = name; } * , chai = new String('chai'); * * assert.notInstanceOf(chai, Tea, 'chai is not an instance of tea'); * * @name notInstanceOf * @param {object} val * @param {object} type * @param {string} msg * @namespace Assert * @public */ assert.notInstanceOf = function (val, type, msg) { new Assertion(val, msg, assert.notInstanceOf, true).to.not.be.instanceOf( type ); }; /** * ### .include(haystack, needle, [message]) * * Asserts that `haystack` includes `needle`. Can be used to assert the * inclusion of a value in an array, a substring in a string, or a subset of * properties in an object. * * assert.include([1,2,3], 2, 'array contains value'); * assert.include('foobar', 'foo', 'string contains substring'); * assert.include({ foo: 'bar', hello: 'universe' }, { foo: 'bar' }, 'object contains property'); * * Strict equality (===) is used. When asserting the inclusion of a value in * an array, the array is searched for an element that's strictly equal to the * given value. When asserting a subset of properties in an object, the object * is searched for the given property keys, checking that each one is present * and strictly equal to the given property value. For instance: * * var obj1 = {a: 1} * , obj2 = {b: 2}; * assert.include([obj1, obj2], obj1); * assert.include({foo: obj1, bar: obj2}, {foo: obj1}); * assert.include({foo: obj1, bar: obj2}, {foo: obj1, bar: obj2}); * * @name include * @param {Array | string} exp * @param {unknown} inc * @param {string} msg * @namespace Assert * @public */ assert.include = function (exp, inc, msg) { new Assertion(exp, msg, assert.include, true).include(inc); }; /** * ### .notInclude(haystack, needle, [message]) * * Asserts that `haystack` does not include `needle`. Can be used to assert * the absence of a value in an array, a substring in a string, or a subset of * properties in an object. * * assert.notInclude([1,2,3], 4, "array doesn't contain value"); * assert.notInclude('foobar', 'baz', "string doesn't contain substring"); * assert.notInclude({ foo: 'bar', hello: 'universe' }, { foo: 'baz' }, 'object doesn't contain property'); * * Strict equality (===) is used. When asserting the absence of a value in an * array, the array is searched to confirm the absence of an element that's * strictly equal to the given value. When asserting a subset of properties in * an object, the object is searched to confirm that at least one of the given * property keys is either not present or not strictly equal to the given * property value. For instance: * * var obj1 = {a: 1} * , obj2 = {b: 2}; * assert.notInclude([obj1, obj2], {a: 1}); * assert.notInclude({foo: obj1, bar: obj2}, {foo: {a: 1}}); * assert.notInclude({foo: obj1, bar: obj2}, {foo: obj1, bar: {b: 2}}); * * @name notInclude * @param {Array | string} exp * @param {unknown} inc * @param {string} msg * @namespace Assert * @public */ assert.notInclude = function (exp, inc, msg) { new Assertion(exp, msg, assert.notInclude, true).not.include(inc); }; /** * ### .deepInclude(haystack, needle, [message]) * * Asserts that `haystack` includes `needle`. Can be used to assert the * inclusion of a value in an array or a subset of properties in an object. * Deep equality is used. * * var obj1 = {a: 1} * , obj2 = {b: 2}; * assert.deepInclude([obj1, obj2], {a: 1}); * assert.deepInclude({foo: obj1, bar: obj2}, {foo: {a: 1}}); * assert.deepInclude({foo: obj1, bar: obj2}, {foo: {a: 1}, bar: {b: 2}}); * * @name deepInclude * @param {Array | string} exp * @param {unknown} inc * @param {string} msg * @namespace Assert * @public */ assert.deepInclude = function (exp, inc, msg) { new Assertion(exp, msg, assert.deepInclude, true).deep.include(inc); }; /** * ### .notDeepInclude(haystack, needle, [message]) * * Asserts that `haystack` does not include `needle`. Can be used to assert * the absence of a value in an array or a subset of properties in an object. * Deep equality is used. * * var obj1 = {a: 1} * , obj2 = {b: 2}; * assert.notDeepInclude([obj1, obj2], {a: 9}); * assert.notDeepInclude({foo: obj1, bar: obj2}, {foo: {a: 9}}); * assert.notDeepInclude({foo: obj1, bar: obj2}, {foo: {a: 1}, bar: {b: 9}}); * * @name notDeepInclude * @param {Array | string} exp * @param {unknown} inc * @param {string} msg * @namespace Assert * @public */ assert.notDeepInclude = function (exp, inc, msg) { new Assertion(exp, msg, assert.notDeepInclude, true).not.deep.include(inc); }; /** * ### .nestedInclude(haystack, needle, [message]) * * Asserts that 'haystack' includes 'needle'. * Can be used to assert the inclusion of a subset of properties in an * object. * Enables the use of dot- and bracket-notation for referencing nested * properties. * '[]' and '.' in property names can be escaped using double backslashes. * * assert.nestedInclude({'.a': {'b': 'x'}}, {'\\.a.[b]': 'x'}); * assert.nestedInclude({'a': {'[b]': 'x'}}, {'a.\\[b\\]': 'x'}); * * @name nestedInclude * @param {object} exp * @param {object} inc * @param {string} msg * @namespace Assert * @public */ assert.nestedInclude = function (exp, inc, msg) { new Assertion(exp, msg, assert.nestedInclude, true).nested.include(inc); }; /** * ### .notNestedInclude(haystack, needle, [message]) * * Asserts that 'haystack' does not include 'needle'. * Can be used to assert the absence of a subset of properties in an * object. * Enables the use of dot- and bracket-notation for referencing nested * properties. * '[]' and '.' in property names can be escaped using double backslashes. * * assert.notNestedInclude({'.a': {'b': 'x'}}, {'\\.a.b': 'y'}); * assert.notNestedInclude({'a': {'[b]': 'x'}}, {'a.\\[b\\]': 'y'}); * * @name notNestedInclude * @param {object} exp * @param {object} inc * @param {string} msg * @namespace Assert * @public */ assert.notNestedInclude = function (exp, inc, msg) { new Assertion(exp, msg, assert.notNestedInclude, true).not.nested.include( inc ); }; /** * ### .deepNestedInclude(haystack, needle, [message]) * * Asserts that 'haystack' includes 'needle'. * Can be used to assert the inclusion of a subset of properties in an * object while checking for deep equality. * Enables the use of dot- and bracket-notation for referencing nested * properties. * '[]' and '.' in property names can be escaped using double backslashes. * * assert.deepNestedInclude({a: {b: [{x: 1}]}}, {'a.b[0]': {x: 1}}); * assert.deepNestedInclude({'.a': {'[b]': {x: 1}}}, {'\\.a.\\[b\\]': {x: 1}}); * * @name deepNestedInclude * @param {object} exp * @param {object} inc * @param {string} msg * @namespace Assert * @public */ assert.deepNestedInclude = function (exp, inc, msg) { new Assertion(exp, msg, assert.deepNestedInclude, true).deep.nested.include( inc ); }; /** * ### .notDeepNestedInclude(haystack, needle, [message]) * * Asserts that 'haystack' does not include 'needle'. * Can be used to assert the absence of a subset of properties in an * object while checking for deep equality. * Enables the use of dot- and bracket-notation for referencing nested * properties. * '[]' and '.' in property names can be escaped using double backslashes. * * assert.notDeepNestedInclude({a: {b: [{x: 1}]}}, {'a.b[0]': {y: 1}}) * assert.notDeepNestedInclude({'.a': {'[b]': {x: 1}}}, {'\\.a.\\[b\\]': {y: 2}}); * * @name notDeepNestedInclude * @param {object} exp * @param {object} inc * @param {string} msg * @namespace Assert * @public */ assert.notDeepNestedInclude = function (exp, inc, msg) { new Assertion( exp, msg, assert.notDeepNestedInclude, true ).not.deep.nested.include(inc); }; /** * ### .ownInclude(haystack, needle, [message]) * * Asserts that 'haystack' includes 'needle'. * Can be used to assert the inclusion of a subset of properties in an * object while ignoring inherited properties. * * assert.ownInclude({ a: 1 }, { a: 1 }); * * @name ownInclude * @param {object} exp * @param {object} inc * @param {string} msg * @namespace Assert * @public */ assert.ownInclude = function (exp, inc, msg) { new Assertion(exp, msg, assert.ownInclude, true).own.include(inc); }; /** * ### .notOwnInclude(haystack, needle, [message]) * * Asserts that 'haystack' does not include 'needle'. * Can be used to assert the absence of a subset of properties in an * object while ignoring inherited properties. * * Object.prototype.b = 2; * assert.notOwnInclude({ a: 1 }, { b: 2 }); * * @name notOwnInclude * @param {object} exp * @param {object} inc * @param {string} msg * @namespace Assert * @public */ assert.notOwnInclude = function (exp, inc, msg) { new Assertion(exp, msg, assert.notOwnInclude, true).not.own.include(inc); }; /** * ### .deepOwnInclude(haystack, needle, [message]) * * Asserts that 'haystack' includes 'needle'. * Can be used to assert the inclusion of a subset of properties in an * object while ignoring inherited properties and checking for deep equality. * * assert.deepOwnInclude({a: {b: 2}}, {a: {b: 2}}); * * @name deepOwnInclude * @param {object} exp * @param {object} inc * @param {string} msg * @namespace Assert * @public */ assert.deepOwnInclude = function (exp, inc, msg) { new Assertion(exp, msg, assert.deepOwnInclude, true).deep.own.include(inc); }; /** * ### .notDeepOwnInclude(haystack, needle, [message]) * * Asserts that 'haystack' includes 'needle'. * Can be used to assert the absence of a subset of properties in an * object while ignoring inherited properties and checking for deep equality. * * assert.notDeepOwnInclude({a: {b: 2}}, {a: {c: 3}}); * * @name notDeepOwnInclude * @param {object} exp * @param {object} inc * @param {string} msg * @namespace Assert * @public */ assert.notDeepOwnInclude = function (exp, inc, msg) { new Assertion(exp, msg, assert.notDeepOwnInclude, true).not.deep.own.include( inc ); }; /** * ### .match(value, regexp, [message]) * * Asserts that `value` matches the regular expression `regexp`. * * assert.match('foobar', /^foo/, 'regexp matches'); * * @name match * @param {unknown} exp * @param {RegExp} re * @param {string} msg * @namespace Assert * @public */ assert.match = function (exp, re, msg) { new Assertion(exp, msg, assert.match, true).to.match(re); }; /** * ### .notMatch(value, regexp, [message]) * * Asserts that `value` does not match the regular expression `regexp`. * * assert.notMatch('foobar', /^foo/, 'regexp does not match'); * * @name notMatch * @param {unknown} exp * @param {RegExp} re * @param {string} msg * @namespace Assert * @public */ assert.notMatch = function (exp, re, msg) { new Assertion(exp, msg, assert.notMatch, true).to.not.match(re); }; /** * ### .property(object, property, [message]) * * Asserts that `object` has a direct or inherited property named by * `property`. * * assert.property({ tea: { green: 'matcha' }}, 'tea'); * assert.property({ tea: { green: 'matcha' }}, 'toString'); * * @name property * @param {object} obj * @param {string} prop * @param {string} msg * @namespace Assert * @public */ assert.property = function (obj, prop, msg) { new Assertion(obj, msg, assert.property, true).to.have.property(prop); }; /** * ### .notProperty(object, property, [message]) * * Asserts that `object` does _not_ have a direct or inherited property named * by `property`. * * assert.notProperty({ tea: { green: 'matcha' }}, 'coffee'); * * @name notProperty * @param {object} obj * @param {string} prop * @param {string} msg * @namespace Assert * @public */ assert.notProperty = function (obj, prop, msg) { new Assertion(obj, msg, assert.notProperty, true).to.not.have.property(prop); }; /** * ### .propertyVal(object, property, value, [message]) * * Asserts that `object` has a direct or inherited property named by * `property` with a value given by `value`. Uses a strict equality check * (===). * * assert.propertyVal({ tea: 'is good' }, 'tea', 'is good'); * * @name propertyVal * @param {object} obj * @param {string} prop * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.propertyVal = function (obj, prop, val, msg) { new Assertion(obj, msg, assert.propertyVal, true).to.have.property(prop, val); }; /** * ### .notPropertyVal(object, property, value, [message]) * * Asserts that `object` does _not_ have a direct or inherited property named * by `property` with value given by `value`. Uses a strict equality check * (===). * * assert.notPropertyVal({ tea: 'is good' }, 'tea', 'is bad'); * assert.notPropertyVal({ tea: 'is good' }, 'coffee', 'is good'); * * @name notPropertyVal * @param {object} obj * @param {string} prop * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.notPropertyVal = function (obj, prop, val, msg) { new Assertion(obj, msg, assert.notPropertyVal, true).to.not.have.property( prop, val ); }; /** * ### .deepPropertyVal(object, property, value, [message]) * * Asserts that `object` has a direct or inherited property named by * `property` with a value given by `value`. Uses a deep equality check. * * assert.deepPropertyVal({ tea: { green: 'matcha' } }, 'tea', { green: 'matcha' }); * * @name deepPropertyVal * @param {object} obj * @param {string} prop * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.deepPropertyVal = function (obj, prop, val, msg) { new Assertion(obj, msg, assert.deepPropertyVal, true).to.have.deep.property( prop, val ); }; /** * ### .notDeepPropertyVal(object, property, value, [message]) * * Asserts that `object` does _not_ have a direct or inherited property named * by `property` with value given by `value`. Uses a deep equality check. * * assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'tea', { black: 'matcha' }); * assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'tea', { green: 'oolong' }); * assert.notDeepPropertyVal({ tea: { green: 'matcha' } }, 'coffee', { green: 'matcha' }); * * @name notDeepPropertyVal * @param {object} obj * @param {string} prop * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.notDeepPropertyVal = function (obj, prop, val, msg) { new Assertion( obj, msg, assert.notDeepPropertyVal, true ).to.not.have.deep.property(prop, val); }; /** * ### .ownProperty(object, property, [message]) * * Asserts that `object` has a direct property named by `property`. Inherited * properties aren't checked. * * assert.ownProperty({ tea: { green: 'matcha' }}, 'tea'); * * @name ownProperty * @param {object} obj * @param {string} prop * @param {string} msg * @public */ assert.ownProperty = function (obj, prop, msg) { new Assertion(obj, msg, assert.ownProperty, true).to.have.own.property(prop); }; /** * ### .notOwnProperty(object, property, [message]) * * Asserts that `object` does _not_ have a direct property named by * `property`. Inherited properties aren't checked. * * assert.notOwnProperty({ tea: { green: 'matcha' }}, 'coffee'); * assert.notOwnProperty({}, 'toString'); * * @name notOwnProperty * @param {object} obj * @param {string} prop * @param {string} msg * @public */ assert.notOwnProperty = function (obj, prop, msg) { new Assertion(obj, msg, assert.notOwnProperty, true).to.not.have.own.property( prop ); }; /** * ### .ownPropertyVal(object, property, value, [message]) * * Asserts that `object` has a direct property named by `property` and a value * equal to the provided `value`. Uses a strict equality check (===). * Inherited properties aren't checked. * * assert.ownPropertyVal({ coffee: 'is good'}, 'coffee', 'is good'); * * @name ownPropertyVal * @param {object} obj * @param {string} prop * @param {unknown} value * @param {string} msg * @public */ assert.ownPropertyVal = function (obj, prop, value, msg) { new Assertion(obj, msg, assert.ownPropertyVal, true).to.have.own.property( prop, value ); }; /** * ### .notOwnPropertyVal(object, property, value, [message]) * * Asserts that `object` does _not_ have a direct property named by `property` * with a value equal to the provided `value`. Uses a strict equality check * (===). Inherited properties aren't checked. * * assert.notOwnPropertyVal({ tea: 'is better'}, 'tea', 'is worse'); * assert.notOwnPropertyVal({}, 'toString', Object.prototype.toString); * * @name notOwnPropertyVal * @param {object} obj * @param {string} prop * @param {unknown} value * @param {string} msg * @public */ assert.notOwnPropertyVal = function (obj, prop, value, msg) { new Assertion( obj, msg, assert.notOwnPropertyVal, true ).to.not.have.own.property(prop, value); }; /** * ### .deepOwnPropertyVal(object, property, value, [message]) * * Asserts that `object` has a direct property named by `property` and a value * equal to the provided `value`. Uses a deep equality check. Inherited * properties aren't checked. * * assert.deepOwnPropertyVal({ tea: { green: 'matcha' } }, 'tea', { green: 'matcha' }); * * @name deepOwnPropertyVal * @param {object} obj * @param {string} prop * @param {unknown} value * @param {string} msg * @public */ assert.deepOwnPropertyVal = function (obj, prop, value, msg) { new Assertion( obj, msg, assert.deepOwnPropertyVal, true ).to.have.deep.own.property(prop, value); }; /** * ### .notDeepOwnPropertyVal(object, property, value, [message]) * * Asserts that `object` does _not_ have a direct property named by `property` * with a value equal to the provided `value`. Uses a deep equality check. * Inherited properties aren't checked. * * assert.notDeepOwnPropertyVal({ tea: { green: 'matcha' } }, 'tea', { black: 'matcha' }); * assert.notDeepOwnPropertyVal({ tea: { green: 'matcha' } }, 'tea', { green: 'oolong' }); * assert.notDeepOwnPropertyVal({ tea: { green: 'matcha' } }, 'coffee', { green: 'matcha' }); * assert.notDeepOwnPropertyVal({}, 'toString', Object.prototype.toString); * * @name notDeepOwnPropertyVal * @param {object} obj * @param {string} prop * @param {unknown} value * @param {string} msg * @public */ assert.notDeepOwnPropertyVal = function (obj, prop, value, msg) { new Assertion( obj, msg, assert.notDeepOwnPropertyVal, true ).to.not.have.deep.own.property(prop, value); }; /** * ### .nestedProperty(object, property, [message]) * * Asserts that `object` has a direct or inherited property named by * `property`, which can be a string using dot- and bracket-notation for * nested reference. * * assert.nestedProperty({ tea: { green: 'matcha' }}, 'tea.green'); * * @name nestedProperty * @param {object} obj * @param {string} prop * @param {string} msg * @namespace Assert * @public */ assert.nestedProperty = function (obj, prop, msg) { new Assertion(obj, msg, assert.nestedProperty, true).to.have.nested.property( prop ); }; /** * ### .notNestedProperty(object, property, [message]) * * Asserts that `object` does _not_ have a property named by `property`, which * can be a string using dot- and bracket-notation for nested reference. The * property cannot exist on the object nor anywhere in its prototype chain. * * assert.notNestedProperty({ tea: { green: 'matcha' }}, 'tea.oolong'); * * @name notNestedProperty * @param {object} obj * @param {string} prop * @param {string} msg * @namespace Assert * @public */ assert.notNestedProperty = function (obj, prop, msg) { new Assertion( obj, msg, assert.notNestedProperty, true ).to.not.have.nested.property(prop); }; /** * ### .nestedPropertyVal(object, property, value, [message]) * * Asserts that `object` has a property named by `property` with value given * by `value`. `property` can use dot- and bracket-notation for nested * reference. Uses a strict equality check (===). * * assert.nestedPropertyVal({ tea: { green: 'matcha' }}, 'tea.green', 'matcha'); * * @name nestedPropertyVal * @param {object} obj * @param {string} prop * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.nestedPropertyVal = function (obj, prop, val, msg) { new Assertion( obj, msg, assert.nestedPropertyVal, true ).to.have.nested.property(prop, val); }; /** * ### .notNestedPropertyVal(object, property, value, [message]) * * Asserts that `object` does _not_ have a property named by `property` with * value given by `value`. `property` can use dot- and bracket-notation for * nested reference. Uses a strict equality check (===). * * assert.notNestedPropertyVal({ tea: { green: 'matcha' }}, 'tea.green', 'konacha'); * assert.notNestedPropertyVal({ tea: { green: 'matcha' }}, 'coffee.green', 'matcha'); * * @name notNestedPropertyVal * @param {object} obj * @param {string} prop * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.notNestedPropertyVal = function (obj, prop, val, msg) { new Assertion( obj, msg, assert.notNestedPropertyVal, true ).to.not.have.nested.property(prop, val); }; /** * ### .deepNestedPropertyVal(object, property, value, [message]) * * Asserts that `object` has a property named by `property` with a value given * by `value`. `property` can use dot- and bracket-notation for nested * reference. Uses a deep equality check. * * assert.deepNestedPropertyVal({ tea: { green: { matcha: 'yum' } } }, 'tea.green', { matcha: 'yum' }); * * @name deepNestedPropertyVal * @param {object} obj * @param {string} prop * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.deepNestedPropertyVal = function (obj, prop, val, msg) { new Assertion( obj, msg, assert.deepNestedPropertyVal, true ).to.have.deep.nested.property(prop, val); }; /** * ### .notDeepNestedPropertyVal(object, property, value, [message]) * * Asserts that `object` does _not_ have a property named by `property` with * value given by `value`. `property` can use dot- and bracket-notation for * nested reference. Uses a deep equality check. * * assert.notDeepNestedPropertyVal({ tea: { green: { matcha: 'yum' } } }, 'tea.green', { oolong: 'yum' }); * assert.notDeepNestedPropertyVal({ tea: { green: { matcha: 'yum' } } }, 'tea.green', { matcha: 'yuck' }); * assert.notDeepNestedPropertyVal({ tea: { green: { matcha: 'yum' } } }, 'tea.black', { matcha: 'yum' }); * * @name notDeepNestedPropertyVal * @param {object} obj * @param {string} prop * @param {unknown} val * @param {string} msg * @namespace Assert * @public */ assert.notDeepNestedPropertyVal = function (obj, prop, val, msg) { new Assertion( obj, msg, assert.notDeepNestedPropertyVal, true ).to.not.have.deep.nested.property(prop, val); }; /** * ### .lengthOf(object, length, [message]) * * Asserts that `object` has a `length` or `size` with the expected value. * * assert.lengthOf([1,2,3], 3, 'array has length of 3'); * assert.lengthOf('foobar', 6, 'string has length of 6'); * assert.lengthOf(new Set([1,2,3]), 3, 'set has size of 3'); * assert.lengthOf(new Map([['a',1],['b',2],['c',3]]), 3, 'map has size of 3'); * * @name lengthOf * @param {unknown} exp * @param {number} len * @param {string} msg * @namespace Assert * @public */ assert.lengthOf = function (exp, len, msg) { new Assertion(exp, msg, assert.lengthOf, true).to.have.lengthOf(len); }; /** * ### .hasAnyKeys(object, [keys], [message]) * * Asserts that `object` has at least one of the `keys` provided. * You can also provide a single object instead of a `keys` array and its keys * will be used as the expected set of keys. * * assert.hasAnyKeys({foo: 1, bar: 2, baz: 3}, ['foo', 'iDontExist', 'baz']); * assert.hasAnyKeys({foo: 1, bar: 2, baz: 3}, {foo: 30, iDontExist: 99, baz: 1337}); * assert.hasAnyKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{foo: 1}, 'key']); * assert.hasAnyKeys(new Set([{foo: 'bar'}, 'anotherKey']), [{foo: 'bar'}, 'anotherKey']); * * @name hasAnyKeys * @param {unknown} obj * @param {Array | object} keys * @param {string} msg * @namespace Assert * @public */ assert.hasAnyKeys = function (obj, keys, msg) { new Assertion(obj, msg, assert.hasAnyKeys, true).to.have.any.keys(keys); }; /** * ### .hasAllKeys(object, [keys], [message]) * * Asserts that `object` has all and only all of the `keys` provided. * You can also provide a single object instead of a `keys` array and its keys * will be used as the expected set of keys. * * assert.hasAllKeys({foo: 1, bar: 2, baz: 3}, ['foo', 'bar', 'baz']); * assert.hasAllKeys({foo: 1, bar: 2, baz: 3}, {foo: 30, bar: 99, baz: 1337]); * assert.hasAllKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{foo: 1}, 'key']); * assert.hasAllKeys(new Set([{foo: 'bar'}, 'anotherKey']), [{foo: 'bar'}, 'anotherKey']); * * @name hasAllKeys * @param {unknown} obj * @param {string[]} keys * @param {string} msg * @namespace Assert * @public */ assert.hasAllKeys = function (obj, keys, msg) { new Assertion(obj, msg, assert.hasAllKeys, true).to.have.all.keys(keys); }; /** * ### .containsAllKeys(object, [keys], [message]) * * Asserts that `object` has all of the `keys` provided but may have more keys not listed. * You can also provide a single object instead of a `keys` array and its keys * will be used as the expected set of keys. * * assert.containsAllKeys({foo: 1, bar: 2, baz: 3}, ['foo', 'baz']); * assert.containsAllKeys({foo: 1, bar: 2, baz: 3}, ['foo', 'bar', 'baz']); * assert.containsAllKeys({foo: 1, bar: 2, baz: 3}, {foo: 30, baz: 1337}); * assert.containsAllKeys({foo: 1, bar: 2, baz: 3}, {foo: 30, bar: 99, baz: 1337}); * assert.containsAllKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{foo: 1}]); * assert.containsAllKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{foo: 1}, 'key']); * assert.containsAllKeys(new Set([{foo: 'bar'}, 'anotherKey']), [{foo: 'bar'}]); * assert.containsAllKeys(new Set([{foo: 'bar'}, 'anotherKey']), [{foo: 'bar'}, 'anotherKey']); * * @name containsAllKeys * @param {unknown} obj * @param {string[]} keys * @param {string} msg * @namespace Assert * @public */ assert.containsAllKeys = function (obj, keys, msg) { new Assertion(obj, msg, assert.containsAllKeys, true).to.contain.all.keys( keys ); }; /** * ### .doesNotHaveAnyKeys(object, [keys], [message]) * * Asserts that `object` has none of the `keys` provided. * You can also provide a single object instead of a `keys` array and its keys * will be used as the expected set of keys. * * assert.doesNotHaveAnyKeys({foo: 1, bar: 2, baz: 3}, ['one', 'two', 'example']); * assert.doesNotHaveAnyKeys({foo: 1, bar: 2, baz: 3}, {one: 1, two: 2, example: 'foo'}); * assert.doesNotHaveAnyKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{one: 'two'}, 'example']); * assert.doesNotHaveAnyKeys(new Set([{foo: 'bar'}, 'anotherKey']), [{one: 'two'}, 'example']); * * @name doesNotHaveAnyKeys * @param {unknown} obj * @param {string[]} keys * @param {string} msg * @namespace Assert * @public */ assert.doesNotHaveAnyKeys = function (obj, keys, msg) { new Assertion(obj, msg, assert.doesNotHaveAnyKeys, true).to.not.have.any.keys( keys ); }; /** * ### .doesNotHaveAllKeys(object, [keys], [message]) * * Asserts that `object` does not have at least one of the `keys` provided. * You can also provide a single object instead of a `keys` array and its keys * will be used as the expected set of keys. * * assert.doesNotHaveAllKeys({foo: 1, bar: 2, baz: 3}, ['one', 'two', 'example']); * assert.doesNotHaveAllKeys({foo: 1, bar: 2, baz: 3}, {one: 1, two: 2, example: 'foo'}); * assert.doesNotHaveAllKeys(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{one: 'two'}, 'example']); * assert.doesNotHaveAllKeys(new Set([{foo: 'bar'}, 'anotherKey']), [{one: 'two'}, 'example']); * * @name doesNotHaveAllKeys * @param {unknown} obj * @param {string[]} keys * @param {string} msg * @namespace Assert * @public */ assert.doesNotHaveAllKeys = function (obj, keys, msg) { new Assertion(obj, msg, assert.doesNotHaveAllKeys, true).to.not.have.all.keys( keys ); }; /** * ### .hasAnyDeepKeys(object, [keys], [message]) * * Asserts that `object` has at least one of the `keys` provided. * Since Sets and Maps can have objects as keys you can use this assertion to perform * a deep comparison. * You can also provide a single object instead of a `keys` array and its keys * will be used as the expected set of keys. * * assert.hasAnyDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [1, 2]]), {one: 'one'}); * assert.hasAnyDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [1, 2]]), [{one: 'one'}, {two: 'two'}]); * assert.hasAnyDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [{two: 'two'}, 'valueTwo']]), [{one