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.
assert('foo' !== 'bar', 'foo is not bar');
assert(Array.isArray([]), 'empty arrays are arrays');
Throw a failure. Node.js assert
module-compatible.
Asserts that object
is truthy.
assert.isOk('everything', 'everything is ok');
assert.isOk(false, 'this will fail');
Asserts that object
is falsy.
assert.isNotOk('everything', 'this will fail');
assert.isNotOk(false, 'this will pass');
Asserts non-strict equality (==
) of actual
and expected
.
assert.equal(3, '3', '== coerces values to strings');
Asserts non-strict inequality (!=
) of actual
and expected
.
assert.notEqual(3, 4, 'these numbers are not equal');
Asserts strict equality (===
) of actual
and expected
.
assert.strictEqual(true, true, 'these booleans are strictly equal');
Asserts strict inequality (!==
) of actual
and expected
.
assert.notStrictEqual(3, '3', 'no coercion for strict equality');
Asserts that actual
is deeply equal to expected
.
assert.deepEqual({ tea: 'green' }, { tea: 'green' });
Assert that actual
is not deeply equal to expected
.
assert.notDeepEqual({ tea: 'green' }, { tea: 'jasmine' });
Asserts valueToCheck
is strictly greater than (>) valueToBeAbove
assert.isAbove(5, 2, '5 is strictly greater than 2');
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');
Asserts valueToCheck
is strictly less than (<) valueToBeBelow
assert.isBelow(3, 6, '3 is strictly less than 6');
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');
Asserts that value
is true.
var teaServed = true;
assert.isTrue(teaServed, 'the tea has been served');
Asserts that value
is not true.
var tea = 'tasty chai';
assert.isNotTrue(tea, 'great, time for tea!');
Asserts that value
is false.
var teaServed = false;
assert.isFalse(teaServed, 'no tea yet? hmm...');
Asserts that value
is not false.
var tea = 'tasty chai';
assert.isNotFalse(tea, 'great, time for tea!');
Asserts that value
is null.
assert.isNull(err, 'there was no error');
Asserts that value
is not null.
var tea = 'tasty chai';
assert.isNotNull(tea, 'great, time for tea!');
Asserts that value is NaN
assert.isNaN(‘foo’, ‘foo is NaN’);
Asserts that value is not NaN
assert.isNotNaN(4, ‘4 is not NaN’);
Asserts that value
is undefined
.
var tea;
assert.isUndefined(tea, 'no tea defined');
Asserts that value
is not undefined
.
var tea = 'cup of chai';
assert.isDefined(tea, 'tea has been defined');
Asserts that value
is a function.
function serveTea() { return 'cup of tea'; };
assert.isFunction(serveTea, 'great, we can have tea now');
Asserts that value
is not a function.
var serveTea = [ 'heat', 'pour', 'sip' ];
assert.isNotFunction(serveTea, 'great, we have listed the steps');
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');
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');
Asserts that value
is an array.
var menu = [ 'green', 'chai', 'oolong' ];
assert.isArray(menu, 'what kind of tea do we want?');
Asserts that value
is not an array.
var menu = 'green|chai|oolong';
assert.isNotArray(menu, 'what kind of tea do we want?');
Asserts that value
is a string.
var teaOrder = 'chai';
assert.isString(teaOrder, 'order placed');
Asserts that value
is not a string.
var teaOrder = 4;
assert.isNotString(teaOrder, 'order placed');
Asserts that value
is a number.
var cups = 2;
assert.isNumber(cups, 'how many cups');
Asserts that value
is not a number.
var cups = '2 cups please';
assert.isNotNumber(cups, 'how many cups');
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');
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');
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
.
assert.notTypeOf('tea', 'number', 'strings are not numbers');
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');
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');
Asserts that haystack
includes needle
. Works for strings and arrays.
assert.include('foobar', 'bar', 'foobar contains string "bar"');
assert.include([ 1, 2, 3 ], 3, 'array contains value');
Asserts that haystack
does not include needle
. Works for strings and arrays.
assert.notInclude('foobar', 'baz', 'string not include substring');
assert.notInclude([ 1, 2, 3 ], 4, 'array not include contain value');
Asserts that value
matches the regular expression regexp
.
assert.match('foobar', /^foo/, 'regexp matches');
Asserts that value
does not match the regular expression regexp
.
assert.notMatch('foobar', /^foo/, 'regexp does not match');
Asserts that object
has a property named by property
.
assert.property({ tea: { green: 'matcha' }}, 'tea');
Asserts that object
does not have a property named by property
.
assert.notProperty({ tea: { green: 'matcha' }}, 'coffee');
Asserts that object
has a property named by property
, which can be a string using dot- and bracket-notation for deep reference.
assert.deepProperty({ tea: { green: 'matcha' }}, 'tea.green');
Asserts that object
does not have a property named by property
, which can be a string using dot- and bracket-notation for deep reference.
assert.notDeepProperty({ tea: { green: 'matcha' }}, 'tea.oolong');
Asserts that object
has a property named by property
with value given by value
.
assert.propertyVal({ tea: 'is good' }, 'tea', 'is good');
Asserts that object
has a property named by property
, but with a value different from that given by value
.
assert.propertyNotVal({ tea: 'is good' }, 'tea', 'is bad');
Asserts that object
has a property named by property
with value given by value
. property
can use dot- and bracket-notation for deep reference.
assert.deepPropertyVal({ tea: { green: 'matcha' }}, 'tea.green', 'matcha');
Asserts that object
has a property named by property
, but with a value different from that given by value
. property
can use dot- and bracket-notation for deep reference.
assert.deepPropertyNotVal({ tea: { green: 'matcha' }}, 'tea.green', 'konacha');
Asserts that object
has a length
property with the expected value.
assert.lengthOf([1,2,3], 3, 'array has length of 3');
assert.lengthOf('foobar', 6, 'string has length of 6');
Asserts that function
will throw an error that is an instance of constructor
, or alternately that it will throw an error with message matching regexp
.
assert.throws(fn, 'function throws a reference error');
assert.throws(fn, /function throws a reference error/);
assert.throws(fn, ReferenceError);
assert.throws(fn, ReferenceError, 'function throws a reference error');
assert.throws(fn, ReferenceError, /function throws a reference error/);
Asserts that function
will not throw an error that is an instance of constructor
, or alternately that it will not throw an error with message matching regexp
.
assert.doesNotThrow(fn, Error, 'function does not throw');
Compares two values using operator
.
assert.operator(1, '<', 2, 'everything is ok');
assert.operator(1, '>', 2, 'this will fail');
Asserts that the target is equal expected
, to within a +/- delta
range.
assert.closeTo(1.5, 1, 0.5, 'numbers are close');
Asserts that the target is equal expected
, to within a +/- delta
range.
assert.approximately(1.5, 1, 0.5, 'numbers are close');
Asserts that set1
and set2
have the same members. Order is not taken into account.
assert.sameMembers([ 1, 2, 3 ], [ 2, 1, 3 ], 'same members');
Asserts that set1
and set2
have the same members - using a deep equality checking. Order is not taken into account.
assert.sameDeepMembers([ {b: 3}, {a: 2}, {c: 5} ], [ {c: 5}, {b: 3}, {a: 2} ], 'same deep members');
Asserts that subset
is included in superset
. Order is not taken into account.
assert.includeMembers([ 1, 2, 3 ], [ 2, 1 ], 'include members');
Asserts that subset
is included in superset
- using deep equality checking. Order is not taken into account. Duplicates are ignored.
assert.includeDeepMembers([ {a: 1}, {b: 2}, {c: 3} ], [ {b: 2}, {a: 1}, {b: 2} ], 'include deep members');
Asserts that non-object, non-array value inList
appears in the flat array list
.
assert.oneOf(1, [ 2, 1 ], 'Not found in list');
Asserts that a function changes the value of a property
var obj = { val: 10 };
var fn = function() { obj.val = 22 };
assert.changes(fn, obj, 'val');
Asserts that a function does not changes the value of a property
var obj = { val: 10 };
var fn = function() { console.log('foo'); };
assert.doesNotChange(fn, obj, 'val');
Asserts that a function increases an object property
var obj = { val: 10 };
var fn = function() { obj.val = 13 };
assert.increases(fn, obj, 'val');
Asserts that a function does not increase object property
var obj = { val: 10 };
var fn = function() { obj.val = 8 };
assert.doesNotIncrease(fn, obj, 'val');
Asserts that a function decreases an object property
var obj = { val: 10 };
var fn = function() { obj.val = 5 };
assert.decreases(fn, obj, 'val');
Asserts that a function does not decreases an object property
var obj = { val: 10 };
var fn = function() { obj.val = 15 };
assert.doesNotDecrease(fn, obj, 'val');
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.
var err = new Error('I am a custom error');
assert.ifError(err); // Rethrows err!
Asserts that object
is extensible (can have new properties added to it).
assert.isExtensible({});
Asserts that object
is not extensible.
var nonExtensibleObject = Object.preventExtensions({});
var sealedObject = Object.seal({});
var frozenObject = Object.freese({});
assert.isNotExtensible(nonExtensibleObject);
assert.isNotExtensible(sealedObject);
assert.isNotExtensible(frozenObject);
Asserts that object
is sealed (cannot have new properties added to it and its existing properties cannot be removed).
var sealedObject = Object.seal({});
var frozenObject = Object.seal({});
assert.isSealed(sealedObject);
assert.isSealed(frozenObject);
Asserts that object
is not sealed.
assert.isNotSealed({});
Asserts that object
is frozen (cannot have new properties added to it and its existing properties cannot be modified).
var frozenObject = Object.freeze({});
assert.frozen(frozenObject);
Asserts that object
is not frozen.
assert.isNotFrozen({});
© 2011–2015 Jake Luer
Licensed under the MIT License.
http://chaijs.com/api/assert/