Unit testing framework for Javascript

Unit.js is an assertion library for Javascript, running on Node.js and the browser. It works with any test runner and unit testing framework like Mocha, Jasmine, Karma, protractor (E2E test framework for Angular apps), QUnit, ... and more.

Unit.js supports dependency injection and is extensible via a plugins system easy to use.


Philosophy of Unit.js:
Modern, flexible, simple and intuitive.
+ Unit.js (fluent style)
+ Assert of Node.js
+ Must.js
+ Should.js
+ Sinon.js
+ other powerful features

Unit.js was designed to provide the essential tools for writing unit tests with fun and qualities.

The learning curve to be productive with Unit.js is very short. The list of assertions is fully documented in the API doc and assertions are expressive like:
test.string('hello');
test.object(user).hasProperty('email');
test.array(fruit).hasValue('kiwi');
test.assert(myVar === true);
test.bool(myVar).isTrue();


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.

Easy unit tests

Easy to extend

Plugins
test.use(require('anyPlugin'));
Dependency injection
test.$di
  .set('hello', function(name) {
    return 'Hello ' + name;
  })
  .provider('logHello', 'hello', function(hello) {
    return test.dump(hello('World'));
  })
;

test
  .given(function() {
    this.hello('from "given()"');
  })

  .when(function() {
    this.hello('from "when()"');
  })

  .then(function() {
    this.hello('from "then()"');
  })

  .case(function() {
    this.hello('from "case()"');
  })

  .$di.get('logHello')
;
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.
BDD / TDD documentation generator

Spec doc generator

You can generate an API spec documentation of your application from your unit tests (output HTML, Markdown, JSON, ...).
For more information, see generate a spec documentation.

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