Introduction
In this blog we will learn about how we can use different assertion styles in Chai to test our units of code. Chai is an assertion library that helps us in BDD (Behaviour Driven Development) and TDD (Test Driven Development) for node applications. Chai can be paired with any javascript testing framework as well. Due to the fact that both BDD and TDD can be performed using Chai it works as one of the most comfortable assertion libraries for testing.
Chai provides certain interfaces to implement assertions for any Javascript based framework. These styles are known as assertion styles and further classified into BDD Style and TDD style.
Types of Assertion Styles
There are essentially three types of assertion styles: Assert, BDD, and Configuration that are used in testing environments.
Assert
The Assert style provides a classic assert-dot notation that can be used using the assert interface. This assert module provides several additional tests and is browser compatible.
var assert = require('chai').assert
, foo = 'bar'
, places = { city: [ 'delhi', 'mumbai', 'pune' ] };
assert.equal(foo, 'bar', 'foo equal `bar`');
assert.lengthOf(places.city, 3, places has 3 cities');We can include an error message at the scenario that the test does not pass and we need an error message.
BDD
BDD can be asserted in two methods, expect and should interfaces. These both can be used to naturally chain language assertions but the ways in which an assertion is initially constructed differs.
Expect
var expect = require('chai').expect
, foo = 'bar'
, places = { city: [ 'delhi', 'mumbai', 'pune' ] };
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(places).to.have.property('city').with.lengthOf(3);We can include arbitrary messages to prepend to any failed assertions.
var answer = 43;
// AssertionError: expected 43 to equal 42.
expect(answer).to.equal(42);
// AssertionError: topic [answer]: expected 43 to equal 42.
expect(answer, 'topic [answer]').to.equal(42);This comes in handy when being used with non-descript topics such as booleans or numbers.
Should
The should style is essentially similar to expect. The only difference being that it extends each object with a should property to start your chain.
var should = require('chai').should() //actually call the function
, foo = 'bar'
, places = { city: [ 'delhi', 'mumbai', 'pune' ] };
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.lengthOf(3);
places.should.have.property('city').with.lengthOf(3)Differences
In differences, we start with an expect interface as starting point for language assertions. The should interface provides a single getter as the starting point by extending Object.prototype.
var chai = require('chai')
, expect = chai.expect
, should = chai.should();A thing to notice would be the fact that the expect interface is just a reference to the expect function however, the should interface is executing the function.
Should Extras
In some scenarios should does not work in the way we want it to. For example, when we have to check the existence of an object created.
db.get(1234, function (err, doc) {
// we expect error to not exist
// we expect doc to exist and be an object
})Instead here we can use should.not.exist and should.exist.
var should = require('chai').should();
db.get(1234, function (err, doc) {
should.not.exist(err);
should.exist(doc);
doc.should.be.an('object');
});There are various other helpers for the should interface:
- should.exist
- should.not.exist
- should.equal
- should.not.equal
- should.Throw
- should.not.Throw
Configuration
Sometimes we need to configure how we want our interfaces to behave. This can be done using the config interface.
config.includeStack
This defines wheter the stack trace is inclded in the Assertion error message or not. The default value being false where the stack trace is not included.
chai.config.includeStack = true; // turn on stack traceconfig.showDiff
This user configurable property defines whether the showDiff flag should be included in the AssertionError. If we give the value as false it doesnt include the showDiff flag.
chai.config.showDiff = false; //turn off reporter diff displayconfig.truncateThreshold
This sets the length threshold for actual and expected values in assertion errors. If the threshold that is set here is exceeded the value is truncated.
In order to disable the truncating altogether we can set it to zero.
chai.config.truncateThreshold = 0; // disable truncating




