Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

Latest Update to Github

Loading...
Loading...
Fork me on GitHub
git clone https://github.com/logicalparadox/chai.git

TDD Style Introduction

The TDD style is exposed through assert interfaces. This provides
the classic assert.test 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');

.ok(object, [message])

assert.ok()

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

Assert object is truthy.

 assert.ok('everthing', 'everything is ok');
 assert.ok(false, 'this will fail');
View Source
assert.ok = function (val, msg) {
  new Assertion(val, msg).is.ok;
};
            

.equal(actual, expected, [message])

assert.equal()

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

Assert strict equality.

 assert.equal(3, 3, 'these numbers are equal');
View Source
assert.equal = function (act, exp, msg) {
  new Assertion(act, msg).to.equal(exp);
};
            

.notEqual(actual, expected, [message])

assert.notEqual()

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

Assert not strict equality.

 assert.notEqual(3, 4, 'these numbers are not equal');
View Source
assert.notEqual = function (act, exp, msg) {
  new Assertion(act, msg).to.not.equal(exp);
};
            

.deepEqual(actual, expected, [message])

assert.deepEqual()

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

Assert not deep equality.

 assert.deepEqual({ tea: 'green' }, { tea: 'green' });
View Source
assert.deepEqual = function (act, exp, msg) {
  new Assertion(act, msg).to.eql(exp);
};
            

.notDeepEqual(actual, expected, [message])

assert.notDeepEqual()

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

Assert not deep equality.

 assert.notDeepEqual({ tea: 'green' }, { tea: 'jasmine' });
View Source
assert.notDeepEqual = function (act, exp, msg) {
  new Assertion(act, msg).to.not.eql(exp);
};
            

.isTrue(value, [message])

assert.isTrue()

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

Assert value is true.

 var tea_served = true;
 assert.isTrue(tea_served, 'the tea has been served');
View Source
assert.isTrue = function (val, msg) {
  new Assertion(val, msg).is.true;
};
            

.isFalse(value, [message])

assert.isFalse()

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

Assert value is false.

 var tea_served = false;
 assert.isFalse(tea_served, 'no tea yet? hmm...');
View Source
assert.isFalse = function (val, msg) {
  new Assertion(val, msg).is.false;
};
            

.isNull(value, [message])

assert.isNull()

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

Assert value is null.

 assert.isNull(err, 'no errors');
View Source
assert.isNull = function (val, msg) {
  new Assertion(val, msg).to.not.exist;
};
            

.isNotNull(value, [message])

assert.isNotNull()

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

Assert value is not null.

 var tea = 'tasty chai';
 assert.isNotNull(tea, 'great, time for tea!');
View Source
assert.isNotNull = function (val, msg) {
  new Assertion(val, msg).to.exist;
};
            

.isUndefined(value, [message])

assert.isUndefined()

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

Assert value is undefined.

 assert.isUndefined(tea, 'no tea defined');
View Source
assert.isUndefined = function (val, msg) {
  new Assertion(val, msg).to.equal(undefined);
};
            

.isFunction(value, [message])

assert.isFunction()

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

Assert value is a function.

 var serve_tea = function () { return 'cup of tea'; };
 assert.isFunction(serve_tea, 'great, we can have tea now');
View Source
assert.isFunction = function (val, msg) {
  new Assertion(val, msg).to.be.a('function');
};
            

.isObject(value, [message])

assert.isObject()

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

Assert value is an object.

 var selection = { name: 'Chai', serve: 'with spices' };
 assert.isObject(selection, 'tea selection is an object');
View Source
assert.isObject = function (val, msg) {
  new Assertion(val, msg).to.be.an('object');
};
            

.isArray(value, [message])

assert.isArray()

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

Assert value is an instance of Array.

 var menu = [ 'green', 'chai', 'oolong' ];
 assert.isArray(menu, 'what kind of tea do we want?');
View Source
assert.isArray = function (val, msg) {
  new Assertion(val, msg).to.be.instanceof(Array);
};
            

.isString(value, [message])

assert.isString()

@param{ String }value
@param{ String }message
@apipublic

Assert value is a string.

 var teaorder = 'chai';
 assert.isString(tea_order, 'order placed');
View Source
assert.isString = function (val, msg) {
  new Assertion(val, msg).to.be.a('string');
};
            

.isNumber(value, [message])

assert.isNumber()

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

Assert value is a number

 var cups = 2;
 assert.isNumber(cups, 'how many cups');
View Source
assert.isNumber = function (val, msg) {
  new Assertion(val, msg).to.be.instanceof(Number);
};
            

.isBoolean(value, [message])

assert.isBoolean()

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

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');
View Source
assert.isBoolean = function (val, msg) {
  new Assertion(val, msg).to.be.a('boolean');
};
            

.typeOf(value, name, [message])

assert.typeOf()

@param{ * }value
@param{ String }typeofname
@param{ String }message
@apipublic

Assert typeof value is name.

 assert.typeOf('tea', 'string', 'we have a string');
View Source
assert.typeOf = function (val, type, msg) {
  new Assertion(val, msg).to.be.a(type);
};
            

.instanceOf(object, constructor, [message])

assert.instanceOf()

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

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');
View Source
assert.instanceOf = function (val, type, msg) {
  new Assertion(val, msg).to.be.instanceof(type);
};
            

.include(value, includes, [message])

assert.include()

@param{ Array | String | Object }value
@param{ * }includes
@param{ String }message
@apipublic

Assert the inclusion of an object in another.

 var obj = {foo: 'bar', baz: {baaz: 42}, qux: 13};
 assert.include(obj, {foo: 'bar'}, 'object contains subobject');

 assert.include('foobar', 'bar', 'foobar contains string `var`);
 assert.include([ 1, 2, 3], 3, 'array contains value);
View Source
assert.include = function (exp, inc, msg) {
  var obj = new Assertion(exp, msg);

  if (Array.isArray(exp)) {
    obj.to.contain(inc);
  } else if ('string' === typeof exp) {
    obj.to.include.string(inc);
  } else {
    obj.to.include.object(inc);
  }
};
            

.match(value, constructor, [message])

assert.match()

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

Assert that value matches regular expression.

 assert.match('foobar', /^foo/, 'Regexp matches');
View Source
assert.match = function (exp, re, msg) {
  new Assertion(exp, msg).to.match(re);
};
            

.length(value, constructor, [message])

assert.length()

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

Assert that object has expected length.

 assert.length([1,2,3], 3, 'Array has length of 3');
 assert.length('foobar', 5, 'String has length of 6');
View Source
assert.length = function (exp, len, msg) {
  new Assertion(exp, msg).to.have.length(len);
};
            

.throw(function, constructor, [message])

assert.throw()

@aliasthrows
@param{ Function }functionto test
@param{ ErrorConstructor }constructor
@param{ String }message
@see
@apipublic

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

 var fn = function () { throw new ReferenceError(''); }
 assert.throw(fn, ReferenceError, 'function throw reference error');
View Source
assert.throw = function (fn, type, msg) {
  new Assertions(fn, msg).to.throw(type);
};