What are the different assertion styles in chai.js?

chai.js is a BDDBehavior Driven Development / TDDTest Driven Development assertion library for Node.js that can be used with any testing framework. It primarily provides two assertion-style APIs: the Assert API and the BDD API.

The Assert API

The Assert API is an alternative to the built-in assert module in Node.js; however, it provides additional tests and is browser compatible. The API has two distinct usage syntaxes: the expression syntax and the usage syntax. The former evaluates the provided expression to verify its truthiness, whereas the latter expects objects to be evaluated based on the assertion method.

Expression Syntax

assert(expression, message);
  • expression: The expression that is to be tested for truthiness.

  • message: The message to be displayed in case of an error.

Example

const assert = require('chai').assert;
try {
assert(10 == 12, 'values are not equal');
} catch (err) {
console.log(err);
}

In the snippet above, the assertion expression is 10 == 12 that turns out to be false, therefore, an AssertionError is thrown with the specified message.

Method Syntax

assert.method(...args);
  • method: The assertion method to be used.

  • ...args: Arguments required by the assertion method usually the object(s) over which the assertion is to be applied.

Example

The following snippet utilizes the equal assertion method to reproduce the assertion test provided in the previous snippet:

const assert = require('chai').assert;
try {
assert.equal(10, 12, 'values are not equal');
} catch (err) {
console.log(err);
}

Similar to the first example, this snippet also equates 10 and 12 and throws the AssertionError with the specified message but also a bit more detail.

Note: To view more methods of the Assert API, visit this link.

The BDD API

On the other hand, the BDD API provides a chainable syntax for assertion tests via two interfaces; expect and should. Both interfaces allow assertions to be written in natural language; however, the former is a method, whereas the latter is a property added to all objects by extending Object.prototype.

The expect method

The expect method takes an assertion object as an argument over which the chainable assertions are applied.

Syntax

expect(obj, [message]).language.chain.assertionMethod(...args);
  • obj: The object over which the assertion is to be applied.

  • message (Optional): The message to be displayed in case of an error.

  • language.chain.: The language chain that makes the assertion user-friendly.

  • assertionMethod: The assertion method to be used.

  • ...args: Arguments required by the assertionMethod.

Example

const expect = require('chai').expect;
try {
str = 'Educative'
obj = { attr: 'Answer' };
expect(str).to.be.a('string');
expect(obj).to.have.property('attr').that.is.a('integer');
} catch (err) {
console.log(err);
}

The above snippet applies the expect method to a string str and an object obj. The first assertion doesn't raise an error since str is a string, whereas the second assertion fails since obj.attr is not an integer.

The should property

To extend the Object.prototype to include the should property in virtually every object; the should method of the chai the module is invoked at the start of the script. Then, on any object in the script, the should property followed by an assertion chain can be specified.

Syntax

obj.should.language.chain.assertionMethod(...args)
  • obj: The object over which the assertion is to be applied.

  • language.chain.: The language chain that makes the assertion user-friendly.

  • assertionMethod: The assertion method to be used.

  • ...args: Arguments required by the assertionMethod.

Example

The snippet below implements the previous example using the should property.

require('chai').should();
try {
str = 'Educative'
obj = { attr: 'Answer' };
str.should.be.a('string');
obj.should.have.property('attr').that.is.a('integer');
} catch (err) {
console.log(err);
}

Similar to the first example, the assertion passes in the case of str. however, fails for the obj as the attr is a string, not an integer.

Note: To view more methods of the BDD API, visit this link.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved