Asynchronous behaviour driven development for Node.
- A Suite is an object which contains zero or more batches, and can be executed or exported.
- A batch is an object literal, representing a structure of nested contexts.
- A context is an object with an optional topic, zero or more vows and zero or more sub-contexts.
- A topic is either a value or a function which can execute asynchronous code.
- A vow is a function which receives the topic as an argument, and runs assertions on it.
Batches are run in sequence, but contexts within a batch are run in parallel (taking dependencies into account).
- can be a static value
{ topic: 42,
'should be equal to 42': function(topic) {
assert.equal(topic, 42);
}
}
- can come from the return value of a function
{ topic: function() { return 42 },
'should be equal to 42': function(topic) {
assert.equal(topic, 42);
}
}
- has access to all the parent topics
{ topic: new(DataStore),
'should respond to `get()`': function(store) {
assert.isFunction(store.get);
},
'calling `get(42)`': {
topic: function(store) { return store.get(42) },
'should return the object with id 42': function(topic) {
assert.equal(topic.id, 42);
}
}
}
- can be asynchronous
{ topic: function() { fs.stat('~/FILE', this.callback); },
'can be accessed': function(err, stat) {
assert.isNull(err); // We have no error
assert.isObject(stat); // We have a stat object
},
'is not empty': function(err, stat) {
assert.isNotZero(stat.size); // The file size is > 0
}
}
Note: Beware the return value!
- can be a promise too
should is an expressive, readable, test framework agnostic, assertion library for node.
var user = { name: 'tj', pets: ['tobi', 'loki', 'jane', 'bandit'] };
user.should.have.property('name', 'tj');
user.should.have.property('pets').with.lengthOf(4)
someAsyncTask(foo, function(err, result) {
should.not.exist(err);
should.exist(result);
result.bar.should.equal(foo);
});
Has all the assertions you would want:
exist
, ok
, true
, false
, arguments
, empty
, eql
, equal
, within
, a
, instanceof
, above
, below
, match
, length
, string
, object
, property
, ownProperty
, contain
, keys
, respondTo
and you can also extend it easily.
Standalone test spies, stubs and mocks for JavaScript. No dependencies, works with any unit testing framework.
"test should call subscriber": function () {
var spy = sinon.spy();
PubSub.subscribe("message", spy);
PubSub.publishSync("message", undefined);
assertTrue(spy.called);
}
Sandboxes simplify working with fakes that need to be restored and/or verified.
"test using sinon.test sandbox": sinon.test(function () {
this.mock(API).expects("method").once();
PubSub.subscribe("message", API.method);
PubSub.publishSync("message", undefined);
})