falconzy's Blog

My Learning Path

Test-Driven Development in Node.js With Mocha

Reference

package.json devDependancies

devDependencies: {
  "chai": "*",
  "mocha": "*" // our preference, but you can use any test runner you like
}

Mocha structure

  • describe() is used to group together similar tests in Mocha
  • It() describes a specific use-case for the method being tested.
  • done() callback – Testing asynchronous code
  • before(), after(), beforeEach(), afterEach() – hooks to setup the test case
  • Pending tests – without a callback

chai BDD styles

API Reference

var expect = chai.expect;
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.length(3);
expect(tea).to.have.property('flavors')
    .with.length(3);