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.
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;
Assert API
assert(expressions, errorMessage)
Write your own test expressions.
.fail(actual, expect, msg, operator)
Throw a failure. Node.js compatible.
.ok(object, [message])
Assert object is truthy.
assert.ok('everthing', 'everything is ok');
assert.ok(false, 'this will fail');
.equal(actual, expected, [message])
Assert strict equality.
assert.equal(3, 3, 'these numbers are equal');
.notEqual(actual, expected, [message])
Assert not equal.
assert.notEqual(3, 4, 'these numbers are not equal');
.strictEqual(actual, expected, [message])
Assert strict equality.
assert.strictEqual(true, true, 'these booleans are strictly equal');
.notStrictEqual(actual, expected, [message])
Assert strict equality.
assert.notStrictEqual(1, true, 'these booleans are not strictly equal');
.deepEqual(actual, expected, [message])
Assert not deep equality.
assert.deepEqual({ tea: 'green' }, { tea: 'green' });
.notDeepEqual(actual, expected, [message])
Assert not deep equality.
assert.notDeepEqual({ tea: 'green' }, { tea: 'jasmine' });
.isTrue(value, [message])
Assert value
is true.
var tea_served = true;
assert.isTrue(tea_served, 'the tea has been served');
.isFalse(value, [message])
Assert value
is false.
var tea_served = false;
assert.isFalse(tea_served, 'no tea yet? hmm...');
.isNull(value, [message])
Assert value
is null.
assert.isNull(err, 'no errors');
.isNotNull(value, [message])
Assert value
is not null.
var tea = 'tasty chai';
assert.isNotNull(tea, 'great, time for tea!');
.isUndefined(value, [message])
Assert value
is undefined.
assert.isUndefined(tea, 'no tea defined');
.isDefined(value, [message])
Assert value
is not undefined.
var tea = 'cup of chai';
assert.isDefined(tea, 'no tea defined');
.isFunction(value, [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])
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])
Assert value
is an object.
var selection = { name: 'Chai', serve: 'with spices' };
assert.isObject(selection, 'tea selection is an object');
.isNotObject(value, [message])
Assert value
is NOT an object.
var selection = 'chai'
assert.isObject(selection, 'tea selection is not an object');
.isArray(value, [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])
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])
Assert value
is a string.
var teaorder = 'chai';
assert.isString(tea_order, 'order placed');
.isNotString(value, [message])
Assert value
is NOT a string.
var teaorder = 4;
assert.isNotString(tea_order, 'order placed');
.isNumber(value, [message])
Assert value
is a number
var cups = 2;
assert.isNumber(cups, 'how many cups');
.isNotNumber(value, [message])
Assert value
NOT is a number
var cups = '2 cups please';
assert.isNotNumber(cups, 'how many cups');
.isBoolean(value, [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])
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])
Assert typeof value
is name
.
assert.typeOf('tea', 'string', 'we have a string');
.notTypeOf(value, name, [message])
Assert typeof value
is NOT name
.
assert.notTypeOf('tea', 'string', 'we have a string');
.instanceOf(object, constructor, [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])
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])
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])
Assert that value
matches regular expression.
assert.match('foobar', /^foo/, 'Regexp matches');
.notMatch(value, regex, [message])
Assert that value
does not match regular expression.
assert.notMatch('foobar', /^foo/, 'Regexp matches');
.property(object, property, [message])
Assert that object
has property. Can use dot-notation for deep reference.
assert.property({ tea: { green: 'matcha' }}, 'tea.green');
.notOwnProperty(object, property, [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])
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])
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])
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])
Compare two values using operator.
assert.operator(1, '<', 2, 'everything is ok');
assert.operator(1, '>', 2, 'this will fail');