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();
Unit.js has multiple interfaces that allow the developer to choose the most comfortable and productive style.
test.string(str)
.number(num).is(42);
test.assert(typeof str === 'string');
test.assert(typeof num === 'number');
test.assert.equal(num, 42);
test.should(str).be.a.String
test.should(num).be.Number
.and.equal(42);
test.must(str).be.a.string();
test.must(num).be.a.number();
test.must(num).equal(42);
Several helpers very useful for structuring large test suites.
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')
;
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.
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: