spacer

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...
Tweet

TDD Style Introduction

module.exports()

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');

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
View Source
module.exports = function (chai) {
            

.fail(actual, expect, msg, operator)

assert.fail()

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

Throw a failure. Node.js compatible.

View Source
assert.fail = function (actual, expected, message, operator) {
    throw new chai.AssertionError({
        actual: actual
      , expected: expected
      , message: message
      , operator: operator
      , stackStartFunction: assert.fail
    });
  }
            

.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) {
    var test = new Assertion(act, msg);

    test.assert(
        exp == test.obj
      , 'expected ' + test.inspect + ' to equal ' + inspect(exp)
      , 'expected ' + test.inspect + ' to not equal ' + inspect(exp));
  };
            

.notEqual(actual, expected, [message])

assert.notEqual()

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

Assert not equal.

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

    test.assert(
        exp != test.obj
      , 'expected ' + test.inspect + ' to equal ' + inspect(exp)
      , 'expected ' + test.inspect + ' to not equal ' + inspect(exp));
  };
            

.strictEqual(actual, expected, [message])

assert.strictEqual()

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

Assert strict equality.

 assert.strictEqual(true, true, 'these booleans are strictly equal');
View Source
assert.strictEqual = function (act, exp, msg) {
    new Assertion(act, msg).to.equal(exp);
  };
            

.notStrictEqual(actual, expected, [message])

assert.notStrictEqual()

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

Assert strict equality.

 assert.notStrictEqual(1, true, 'these booleans are not strictly equal');
View Source
assert.notStrictEqual = 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.equal(null);
  };
            

.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.not.equal(null);
  };
            

.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);
  };
            

.isDefined(value, [message])

assert.isDefined()

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

Assert value is not undefined.

 var tea = 'cup of chai';
 assert.isDefined(tea, 'no tea defined');
View Source
assert.isDefined = function (val, msg) {
    new Assertion(val, msg).to.not.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.a('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.a('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 }value
@param{ * }includes
@param{ String }message
@apipublic

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);
View Source
assert.include = function (exp, inc, msg) {
    var obj = new Assertion(exp, msg);

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

.match(value, regex, [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);
  };
            

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

assert.throws()

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

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

 assert.throw(fn, ReferenceError, 'function throw reference error');
View Source
assert.throws = function (fn, type, msg) {
    if ('string' === typeof type) {
      msg = type;
      type = null;
    }

    new Assertion(fn, msg).to.throw(type);
  };
            

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

assert.doesNotThrow()

@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 (err) { if (err) throw Error(err) };
 assert.doesNotThrow(fn, Error, 'function throw reference error');
View Source
assert.doesNotThrow = function (fn, type, msg) {
    if ('string' === typeof type) {
      msg = type;
      type = null;
    }

    new Assertion(fn, msg).to.not.throw(type);
  };
            

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

assert.operator()

@param{ * }objectto test
@param{ String }operator
@param{ * }secondobject
@param{ String }message
@apipublic
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.