-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the Js-unit-tests wiki!
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:
«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 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');
}