Mocha, Chai, Sinon

Mocha

// This example is using node.js built-in assertion module: <https://nodejs.org/api/assert.html>
mocha.setup('bdd');

describe('pow', function() {	
  beforeEach(() => {
		/* Setup Instruction. */
	});

  it("raises to n-th power", () => {
    assert.equal(pow(2, 3), 8);
  });
});

describe("test", function() {
  before(() => console.log("Testing started – before all tests"));
  after(() => console.log("Testing finished – after all tests"));

  beforeEach(() => console.log("Before a test – enter a test"));
  afterEach(() => console.log("After a test – exit a test"));

  it('test 1', () => console.log(1));
  it('test 2', () => console.log(2));
});

// Async
it('should save', function(done) {
  var user = new User();
  user.save(function(err) {
    if (err) {
			throw err;
		}
    done();
  });
});

Chai

chai.should();

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

const assert = chai.assert;
assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');
assert.lengthOf(foo, 3)
assert.property(tea, 'flavors');
assert.lengthOf(tea.flavors, 3);

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

Sinon

Fakes

const once = (fn) => {
  var returnValue,
    called = false;
  return () => {
    if (!called) {
      called = true;
      returnValue = fn.apply(this, arguments);
    }
    return returnValue;
  };
}
it("calls the original function", function () {
  const callback = sinon.fake();
  const proxy = once(callback);

	// The fact that the function was only called once is important:
  proxy();
	proxy();

  assert(callback.called);
	assert(callback.calledOnce);
  // ...or:
  // assert.equals(callback.callCount, 1);
});
it("calls original function with right this and args", function () {
  const callback = sinon.fake();
  const proxy = once(callback);
  const obj = {};

  proxy.call(obj, 1, 2, 3);

  assert(callback.calledOn(obj));
  assert(callback.calledWith(1, 2, 3));
});

Behavior