Unit testing framework for javascript

Powerful

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.

Installation

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.

Unit.js
test.string(str)
.number(num).is(42);
Assert of Node.js
test.assert(typeof str === 'string');
test.assert(typeof num === 'number');
test.assert.equal(num, 42);
Should.js
test.should(str).be.a.String
test.should(num).be.Number
.and.equal(42);
Must.js
test.must(str).be.a.string();
test.must(num).be.a.number();
test.must(num).equal(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(){
    test
      .object({foo: 'bar', other: 'other value', peace: 'love'})
        .hasValue('bar')
        .hasProperty('other', 'other value')
        .contains({peace: 'love'})
        .is({foo: 'bar', other: 'other value', peace: 'love'})

      .number(42)
        .isBetween(41, 43)
    ;
  });

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

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

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

    test
      .object(obj)
        .hasProperty('name')
        .hasProperty('message', 'hello')

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

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

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

          .object(obj)
            .isEmpty()

          .given(obj.name = 'expressive')

          // works also with:  case, when, given, 
          .then('In "then" blocks', function(){
            test.string(obj.name).startsWith('express');
          })
        ;
      })

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

      .string(obj.message)
        .startsWith('Hello')
        .endsWith('world !')
        .notContains('foobar')

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

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

      .then('Test the exception')

      .exception(trigger)
        .hasMessage('Whoops !')
        .hasMessage(/whoops/i)
        .isInstanceOf(Error)

      // or
      .error(trigger)
        .hasMessage('Whoops !')
        .hasMessage(/whoops/i)
    ;
  });
});

Result
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

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;

test.myPlugin(foo).myGreatFeature();
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 :