The assert (TDD) style is exposed through assert interfaces. This provides the classic assert-dot notation, similiar to that packaged with node.js. This assert module, however, provides several additional tests and is browser compatible.

// assert
var assert = require('chai').assert;
  , foo = 'bar';

assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');

Configuration

By default, Chai does not show stack traces upon an AssertionError. This can be changed by modifying the includeStack parameter for chai.Assertion. For example:

var chai = require('chai');
chai.Assertion.includeStack = true; // defaults to false

Assert API

assert(expressions, errorMessage)

  • @param{ Mixed }expressionto test for truthiness.
  • @param{ String }messageto display on error

Write your own test expressions.

.fail(actual, expect, msg, operator)

  • @param{ * }actualvalue
  • @param{ * }expectedvalue
  • @param{ String }message
  • @param{ String }operator

Throw a failure. Node.js compatible.

.ok(object, [message])

  • @param{ * }objectto test
  • @param{ String }message

Assert object is truthy.

 assert.ok('everthing', 'everything is ok');
 assert.ok(false, 'this will fail');

.equal(actual, expected, [message])

  • @param{ * }actual
  • @param{ * }expected
  • @param{ String }message

Assert strict equality.

 assert.equal(3, 3, 'these numbers are equal');

.notEqual(actual, expected, [message])

  • @param{ * }actual
  • @param{ * }expected
  • @param{ String }message

Assert not equal.

 assert.notEqual(3, 4, 'these numbers are not equal');

.strictEqual(actual, expected, [message])

  • @param{ * }actual
  • @param{ * }expected
  • @param{ String }message

Assert strict equality.

 assert.strictEqual(true, true, 'these booleans are strictly equal');

.notStrictEqual(actual, expected, [message])

  • @param{ * }actual
  • @param{ * }expected
  • @param{ String }message

Assert strict equality.

 assert.notStrictEqual(1, true, 'these booleans are not strictly equal');

.deepEqual(actual, expected, [message])

  • @param{ * }actual
  • @param{ * }expected
  • @param{ String }message

Assert not deep equality.

 assert.deepEqual({ tea: 'green' }, { tea: 'green' });

.notDeepEqual(actual, expected, [message])

  • @param{ * }actual
  • @param{ * }expected
  • @param{ String }message

Assert not deep equality.

 assert.notDeepEqual({ tea: 'green' }, { tea: 'jasmine' });

.isTrue(value, [message])

  • @param{ Boolean }value
  • @param{ String }message

Assert value is true.

 var tea_served = true;
 assert.isTrue(tea_served, 'the tea has been served');

.isFalse(value, [message])

  • @param{ Boolean }value
  • @param{ String }message

Assert value is false.

 var tea_served = false;
 assert.isFalse(tea_served, 'no tea yet? hmm...');

.isNull(value, [message])

  • @param{ * }value
  • @param{ String }message

Assert value is null.

 assert.isNull(err, 'no errors');

.isNotNull(value, [message])

  • @param{ * }value
  • @param{ String }message

Assert value is not null.

 var tea = 'tasty chai';
 assert.isNotNull(tea, 'great, time for tea!');

.isUndefined(value, [message])

  • @param{ * }value
  • @param{ String }message

Assert value is undefined.

 assert.isUndefined(tea, 'no tea defined');

.isDefined(value, [message])

  • @param{ * }value
  • @param{ String }message

Assert value is not undefined.

 var tea = 'cup of chai';
 assert.isDefined(tea, 'no tea defined');

.isFunction(value, [message])

  • @param{ Function }value
  • @param{ String }message

Assert value is a function.

 var serve_tea = function () { return 'cup of tea'; };
 assert.isFunction(serve_tea, 'great, we can have tea now');

.isNotFunction(value, [message])

  • @param{ Mixed }value
  • @param{ String }message

Assert value is NOT a function.

 var serve_tea = [ 'heat', 'pour', 'sip' ];
 assert.isNotFunction(serve_tea, 'great, we can have tea now');

.isObject(value, [message])

  • @param{ Object }value
  • @param{ String }message

Assert value is an object.

 var selection = { name: 'Chai', serve: 'with spices' };
 assert.isObject(selection, 'tea selection is an object');

.isNotObject(value, [message])

  • @param{ Mixed }value
  • @param{ String }message

Assert value is NOT an object.

 var selection = 'chai'
 assert.isObject(selection, 'tea selection is not an object');

.isArray(value, [message])

  • @param{ Mixed }value
  • @param{ String }message

Assert value is an instance of Array.

 var menu = [ 'green', 'chai', 'oolong' ];
 assert.isArray(menu, 'what kind of tea do we want?');

.isArray(value, [message])

  • @param{ Mixed }value
  • @param{ String }message

Assert value is NOT an instance of Array.

 var menu = 'green|chai|oolong';
 assert.isNotArray(menu, 'what kind of tea do we want?');

.isString(value, [message])

  • @param{ String }value
  • @param{ String }message

Assert value is a string.

 var teaorder = 'chai';
 assert.isString(tea_order, 'order placed');

.isNotString(value, [message])

  • @param{ Mixed }value
  • @param{ String }message

