Home

Awesome

QUnit-TAP

A TAP Output Producer Plugin for QUnit

Build Status NPM version Dependency Status

DESCRIPTION

QUnit-TAP is a simple plugin for QUnit to produce TAP output.

QUnit-TAP provides TAP output feature for ANY version of QUnit (But there are some exceptions. See TESTED ENVIRONMENTS section below). With QUnit-TAP you can run your QUnit test scripts on your terminal, use TAP Consumers like prove for test automation, pass test output to Jenkins, and so on.

QUnit-TAP runs under headless browsers like PhantomJS, command-line js environments (like SpiderMonkey or Rhino), and CommonJS environments like Node.js, and of cource, runs on your real browser too.

CHANGELOG

CAUTION for Node.js users

TESTED ENVIRONMENTS

QUnit versionPhantomJSNode.jsRhino
1.0.0OKOKOK
1.1.0OKOKOK
1.2.0OKOKOK
1.3.0OKOKOK
1.4.0OKOKOK
1.5.0OKOKOK
1.6.0OKOKOK
1.7.0OKOKOK
1.8.0OKOKOK
1.9.0OKOKOK
1.10.0OKOKOK
1.11.0OKNGNG
1.12.0OKNGNG
1.13.0OKOKOK
1.14.0OKOKOK
1.15.0OKOKOK
1.16.0OKOKOK
1.17.0OKOKOK
1.17.1OKOKOK
1.18.0OKOKOK
1.19.0OKOKNG
1.20.0OKOKNG
1.21.0OKOKNG
1.22.0OKOKNG
1.23.0OKOKNG
1.23.1OKOKNG
2.0.0OKOKNG
2.0.1OKOKNG
2.1.0OKOKNG
2.1.1OKOKNG
2.2.0OKOKNG
2.2.1OKOKNG
2.3.0OKOKNG

DOWNLOAD

You can use QUnit-TAP,

USAGE

Simple steps to use QUnit-TAP.

  1. load/require qunit.js
  2. load/require qunit-tap.js
  3. Call qunitTap function with two or three arguments. The first argument is QUnit object reference, the second is print-like function for TAP output. And the third argument is object to customize default behavior. (Note that the first and second argument is mandatory, and the third argument is optional.)
  4. (optional) qunitTap function returns an object that represents QUnit-TAP API.

usage example 1 : embed QUnit-TAP in your HTML (e.g. to run with PhantomJS)

<script type="text/javascript" src="path/to/qunit.js"></script>
<script type="text/javascript" src="path/to/qunit-tap.js"></script>
<script>
  qunitTap(QUnit, function() { console.log.apply(console, arguments); });
</script>
<script type="text/javascript" src="path/to/your_test.js"></script>
<script type="text/javascript" src="path/to/your_test2.js"></script>

usage example 2 : use QUnit-TAP with Node.js

First, declare qunitjs and qunit-tap as devDependencies in your package.json, then run npm install.

{
    . . .
    "devDependencies": {
        "qunitjs": "2.3.0",
        "qunit-tap": "1.5.1",
        . . .
    },
    . . .
}

Next, require and configure them.

var QUnit = require('qunitjs');
var qunitTap = require('qunit-tap');
qunitTap(QUnit, function() { console.log.apply(console, arguments); });
QUnit.config.autorun = false;

// your tests

QUnit.load();

usage example 3 : use QUnit-TAP with Rhino/SpiderMonkey

load("path/to/qunit.js");
load("path/to/qunit-tap.js");

// enable TAP output
qunitTap(QUnit, print);  //NOTE: 'print' is Rhino/SpiderMonkey's built-in function

// or customize default behavior
// qunitTap(QUnit, print, {showExpectationOnFailure: true, showSourceOnFailure: false});

// configure QUnit to run under non-browser env.
QUnit.config.autorun = false;

load("path/to/your_test.js");
load("path/to/your_test2.js");

QUnit.load();

CONFIGURATION OPTIONS

QUnit-TAP is already configured with reasonable default. To customize, qunitTap function takes third optional argument as options object to override default behavior. Customization props are,

Related config option on QUnit

More on customization

You can even override moduleStart, testStart, log, done, testDone method in object returned from qunitTap function. In these methods, this refers to the object returned from qunitTap function.

var tap = qunitTap(QUnit, function() { console.log.apply(console, arguments); });
. . .
tap.moduleStart = function(arg) {
    // 'this' refers to tap object
    this.note('customized: ' + arg.name);
};

