JS (Node) Test Frameworks
- Built into node.js
assert.fail(actual, expected, message, operator) assert.ok(value, [message]) assert.equal(actual, expected, [message]) assert.doesNotThrow(block, [error], [message]) assert.ifError(value)
- browser support
- test coverage reporting
- simple async support
- times tests & highlights slow tests
- global variable leak reporting
- before, after, before each, after each hooks
- use any assertion library you want
- run tests matching regex
Synchronous
describe('Array', function(){ describe('#indexOf()', function(){ it('should return -1 when the value is not present', function(){ [1,2,3].indexOf(5).should.equal(-1); [1,2,3].indexOf(0).should.equal(-1); }) }) })
Async
describe('User', function(){ describe('#save()', function(){ it('should save without error', function(done){ var user = new User('Luna'); user.save(function(err){ if (err) throw err; done(); }); }) }) })
- By TJ Holowaychuk (author of Express web framework)
- async support
- test coverage support using node-jscoverage
assert.isNull(null); assert.isNotNull(false); assert.isDefined(false); assert.isUndefined(undefined); assert.length('foo', 3); assert.includes([1,2,3], 3);
Define tests by exporting a function
exports['test String#length'] = function(){ assert.equal(6, 'foobar'.length); };
For large numbers of tests you can export a “suite”
module.exports = { 'test String#length': function(beforeExit, assert) { assert.equal(6, 'foobar'.length); } };
Async exports
setTimeout(function() { exports['test async exports'] = function(){ assert.ok('wahoo'); }; }, 100);
- Runs in browser or node
- async support
- setup & teardown functions
- flexible reporters HTML & XUnit style output
- allows use of mocks and stubs
exports.testSomething = function(test){ test.expect(1); test.ok(true, "this assertion should pass"); test.done(); }; exports.testSomethingElse = function(test){ test.ok(false, "this assertion should fail"); test.done(); };
- BDD Framework
- can be run in browser, or node
- Ruby Gem for rails testing
- From Pivotal Labs (makers of Pivotal Tracker)
describe("Jasmine", function() { it("makes testing JavaScript awesome!", function() { expect(yourCode).toBeLotsBetter(); }); });
- async BDD framework for node.js
- executes tests in parallel
- executes tests sequentially when there are dependencies
- emphasis on speed of execution, clarity, and user experience
var vows = require('vows'), assert = require('assert'); // Create a Test Suite vows.describe('Division by Zero').addBatch({ 'when dividing a number by zero': { topic: function () { return 42 / 0 }, 'we get Infinity': function (topic) { assert.equal (topic, Infinity); } }, 'but when dividing zero by zero': { topic: function () { return 0 / 0 }, 'we get a value which': { 'is not a number': function (topic) { assert.isNaN (topic); }, 'is not equal to itself': function (topic) { assert.notEqual (topic, topic); } } } }).run(); // Run it
- A node.js cucumber implementation
- a few extra asynchronous keywords.
- supports 160+ languages
- exports to VowsJS stub
- From Rackspace
- text, html, json reporters (no xunit?)
- support for hooking up the coverage for services started with the process runner
- support for aggregating code coverage reports
- code coverage
- test isolation — each test runs in a separate process
- needs unix sockets?
- process runner — keeps track of integration test dependencies via json config file
should
Assertion framework
</pre> 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); });
Gently
for mocking
See also this list:
https://github.com/joyent/node/wiki/modules#testing