Assert value is NOT a string.

 var teaorder = 4;
 assert.isNotString(tea_order, 'order placed');

.isNumber(value, [message])

  • @param{ Number }value
  • @param{ String }message

Assert value is a number

 var cups = 2;
 assert.isNumber(cups, 'how many cups');

.isNotNumber(value, [message])

  • @param{ Mixed }value
  • @param{ String }message

Assert value NOT is a number

 var cups = '2 cups please';
 assert.isNotNumber(cups, 'how many cups');

.isBoolean(value, [message])

  • @param{ Mixed }value
  • @param{ String }message

Assert value is a boolean

 var teaready = true
   , teaserved = false;

 assert.isBoolean(tea_ready, 'is the tea ready');
 assert.isBoolean(tea_served, 'has tea been served');

.isNotBoolean(value, [message])

  • @param{ Mixed }value
  • @param{ String }message

Assert value is NOT a boolean

 var teaready = 'yep'
   , teaserved = 'nope';

 assert.isNotBoolean(tea_ready, 'is the tea ready');
 assert.isNotBoolean(tea_served, 'has tea been served');

.typeOf(value, name, [message])

  • @param{ Mixed }value
  • @param{ String }typeofname
  • @param{ String }message

Assert typeof value is name.

 assert.typeOf('tea', 'string', 'we have a string');

.notTypeOf(value, name, [message])

  • @param{ Mixed }value
  • @param{ String }typeofname
  • @param{ String }message

Assert typeof value is NOT name.

 assert.notTypeOf('tea', 'string', 'we have a string');

.instanceOf(object, constructor, [message])

  • @param{ Object }object
  • @param{ Constructor }constructor
  • @param{ String }message

Assert value is instanceof constructor.

 var Tea = function (name) { this.name = name; }
   , Chai = new Tea('chai');

 assert.instanceOf(Chai, Tea, 'chai is an instance of tea');

.notInstanceOf(object, constructor, [message])

  • @param{ Object }object
  • @param{ Constructor }constructor
  • @param{ String }message

Assert value is NOT instanceof constructor.

 var Tea = function (name) { this.name = name; }
   , Chai = new String('chai');

 assert.notInstanceOf(Chai, Tea, 'chai is an instance of tea');

.include(value, includes, [message])

  • @param{ Array | String }value
  • @param{ * }includes
  • @param{ String }message

Assert the inclusion of an object in another. Works for strings and arrays.

 assert.include('foobar', 'bar', 'foobar contains string `var`);
 assert.include([ 1, 2, 3], 3, 'array contains value);

.match(value, regex, [message])

  • @param{ * }value
  • @param{ RegExp }RegularExpression
  • @param{ String }message

Assert that value matches regular expression.

 assert.match('foobar', /^foo/, 'Regexp matches');

.notMatch(value, regex, [message])

  • @param{ * }value
  • @param{ RegExp }RegularExpression
  • @param{ String }message

Assert that value does not match regular expression.

 assert.notMatch('foobar', /^foo/, 'Regexp matches');

.property(object, property, [message])

  • @param{ Object }object
  • @param{ String }Propertyaddress
  • @param{ String }message

Assert that object has property. Can use dot-notation for deep reference.

 assert.property({ tea: { green: 'matcha' }}, 'tea.green');

.notOwnProperty(object, property, [message])

  • @param{ Object }object
  • @param{ String }propertyaddress
  • @param{ String }message

Assert that object does not have property. Can use dot-notation for deep reference.

 assert.ownProperty({ tea: { green: 'matcha' }}, 'tea.oolong');

.propertyVal(object, property, value, [message])

  • @param{ Object }object
  • @param{ String }propertyaddress
  • @param{ Mixed }value
  • @param{ String }message

Assert that object has property with value. Can use dot-notation for deep reference.

 assert.propertyVal({ tea: { green: 'matcha' }}, 'tea.green', 'matcha');

.propertyNotVal(object, property, value, [message])

  • @param{ Object }object
  • @param{ String }propertyaddress
  • @param{ Mixed }value
  • @param{ String }message

Assert that object has property but value does not equal value. Can use dot-notation for deep reference.

 assert.propertyNotVal({ tea: { green: 'matcha' }}, 'tea.green', 'konacha');

.lengthOf(object, length, [message])

  • @param{ * }value
  • @param{ Number }length
  • @param{ String }message

Assert that object has expected length.

 assert.lengthOf([1,2,3], 3, 'Array has length of 3');
 assert.lengthOf('foobar', 5, 'String has length of 6');

.throws(function, [constructor/regexp], [message])

Assert that a function will throw a specific type of error.

 assert.throw(fn, ReferenceError, 'function throw reference error');

.doesNotThrow(function, [constructor/regexp], [message])

Assert that a function will throw a specific type of error.

 var fn = function (err) { if (err) throw Error(err) };
 assert.doesNotThrow(fn, Error, 'function throw reference error');

.operator(val, operator, val2, [message])

  • @param{ * }objectto test
  • @param{ String }operator
  • @param{ * }secondobject
  • @param{ String }message

Compare two values using operator.

 assert.operator(1, '<', 2, 'everything is ok');
 assert.operator(1, '>', 2, 'this will fail');