Permalink
Browse files

Run qunit tests via phantom in 'npm test' rather than .travis.yml.

We also throw a helpful error if phantom isn't found.
  • Loading branch information...
1 parent ecff01e commit fac471eda19113e58ccb3ed485f98e408b7db3a7 @toolness toolness committed Feb 19, 2013
Showing with 68 additions and 3 deletions.
  1. +0 −3 .travis.yml
  2. +8 −0 test/qunit.test.js
  3. 0 {static/test → test/utils}/phantom-qunit.js
  4. +60 −0 test/utils/qunit-tap.js
View
3 .travis.yml
@@ -13,8 +13,5 @@ before_script:
- mysql -e 'create database test_openbadges;'
- mysql -uroot -e "grant all on openbadges.* to 'badgemaker'@'localhost' identified by 'secret'"
- mysql -uroot -e "grant all on test_openbadges.* to 'badgemaker'@'localhost' identified by 'secret'"
- - node app.js &
script:
- npm test
- - phantomjs static/test/phantom-qunit.js http://localhost:8888/test/
- - phantomjs static/test/phantom-qunit.js http://localhost:8888/test/badge-accept/
View
8 test/qunit.test.js
@@ -0,0 +1,8 @@
+var test = require('tap').test;
+var qunitTap = require('./utils/qunit-tap');
+
+test('root qunit tests', qunitTap.testRunner('/test/'));
+
+test('badge-accept qunit tests', qunitTap.testRunner('/test/badge-accept/'));
+
+test('backpack qunit tests', qunitTap.testRunner('/test/backpack/'));
View
0 static/test/phantom-qunit.js → test/utils/phantom-qunit.js
File renamed without changes.
View
60 test/utils/qunit-tap.js
@@ -0,0 +1,60 @@
+var app = require('../../app'),
+ test = require('tap').test;
+ spawn = require('child_process').spawn;
+
+const PHANTOMJS = 'phantomjs' || process.env['PHANTOMJS'];
+const PORT = 8889;
+const PHANTOMJS_NOT_FOUND_ERR =
+ 'phantomjs not found. please install it from ' +
+ 'http://phantomjs.org/, or set the ' +
+ 'PHANTOMJS environment variable to its absolute path.';
+
+function log(msg) {
+ process.stderr.write(msg);
+}
+
+function loggedSpawn(command, args, options) {
+ var process = spawn(command, args, options);
+ process.stdout.setEncoding('utf8');
+ process.stderr.setEncoding('utf8');
+ process.stdout.on('data', log);
+ process.stderr.on('data', log);
+ return process;
+}
+
+function start(path, cb) {
+ app.listen(PORT, function() {
+ var url = 'http://localhost:' + PORT + path;
+ var args = [__dirname + '/phantom-qunit.js', url];
+ var cmdline = PHANTOMJS + ' ' + args.join(' ');
+ var phantom = loggedSpawn(PHANTOMJS, args);
+
+ log('running browser-based tests at ' + url + ' using phantomjs.');
+ phantom.on('exit', function(status) {
+ app.close();
+ if (status != 0)
+ throw new Error('process "' + cmdline + '" exited with status ' +
+ status);
+ if (cb) cb();
+ });
+ });
+}
+
+exports.runQunitTests = function(path, cb) {
+ if ('PHANTOMJS' in process.env)
+ start(path, cb);
+ else
+ spawn('which', [PHANTOMJS]).on('exit', function(status) {
+ if (status) throw new Error(PHANTOMJS_NOT_FOUND_ERR);
+ start(path, cb);
+ });
+};
+
+exports.testRunner = function(path) {
+ return function(t) {
+ exports.runQunitTests(path, function() {
+ t.ok(true, 'all qunit tests in ' + path + ' pass');
+ t.end();
+ });
+ };
+};

0 comments on commit fac471e

Please sign in to comment.