Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Last active December 12, 2015 09:29
Show Gist options
  • Save ishiduca/4752399 to your computer and use it in GitHub Desktop.
Save ishiduca/4752399 to your computer and use it in GitHub Desktop.
QUnit + qunit-tap のテスト環境

QUnit と qunit-tap でテスト環境

qunit-tap関係のドキュメント

QUnit関係のドキュメント

1. QUnit(qunitjs) と qunit-tap のインストール

    $ cd $HOME
    $ mkdir node_modules
    $ npm install [email protected]
    $ npm install qunit-tap

qunitjs (QUnit) は、v1.11.0 の場合、qunit-tap から使うと

    elem.attachEvent( "on" + type, fn );
     ^
TypeError: Object #<Object> has no method 'attachEvent'

のようにエラーが出るので使わない(2013.02.11 現在)

2. テストの準備

テストの前に qunit-tap を使うための設定(test_helper.js)を書く

$ mkdir myproject
$ cd myproject
$ vi test_helper.js

test_helper.js

var util     = require('util')
,   QUnit    = require('qunitjs')
,   qunitTap = require('qunit-tap').qunitTap
;

qunitTap(QUnit, util.puts);
QUnit.init();
QUnit.config.updateRate = 0;

module.exports.QUnit = QUnit;

3. テスト書く

proveコマンド利用することを考慮して、テストディレクトリは「t」にしておくと楽

$ mkdir t
$ vi t/hoge.js

t/hoge.js

var path = require('path')
,   QUnit = require(path.join(__dirname, '../test_helper')).Qunit
;

QUint.test('hoge', function () {
    QUnit.ok(1, 'exists ok');
});

4. テストする

4-1. $ node t/hoge.js

# test: hoge
ok 1 - exists ok
1..1

4-2. $ prove --ext=.js --exe=node

「t/」直下のテストを一括で行う場合、prove が楽

All tests successful.
Files=1, Tests=1,  0 wallclock secs ( 0.03 usr  0.01 sys +  0.07 cusr  0.01 csys =  0.12 CPU)
Result: PASS

別のディレクトリでテストするなど

$ prove --ext=.js --exe=node --lib test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment