tests inside of one testfile run synchronous, but every testfile runs parallel
tests from each file run in its own spawned node process
same API for client and server side code (original QUnit is used)
the simplest API of the world, especially for asynchronous testing
you can write tests in TDD or BDD style depending on your task and test type
you can run the same tests in browser if there is no dependencies to node
generators support
Installation
$ npm i qunit
API
http://api.qunitjs.com
The only exception
// Separate tests into modules.// Use `QUnit` namespace, because `module` is reserved for node.QUnit.module(name, lifecycle)
Usage
Command line
Read full cli api doc using "--help" or "-h":
$ qunit -h$ qunit -c ./code.js -t ./tests.js
By default, code and dependencies are added to the global scope. To specify
requiring them into a namespace object, prefix the path or module name with the
variable name to be used for the namespace object, followed by a colon:
var testrunner = require("qunit");// Defaults:{ // logging options log: { // log assertions overview assertions: true, // log expected and actual values for failed tests errors: true, // log tests overview tests: true, // log summary summary: true, // log global summary (all files) globalSummary: true, // log coverage coverage: true, // log global coverage (all files)
// change any option for all tests globallytestrunner.options.optionName = value;// or use setup functiontestrunner.setup({ log: { summary: true }});// one code and tests filetestrunner.run({ code: "/path/to/your/code.js", tests: "/path/to/your/tests.js"}, callback);// require code into a namespace object, rather than globallytestrunner.run({ code: {path: "/path/to/your/code.js", namespace: "code"}, tests: "/path/to/your/tests.js"}, callback);// one code and multiple tests filetestrunner.run({ code:
Writing tests
QUnit API and code which have to be tested are already loaded and attached to the global context.
Some tests examples
test("a basic test example", function () { ok(true, "this test is fine"); var value = "hello"; equal("hello", value, "We expect value to be hello");});QUnit.module("Module A");test("first test within module", 1, function () { ok(true, "a dummy");});test("second test within module", 2, function () { ok(true, "dummy 1 of 2"
Generators support
test("my async test with generators", function* () { var data = yield asyncFn(); equal(data, {a: 1}, 'generators work');});
Run tests
$ npm i$ npm test
Coverage
Code coverage via Istanbul.
To utilize, install istanbul and set option coverage: true or give a path where to store report coverage: {dir: "coverage/path"} or pass --cov parameter in the shell.
To specify the format of coverage report pass reporters array to the coverage options: coverage: {reporters: ['lcov', 'json']} (default)
Coverage calculations based on code and tests passed to node-qunit.