From 509296079605854520ded963ef4ba83f630bdc68 Mon Sep 17 00:00:00 2001 From: luckysw Date: Wed, 13 Aug 2014 15:58:36 +0200 Subject: [PATCH 1/4] - Allow empty code (e.g. Typescript tests with Typescript code dependencies) --- lib/child.js | 17 ++++++++++++----- lib/testrunner.js | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/child.js b/lib/child.js index c0dda7b..ca7cc50 100644 --- a/lib/child.js +++ b/lib/child.js @@ -12,9 +12,14 @@ var QUnit = require('qunitjs'), // http://GOESSNER.net/articles/JsonPath/ require('../support/json/cycle'); -var options = JSON.parse(process.argv[2]), - currentModule = path.basename(options.code.path, '.js'), - currentTest; +var options = JSON.parse(process.argv[2]), currentModule, currentTest; +if (!!options.code) { + currentModule = path.basename(options.code.path, '.js'); +} else { + // Script-based tests could contain code and test. In that case + // code can be not set, and the module name can be taken from the test name itself. + currentModule = ''; +} process.on('uncaughtException', function(err) { if (QUnit.config.current) { @@ -161,8 +166,10 @@ options.deps.forEach(function(dep) { _require(dep, true); }); -// require code -_require(options.code, true); +// require code, if one +if (!!options.code) { + _require(options.code, true); +} // require tests options.tests.forEach(function(test) { diff --git a/lib/testrunner.js b/lib/testrunner.js index f7a7621..23b3e23 100644 --- a/lib/testrunner.js +++ b/lib/testrunner.js @@ -73,7 +73,11 @@ function runOne(opts, callback) { } else if (msg.event === 'testDone') { log.add('tests', msg.data); } else if (msg.event === 'done') { - msg.data.code = opts.code.path; + if (!!opts.code) { + msg.data.code = opts.code.path; + } else{ + msg.data.code = ''; + } log.add('summaries', msg.data); if (opts.coverage) { coverage.add(msg.data.coverage); @@ -94,8 +98,8 @@ function runOne(opts, callback) { process.on('exit', kill); - if (opts.log.testing) { - console.log('\nTesting ', opts.code.path + ' ... '); + if (opts.log.testing && !!opts.code) { + util.print('\nTesting ', opts.code.path + ' ... '); } } @@ -105,6 +109,10 @@ function runOne(opts, callback) { * @return {Object} */ function absPath(file) { + if (!file) { + return undefined; + } + if (typeof file === 'string') { file = {path: file}; } @@ -152,7 +160,7 @@ exports.run = function(files, callback) { if (options.coverage || files[0].coverage) coverage.setup(options.coverage); files.forEach(function(file) { - var opts = _.extend({}, options, file); + var opts = _.extend({}, options, file); !opts.log && (opts.log = {}); opts.deps = absPaths(opts.deps); From c042f57d2ee828956cdd61d15a0a1e10038e8816 Mon Sep 17 00:00:00 2001 From: luckysw Date: Wed, 13 Aug 2014 16:22:13 +0200 Subject: [PATCH 2/4] - Introduced moduleDeps options, to allow module names in deps instead of file names --- lib/testrunner.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/testrunner.js b/lib/testrunner.js index 23b3e23..fbcd72c 100644 --- a/lib/testrunner.js +++ b/lib/testrunner.js @@ -41,9 +41,12 @@ options = exports.options = { // run test coverage tool coverage: false, - // define dependencies, which are required then before code + // define file dependencies, which are required then before code deps: null, + // define module dependencies, which are required then before code + moduleDeps: null, + // define namespace your code will be attached to on global['your namespace'] namespace: null }; @@ -167,6 +170,16 @@ exports.run = function(files, callback) { opts.code = absPath(opts.code); opts.tests = absPaths(opts.tests); + if (!!opts.moduleDeps) { + if (!Array.isArray(opts.moduleDeps)) { + opts.deps.push(opts.moduleDeps); + } else { + opts.moduleDeps.forEach(function(mod) { + opts.deps.push(mod); + }); + } + } + runOne(opts, function(err, stat) { if (err) { return callback(err, log.stats()); From d284154546774d5294e19a694c350906d6848460 Mon Sep 17 00:00:00 2001 From: luckysw Date: Wed, 13 Aug 2014 16:49:24 +0200 Subject: [PATCH 3/4] - Fixed moduleDeps implementation --- lib/child.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/child.js b/lib/child.js index ca7cc50..8ea0ca2 100644 --- a/lib/child.js +++ b/lib/child.js @@ -49,7 +49,13 @@ global.QUnit = QUnit; * @param {Object} res */ function _require(res, addToGlobal) { - var exports = require(res.path.replace(/\.js$/, '')); + var exports; + if (typeof res === 'string') { + exports = require(res); + } + else { + exports = require(res.path.replace(/\.js$/, '')); + } if (addToGlobal) { // resource can define 'namespace' to expose its exports as a named object From 915bf564c6c98dc6eedf73e76558dc66c5d3e405 Mon Sep 17 00:00:00 2001 From: luckysw Date: Wed, 13 Aug 2014 17:57:38 +0200 Subject: [PATCH 4/4] - Better error reporting --- lib/child.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/child.js b/lib/child.js index 8ea0ca2..8f9962b 100644 --- a/lib/child.js +++ b/lib/child.js @@ -22,6 +22,7 @@ if (!!options.code) { } process.on('uncaughtException', function(err) { + console.log(err); if (QUnit.config.current) { QUnit.ok(false, 'Test threw unexpected exception: ' + err.message); QUnit.start();