Jasmine is a Behavior Driven Development testing framework for JavaScript. It does not rely on browsers, DOM, or any JavaScript framework. Thus it's suited for websites, Node.js projects, or anywhere that JavaScript can run.
License: MIT
- Simple setup for node through jasmine-node
- Headless running out of the box
- Nice fluent syntax for assertions built-in, and does play pretty well with other assertion libraries
- Supported by many CI servers (TeamCity, Codeship, etc.) and some that don’t support natively have plugins (jenkins has a maven plugin)
- Descriptive syntax for BDD paradigm
- Very popular
- Everything comes bundled
- Asynchronous testing can be a bit of a headache
- Expects a specific suffix to all test files (spec.js by default)
- Everything comes bundled
- Sparse console output
describe("A suite is just a function", function() {
var a;
it("and so is a spec", function() {
a = true;
expect(a).toBe(true);
});
});
Standalone and test framework agnostic JavaScript test spies, stubs and mocks.
License: BSD
- Modular and focused only on spying, stubbing and mocking.
- No dependencies, works with any unit testing framework.
- Not a complete suite. Should be coupled with a test runner and assertion library.
// test spy
it("calls original function with right this and args", function () {
var callback = sinon.spy();
var proxy = once(callback);
var obj = {};
proxy.call(obj, 1, 2, 3);
assert(callback.calledOn(obj));
assert(callback.calledWith(1, 2, 3));
});
// test stub
it("returns the return value from the original function", function () {
var callback = sinon.stub().returns(42);
var proxy = once(callback);
assert.equals(proxy(), 42);
});
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.
License: MIT
- Very popular and active community. Over 3,000 dependent npm packages, 4.7M downloads last month.
- Simple setup
- Headless running out of the box
- Allows use of any assertion library that will throw exceptions on failure, such as Chai
- Supported by some CI servers and plugins for others (jenkins has a maven plugin)
- Has aliases for functions to be more BDD-oriented or TDD-oriented
- Highly extensible
- Asynchronous testing is a breeze
- Newer to the field, so support might be lacking in certain areas
- Runs all tests in one process. Shared resources -- no isolation.
var assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal(-1, [1,2,3].indexOf(4));
});
});
});
Concurrent JavaScript test runner
License: MIT
- Minimal and fast
- Simple test syntax
- Runs tests concurrently (or serially, if necessary)
- Enforces writing atomic tests
- No implicit globals
- Isolated environment for each test file
- Write your tests in ES2017
- Promise support
- Generator function support
- Async function support
- Observable support
- Enhanced assertion messages
- TAP reporter
- Clean stack traces
- Automatic migration from other test runners
- Global space bugs could possibly be hard to debug, but there will be a (much) better chance they're discovered.
- Since tests run in child processes, debugging tests requires a workaround:
node --inspect node_modules/ava/profile.js some/test/file.js
- Opinionated -- first test argument must be named "t"
- If you need to set up global state for each test (like spying on console.log for example), you'll need to make sure the tests are run serially
import test from 'ava';
test('foo', t => {
t.pass();
});
test('bar', async t => {
const bar = Promise.resolve('bar');
t.is(await bar, 'bar');
});
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.
License: MIT
- Popular -- over 2,400 dependent npm packages, 3.2M downloads last month.
- Includes many assertions that you'd think should be part of core node assert module.
- Can use should and/or expect for BDD style assertions
- Verbose, e.g.:
expect(func).to.have.been.called.with(arg)
- Spies can be made with
chai-spies
plugin, but no support for stubs
chai.should();
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.lengthOf(3);
tea.should.have.property('flavors')
.with.lengthOf(3);
var 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);
var assert = chai.assert;
assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');
assert.lengthOf(foo, 3)
assert.property(tea, 'flavors');
assert.lengthOf(tea.flavors, 3);