Testing with Mocha – 5. BDD style assertions with Chai
Tags: testingwithmocha, bdd, chai, javascript
Published: 2021-04-23 06:18:10

---
title: Testing with Mocha – 5. BDD style assertions with Chai
published: true
date: 2021-04-22 06:56:50 UTC
tags: TestingwithMocha,bdd,Chai,JavaScript
canonical_url: https://www.danielwerner.dev/testing-with-mocha-5-bdd-style-assertions-with-chai
---

Chai is a TDD and BDD library, and comes with several flavors for assertions: should, expect and assert. In the following lessons we will take a look at both styles and see how to use them in practice.

## Installation and first example

First we need to install chai using a package manager, for example npm:

```

npm install chai

```

When it is done, let’s take a look at our first example with chai.

See the Pen [Testing with Mocha – Example 5.1](https://codepen.io/daniel-werner-the-decoder/pen/abpQaBb) by Daniel Werner ([@daniel-werner-the-decoder](https://codepen.io/daniel-werner-the-decoder)) on [CodePen](https://codepen.io).

<script async="" src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>

As we see in this example we are using expect instead of assert, let’s see some more details about the different assertion styles, which can be used in Chai.

The BDD style assertions are `expect` and `should`. Both use the chainable getters to create nice and readable assertions.

The basic difference between expect and should is how they create the chain of expectations.

## Expect

Using expect you have to wrap the asserted value with and `expect()` function call, and create the chainable assertion afterwards. Let’s see an example.

See the Pen [Testing with Mocha – Example 5.1](https://codepen.io/daniel-werner-the-decoder/pen/abpQaBb) by Daniel Werner ([@daniel-werner-the-decoder](https://codepen.io/daniel-werner-the-decoder)) on [CodePen](https://codepen.io).

<script async="" src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>
## Should

Should provides the same chainable interface as expect, but should extends all the objects with a should property. This should property can be used to create the assert chain.

Let’s see the previous example rewritten with should.

See the Pen [Testing with Mocha – Example 5.2](https://codepen.io/daniel-werner-the-decoder/pen/NWdELgB) by Daniel Werner ([@daniel-werner-the-decoder](https://codepen.io/daniel-werner-the-decoder)) on [CodePen](https://codepen.io).

<script async="" src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>

Important to notice, that as opposed to expect should function should actually be called after requiring it.

```

const should = require('chai').should()

```

When using `should` with ES2015 style imports, the function call should go on separate line, like:

```

import chai from 'chai';
chai.should();

```

As opposed to equal, which only needs to be required/imported.

```

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

```

For more information about should and expect style assertions check the [Chai documentation](https://www.chaijs.com/guide/styles/).

* * *

_Check out all posts in the series_ **[Testing with Mocha](https://www.danielwerner.dev/category/testing-with-mocha/)**_._

The post [Testing with Mocha – 5. BDD style assertions with Chai](https://www.danielwerner.dev/testing-with-mocha-5-bdd-style-assertions-with-chai/) appeared first on [Daniel Werner](https://www.danielwerner.dev).