TAP OUTPUT EXAMPLE

QUnit-TAP produces output based on TAP specification.

# module: math module
# test: add
ok 1
ok 2
ok 3 - passing 3 args
ok 4 - just one arg
ok 5 - no args
not ok 6 - expected: 7, got: 1, test: add, module: math module
not ok 7 - with message, expected: 7, got: 1, test: add, module: math module
ok 8
ok 9 - with message
not ok 10 - test: add, module: math module
not ok 11 - with message, test: add, module: math module
# module: incr module
# test: increment
ok 12
ok 13
# module: TAP spec compliance
# test: Diagnostic lines
ok 14 - with\r
# multiline
# message
not ok 15 - with\r
# multiline
# message, expected: "foo\r
# bar", got: "foo
# bar", test: Diagnostic lines, module: TAP spec compliance
not ok 16 - with\r
# multiline
# message, expected: "foo
# bar", got: "foo\r
# bar", test: Diagnostic lines, module: TAP spec compliance
1..16

Configuration for this example is,

qunitTap(QUnit, function() { console.log.apply(console, arguments); }, {
  showSourceOnFailure: false
});

Explicitly, it's same as,

qunitTap(QUnit, function() { console.log.apply(console, arguments); }, {
  initialCount: 1,
  showExpectationOnFailure: true,
  showTestNameOnFailure: true,
  showModuleNameOnFailure: true,
  showSourceOnFailure: false
});

RUNNING EXAMPLES

prepare

$ git clone git://github.com/twada/qunit-tap.git
$ cd qunit-tap

to run with PhantomJS

# assume you have built and installed phantomjs
$ cd sample/js/
$ ./phantomjs_test.sh

# with prove
$ prove --exec=sh phantomjs_test.sh

for details, see phantomjs_test.sh

to run with Rhino/SpiderMonkey

# assume you are using rhino
$ cd sample/js/
$ java -jar /path/to/js.jar run_tests.js

for details, see sample/js/

to run under CommonJS environment (includes Node.js)

# assume you are using node.js
$ cd sample/commonjs/
$ node test/math_test.js
$ node test/incr_test.js

# with prove
$ prove --exec=/path/to/node test/*.js

for details, see sample/commonjs/

TROUBLE SHOOTING

qunitjs loading problem on Node

If you are using Node.js (or any CommonJS env) and have an error like this,

$ node test/incr_test.js 

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: should pass QUnit object reference. Please check QUnit's "require" path if you are using Node.js (or any CommonJS env).
    at qunitTap (/path/to/qunit-tap.js:22:15)
    at Object.<anonymous> (/path/to/using_qunit_via_require_module.js)
    ....
$

Check QUnit's version you are using. QUnit's module export path has changed since QUnit 1.3.0, so you should fix 'require' path to get QUnit Object from 'require' module.

  var util = require("util"),
-     QUnit = require('./path/to/qunit').QUnit,
+     QUnit = require('./path/to/qunit'),
      qunitTap = require('qunit-tap').qunitTap;
  qunitTap(QUnit, util.puts);
  QUnit.config.autorun = false;

Official QUnit npm module is available since QUnit version 1.9.0, so the best way to get QUnit Object is just use 'qunitjs' module.

  var util = require("util"),
-     QUnit = require('./path/to/qunit').QUnit,
+     QUnit = require('qunitjs'),
      qunitTap = require('qunit-tap').qunitTap;
  qunitTap(QUnit, util.puts);
  QUnit.config.autorun = false;

for details, see my fix.

qunit-tap loading problem on Node

Similarly, QUnit-TAP exports single qunitTap function as module.exports since 1.4.0. Therefore, require("qunit-tap") returns qunitTap function itself. Please fix your code if you are using Node.js (or any CommonJS env).

HOW TO RUN REGRESSION TESTS FOR DEVELOPMENT

to run version compatibility suite for PhantomJS

  1. download and install PhantomJS
  2. run ./test/version_compatibility_phantomjs.sh

to run version compatibility suite for Node.js

  1. install Node.js
  2. install test modules by npm install
  3. run ./test/version_compatibility_node.sh

to run version compatibility suite for Rhino

  1. run ./test/download_rhino.sh
  2. run ./test/version_compatibility_rhino.sh

AUTHOR

CONTRIBUTORS

LICENSE

Dual licensed under the MIT and GPLv2 licenses.