Skip to content
Luis Filipe Magalhaes Pimenta edited this page Feb 2, 2017 · 16 revisions

Welcome to the Js-unit-tests wiki!

Mocha

The BDD style interface provides many global functions like:

  • describe()
  • it()
  • before()
  • after()
  • beforeEach()
  • afterEach()

And they just provide a way to keep tests easier to read and organized.

Each test must have basic blocks and could have the other blocks. The main blocks are:

  1. Basic Blocks
  2. Non Basic Blocks

Chai - assert

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

We will use only the assert part of this library because it is more similar to phpUnit asserts ;)

In order to use that import it like this:

var assert = require('chai').assert;
var foo = 'bar';
assert.equal(foo, 'bar', 'foo equal `bar`');

Documentation on chai site

Sinon

Sinon is a very powerfull library that allows us to make many things, one of those are spies.

Require sinon like this:

var sinon = require('sinon');

«A test spy is a function that records arguments, return value, the value of this and exception thrown (if any) for all its calls. A test spy can be an anonymous function or it can wrap an existing function.»

Example:

"test should call subscribers with message as first argument" : function () {
    var message = "an example message";
    var callBackSpy = sinon.spy();

    PubSub.subscribe(message, callBackSpy);
    PubSub.publishSync(message, "some payload");

    assert(callBackSpy.calledOnce);
    assert(callBackSpy.calledWith(message), 'Incorrect call args');
}

Clone this wiki locally