Skip to content

Testing Chai

Sribalaji M edited this page Jan 31, 2018 · 4 revisions

Chai Assertion Library

Chai is a BDD / TDD assertion library for node and the browser that can be paired with any javascript testing framework.

Installation

You can install Chai as dev-dependency using npm -

npm install chai --save-dev

Assertion Styles

Assert

The assert style is exposed through assert interface. This provides the classic assert-dot notation, similar to that packaged with node.js. This assert module, however, provides several additional tests

const {assert} = require('chai')
  , foo = 'bar'
  , beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };

assert.typeOf(foo, 'string'); // without optional message
assert.typeOf(foo, 'string', 'foo is a string'); // with optional message
assert.equal(foo, 'bar', 'foo equal `bar`');
assert.lengthOf(foo, 3, 'foo`s value has a length of 3');
assert.lengthOf(beverages.tea, 3, 'beverages has 3 types of tea');

BDD

The BDD style comes in two flavors: expect and should. Both use the same chainable language to construct assertions, but they differ in the way an assertion is initially constructed.

Expect

The BDD style is exposed through expect or should interfaces. In both scenarios, you chain together natural language assertions.

const {expect} = require('chai')
  , foo = 'bar'
  , beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };

expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(beverages).to.have.property('tea').with.lengthOf(3);

Should

The should style allows for the same chainable assertions as the expect interface, however it extends each object with a should property to start your chain.

const should = require('chai').should() //actually call the function
  , foo = 'bar'
  , beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };

foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.lengthOf(3);
beverages.should.have.property('tea').with.lengthOf(3);

Note

Note that the expect require is just a reference to the expect function, whereas with the should require, the function is being executed.

const chai = require('chai')
  , expect = chai.expect
  , should = chai.should();

Preferred Approach

We strongly prefer the use of BDD style should as it makes the code more readable.

Chai Assertions for Promises

Chai as Promised extends Chai with a fluent language for asserting facts about promises.

Installation

npm install chai-as-promised --save-dev

Usage

const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
 
chai.use(chaiAsPromised);

chai.should();

Instead of manually wiring up your expectations to a promise's fulfilled and rejected handlers:

doSomethingAsync().then(
    function (result) {
        result.should.equal("foo");
        done();
    },
    function (err) {
       done(err);
    }
);

you can write code that expresses what you really mean:

return doSomethingAsync().should.eventually.equal("foo");

For Example, suppose we want to make an api call to an endpoint and examine the response to be as expected,

This is hows its done without chai-as-promised:

it('should get api/endpoint', (done) => {
  fetch('http://localhost:3000/api/endpoint').then(
     (response) => {
        response.should.equal(myResObj);
        done();
     });
});

But with chai-as-promised, this can be greatly simplified and improve readability:

it('should get api/endpoint', () => {
  fetch('http://localhost:3000/api/endpoint').should.eventually.equal(myResObj);
});

References

Clone this wiki locally