The assert
style is very similar to node.js’ included assert module, with a bit of extra sugar. Of the three style options, assert
is the only one that is not chainable. Check out the Style Guide for a comparison.
Write your own test expressions.
Throw a failure. Node.js assert
module-compatible.
Asserts that object
is truthy.
Asserts that object
is falsy.
Asserts non-strict equality (==
) of actual
and expected
.
Asserts non-strict inequality (!=
) of actual
and expected
.
Asserts strict equality (===
) of actual
and expected
.
Asserts strict inequality (!==
) of actual
and expected
.
Asserts that actual
is deeply equal to expected
.
Assert that actual
is not deeply equal to expected
.
Asserts valueToCheck
is strictly greater than (>) valueToBeAbove
.
Asserts valueToCheck
is greater than or equal to (>=) valueToBeAtLeast
.
Asserts valueToCheck
is strictly less than (<) valueToBeBelow
.
Asserts valueToCheck
is less than or equal to (<=) valueToBeAtMost
.
Asserts that value
is true.
Asserts that value
is not true.
Asserts that value
is false.
Asserts that value
is not false.
Asserts that value
is not null.
Asserts that the target is neither null
nor undefined
.
Asserts that the target is either null
or undefined
.
Asserts that value
is undefined
.
Asserts that value
is not undefined
.
Asserts that value
is a function.
Asserts that value
is not a function.
Asserts that value
is an object of type ‘Object’ (as revealed by Object.prototype.toString
). The assertion does not match subclassed objects.
Asserts that value
is not an object of type ‘Object’ (as revealed by Object.prototype.toString
).
Asserts that value
is an array.
Asserts that value
is not an array.
Asserts that value
is a string.
Asserts that value
is not a string.
Asserts that value
is a number.
Asserts that value
is not a number.
Asserts that value
is a finite number. Unlike .isNumber
, this will fail for NaN
and Infinity
.
Asserts that value
is a boolean.
Asserts that value
is not a boolean.
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');
Asserts that value
’s type is not name
, as determined by Object.prototype.toString
.
Asserts that value
is an instance of constructor
.
Asserts value
is not an instance of constructor
.
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 stricty equal to the given property value. For instance:
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:
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.
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.
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.
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.
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.
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.
Asserts that ‘haystack’ includes ‘needle’. Can be used to assert the inclusion of a subset of properties in an object while ignoring inherited properties.
Asserts that ‘haystack’ includes ‘needle’. Can be used to assert the absence of a subset of properties in an object while ignoring inherited properties.
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.
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.
Asserts that value
matches the regular expression regexp
.
Asserts that value
does not match the regular expression regexp
.
Asserts that object
has a direct or inherited property named by property
.
Asserts that object
does not have a direct or inherited property named by property
.
Asserts that object
has a direct or inherited property named by property
with a value given by value
. Uses a strict equality check (===).
Asserts that object
does not have a direct or inherited property named by property
with value given by value
. Uses a strict equality check (===).
Asserts that object
has a direct or inherited property named by property
with a value given by value
. Uses a deep equality check.
Asserts that object
does not have a direct or inherited property named by property
with value given by value
. Uses a deep equality check.
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.
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.
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 (===).
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 (===).
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.
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' });
Asserts that object
has a length
property with the expected value.
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.hasAnyKey({foo: 1, bar: 2, baz: 3}, ['foo', 'iDontExist', 'baz']);
assert.hasAnyKey({foo: 1, bar: 2, baz: 3}, {foo: 30, iDontExist: 99, baz: 1337]);
assert.hasAnyKey(new Map([[{foo: 1}, 'bar'], ['key', 'value']]), [{foo: 1}, 'thisKeyDoesNotExist']);
assert.hasAnyKey(new Set([{foo: 'bar'}, 'anotherKey'], [{foo: 'bar'}, 'thisKeyDoesNotExist']);
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']);
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']);
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']);
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']);
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: 'one'}, {two: 'two'}]);
assert.hasAnyDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), {one: 'one'});
assert.hasAnyDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{one: 'one'}, {three: 'three'}]);
assert.hasAnyDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{one: 'one'}, {two: 'two'}]);
Asserts that object
has all and only all 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.hasAllDeepKeys(new Map([[{one: 'one'}, 'valueOne']]), {one: 'one'});
assert.hasAllDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [{two: 'two'}, 'valueTwo']]), [{one: 'one'}, {two: 'two'}]);
assert.hasAllDeepKeys(new Set([{one: 'one'}]), {one: 'one'});
assert.hasAllDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{one: 'one'}, {two: 'two'}]);
Asserts that object
contains all 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.containsAllDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [1, 2]]), {one: 'one'});
assert.containsAllDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [{two: 'two'}, 'valueTwo']]), [{one: 'one'}, {two: 'two'}]);
assert.containsAllDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), {one: 'one'});
assert.containsAllDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{one: 'one'}, {two: 'two'}]);
Asserts that object
has none 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.doesNotHaveAnyDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [1, 2]]), {thisDoesNot: 'exist'});
assert.doesNotHaveAnyDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [{two: 'two'}, 'valueTwo']]), [{twenty: 'twenty'}, {fifty: 'fifty'}]);
assert.doesNotHaveAnyDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), {twenty: 'twenty'});
assert.doesNotHaveAnyDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{twenty: 'twenty'}, {fifty: 'fifty'}]);
Asserts that object
does not have 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.doesNotHaveAllDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [1, 2]]), {thisDoesNot: 'exist'});
assert.doesNotHaveAllDeepKeys(new Map([[{one: 'one'}, 'valueOne'], [{two: 'two'}, 'valueTwo']]), [{twenty: 'twenty'}, {one: 'one'}]);
assert.doesNotHaveAllDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), {twenty: 'twenty'});
assert.doesNotHaveAllDeepKeys(new Set([{one: 'one'}, {two: 'two'}]), [{one: 'one'}, {fifty: 'fifty'}]);
If errorLike
is an Error
constructor, asserts that fn
will throw an error that is an instance of errorLike
. If errorLike
is an Error
instance, asserts that the error thrown is the same instance as errorLike
. If errMsgMatcher
is provided, it also asserts that the error thrown will have a message matching errMsgMatcher
.
assert.throws(fn, 'function throws a reference error');
assert.throws(fn, /function throws a reference error/);
assert.throws(fn, ReferenceError);
assert.throws(fn, errorInstance);
assert.throws(fn, ReferenceError, 'Error thrown must be a ReferenceError and have this msg');
assert.throws(fn, errorInstance, 'Error thrown must be the same errorInstance and have this msg');
assert.throws(fn, ReferenceError, /Error thrown must be a ReferenceError and match this/);
assert.throws(fn, errorInstance, /Error thrown must be the same errorInstance and match this/);
If errorLike
is an Error
constructor, asserts that fn
will not throw an error that is an instance of errorLike
. If errorLike
is an Error
instance, asserts that the error thrown is not the same instance as errorLike
. If errMsgMatcher
is provided, it also asserts that the error thrown will not have a message matching errMsgMatcher
.
assert.doesNotThrow(fn, 'Any Error thrown must not have this message');
assert.doesNotThrow(fn, /Any Error thrown must not match this/);
assert.doesNotThrow(fn, Error);
assert.doesNotThrow(fn, errorInstance);
assert.doesNotThrow(fn, Error, 'Error must not have this message');
assert.doesNotThrow(fn, errorInstance, 'Error must not have this message');
assert.doesNotThrow(fn, Error, /Error must not match this/);
assert.doesNotThrow(fn, errorInstance, /Error must not match this/);
Compares two values using operator
.
Asserts that the target is equal expected
, to within a +/- delta
range.
Asserts that the target is equal expected
, to within a +/- delta
range.
Asserts that set1
and set2
have the same members in any order. Uses a strict equality check (===).
Asserts that set1
and set2
don’t have the same members in any order. Uses a strict equality check (===).
Asserts that set1
and set2
have the same members in any order. Uses a deep equality check.
Asserts that set1
and set2
don’t have the same members in any order. Uses a deep equality check.
Asserts that set1
and set2
have the same members in the same order. Uses a strict equality check (===).
Asserts that set1
and set2
don’t have the same members in the same order. Uses a strict equality check (===).
Asserts that set1
and set2
have the same members in the same order. Uses a deep equality check.
assert.sameDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { a: 1 }, { b: 2 }, { c: 3 } ], ‘same deep ordered members’);
Asserts that set1
and set2
don’t have the same members in the same order. Uses a deep equality check.
assert.notSameDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { a: 1 }, { b: 2 }, { z: 5 } ], ‘not same deep ordered members’); assert.notSameDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { b: 2 }, { a: 1 }, { c: 3 } ], ‘not same deep ordered members’);
Asserts that subset
is included in superset
in any order. Uses a strict equality check (===). Duplicates are ignored.
Asserts that subset
isn’t included in superset
in any order. Uses a strict equality check (===). Duplicates are ignored.
Asserts that subset
is included in superset
in any order. Uses a deep equality check. Duplicates are ignored.
Asserts that subset
isn’t included in superset
in any order. Uses a deep equality check. Duplicates are ignored.
Asserts that subset
is included in superset
in the same order beginning with the first element in superset
. Uses a strict equality check (===).
Asserts that subset
isn’t included in superset
in the same order beginning with the first element in superset
. Uses a strict equality check (===).
Asserts that subset
is included in superset
in the same order beginning with the first element in superset
. Uses a deep equality check.
Asserts that subset
isn’t included in superset
in the same order beginning with the first element in superset
. Uses a deep equality check.
assert.notIncludeDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { a: 1 }, { f: 5 } ], 'not include deep ordered members');
assert.notIncludeDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { b: 2 }, { a: 1 } ], 'not include deep ordered members');
assert.notIncludeDeepOrderedMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [ { b: 2 }, { c: 3 } ], 'not include deep ordered members');
Asserts that non-object, non-array value inList
appears in the flat array list
.
Asserts that a function changes the value of a property.
Asserts that a function changes the value of a property by an amount (delta).
Asserts that a function does not change the value of a property.
Asserts that a function does not change the value of a property or of a function’s return value by an amount (delta)
Asserts that a function increases a numeric object property.
Asserts that a function increases a numeric object property or a function’s return value by an amount (delta).
Asserts that a function does not increase a numeric object property.
Asserts that a function does not increase a numeric object property or function’s return value by an amount (delta).
Asserts that a function decreases a numeric object property.
Asserts that a function decreases a numeric object property or a function’s return value by an amount (delta)
Asserts that a function does not decreases a numeric object property.
Asserts that a function does not decreases a numeric object property or a function’s return value by an amount (delta)
Asserts that a function does not decreases a numeric object property or a function’s return value by an amount (delta)
Asserts if value is not a false value, and throws if it is a true value. This is added to allow for chai to be a drop-in replacement for Node’s assert class.
Asserts that object
is extensible (can have new properties added to it).
Asserts that object
is not extensible.
Asserts that object
is sealed (cannot have new properties added to it and its existing properties cannot be removed).
Asserts that object
is frozen (cannot have new properties added to it and its existing properties cannot be modified).
Asserts that the target does not contain any values. For arrays and strings, it checks the length
property. For Map
and Set
instances, it checks the size
property. For non-function objects, it gets the count of own enumerable string keys.
Asserts that the target contains values. For arrays and strings, it checks the length
property. For Map
and Set
instances, it checks the size
property. For non-function objects, it gets the count of own enumerable string keys.
© 2016 Chai.js Assertion Library
Licensed under the MIT License.
http://chaijs.com/api/assert/