Unit testing framework for javascript


Simple, intuitive and flexible unit testing framework for javascript.

  • + Must.js
  • + Should.js
  • + Assert of Node.js
  • + Sinon.js
  • + fluent style like Atoum
  • + other friendly features.

= Unit.js

Unit.js is test runner and framework agnostic, by default Unit.js run with Mocha but works with any other runner.

Unit.js can be used as an assertion library or as a full stack unit testing framework with Mocha.


Unit.js is a Node.js module.
You can install it with NPM (Node Package Manager).

npm install unit.js
Getting started
var test = require('unit.js');

test.assert       // assert of Node.js
test.should       // Should.js
test.must         // Must.js
test.sinon        // Sinon.js
test.httpAgent    // supertest (testing HTTP servers)

test.mock         // Mocks (Sinon.js)
test.stub         // Stubs (Sinon.js)
test.spy          // Spy (Sinon.js)
test.fakeTimers   // Faking timers (Sinon.js)
test.dump         // dump the tested value

test.case         // expressive helper
test.given        // expressive helper
test.when         // expressive helper
test.then         // expressive helper

test.value        // value asserter
test.object       // object asserter
test.string       // string asserter
test.number       // number asserter
test.exception    // exception asserter
test.date         // date asserter

// ... and more assertions and features

Multiple assertions styles

Unit.js has multiple interfaces that allow the developer to choose the most comfortable and productive style.

Assert of Node.js
test.assert(typeof str === 'string');
test.assert(typeof num === 'number');
test.assert.equal(num, 42);

Flexible and expressive

Several helpers very useful for structuring large test suites.

var test = require('unit.js');

describe('Example', function(){

  it('Should be intuitive', function(){
      .object({foo: 'bar', other: 'other value', peace: 'love'})
        .hasProperty('other', 'other value')
        .contains({peace: 'love'})
        .is({foo: 'bar', other: 'other value', peace: 'love'})

        .isBetween(41, 43)

  it('Should be flexible and expressive', function(){

    var obj = {message: 'hello', name: 'Nico'};

    var trigger = function(){
      throw new Error('Whoops !');

        .hasProperty('message', 'hello')

      .when('Add "job" property', obj.job = 'developper')

        test.object(obj).hasProperty('job', 'developper');
      .case('Delete all properties', function() {

          .when(function() {
            delete obj.name;
            delete obj.message;
            delete obj.job;


          .given(obj.name = 'expressive')

          // works also with:  case, when, given, 
          .then('In "then" blocks', function(){

      .if(obj.message = 'Hello world !')
      .and(obj.name = 'say hello')

        .endsWith('world !')

      .given(obj = new Date(2014, 02))

        .isBefore(new Date(2020, 12))
        .isAfter(new Date(2010, 01))
        .isBetween(new Date(2014, 01), new Date(2014, 12))

      .then('Test the exception')

        .hasMessage('Whoops !')

      // or
        .hasMessage('Whoops !')

Unit.js is a unit testing framework intuitive and expressive

Free and open source unit testing framework

Open source

Unit.js is an open source project, the repository is hosted on GitHub.

You can use this program for both personal and commercial reasons. See the license.

Unit tests documentation


  • Unit.js Quickstart
  • All assertions are documented in the API doc.
  • Several tutorials in the guide section.
  • Full code examples in the spec doc.
Easy unit tests

Easy to extend and integrate

Easy to integrate
var test = require('unit.js');
Easy to extend
test.myPlugin = require('some-module');
test.myOtherPlugin = someFunction;

BDD / TDD documentation generator

Spec doc generator

You can generate an API spec documentation of your application from your unit tests (support HTML and markdown).
For more information, see generate a spec documentation.

Example, all spec of this documentation are generated with Unit.js and Mocha :