Table of contents
1.
Introduction
2.
Types of Assertion Styles
2.1.
Assert
2.2.
BDD
2.2.1.
Expect
2.2.2.
Should
2.2.3.
Differences
2.2.4.
Should Extras
2.3.
Configuration
2.3.1.
config.includeStack
2.3.2.
config.showDiff
2.3.3.
config.truncateThreshold
3.
FAQs
4.
Conclusion
Last Updated: Mar 27, 2024

Assertion Styles in Chai

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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');
You can also try this code with Online Javascript Compiler
Run Code

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);
You can also try this code with Online Javascript Compiler
Run Code

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);
You can also try this code with Online Javascript Compiler
Run Code

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)
You can also try this code with Online Javascript Compiler
Run Code

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();
You can also try this code with Online Javascript Compiler
Run Code

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
})
You can also try this code with Online Javascript Compiler
Run Code

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');
});
You can also try this code with Online Javascript Compiler
Run Code

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 trace
You can also try this code with Online Javascript Compiler
Run Code

config.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 display
You can also try this code with Online Javascript Compiler
Run Code

config.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
You can also try this code with Online Javascript Compiler
Run Code

FAQs

1. How can we combine multiple assertions?

Ans: A good way to combine multiple assertion is by using the Promise.all to make sure that all the assertions pass before we can move forward with the promise variables.
 

2. What is the advantage of using chai?

Ans: Chai provides functions and methods that can be used to assert the output of our code against the expected values. Also, chai is extremely readable making it useful as a documentation as well.

 

Conclusion

In this blog, we discussed the different assertion styles in chai.js and how to implement them using interfaces.

You may want to learn more about how to extend these assertions using plugin utilitiesWe hope that this blog has helped you enhance your knowledge regarding Assertion Styles in Chai. Do upvote our blog to help other ninjas grow.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Live masterclass