Mocha Hooks & Assertions

Setup/teardown hooks and assertion libraries.

Hooks

Before/After
before(function() {
  // runs once before all tests
});

after(function() {
  // runs once after all tests
});

beforeEach(function() {
  // runs before each test
});

afterEach(function() {
  // runs after each test
});

Chai Assertions

const { expect } = require("chai");

Expect syntax
expect(foo).to.be.a("string");
expect(foo).to.equal("bar");
expect(foo).to.have.lengthOf(3);
expect(tea).to.have.property("flavor");

Negation
expect(foo).to.not.equal("baz");

Chai BDD Chains

Language chains
expect(foo).to.be.true;
expect(foo).to.be.null;
expect(foo).to.be.undefined;
expect(foo).to.exist;

Numeric
expect(num).to.be.above(5);
expect(num).to.be.below(10);
expect(num).to.be.within(5, 10);

Arrays
expect(arr).to.include("item");
expect(arr).to.have.members([1, 2, 3]);

Should Syntax

const should = require("chai").should();

Should assertions
foo.should.be.a("string");
foo.should.equal("bar");
foo.should.have.lengthOf(3);

Chaining
foo.should.not.equal("bar");
obj.should.have.property("key").equal("value");