Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for direct requireJs dependency #99

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions lib/child.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ 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 = '<none>';
}

process.on('uncaughtException', function(err) {
console.log(err);
if (QUnit.config.current) {
QUnit.ok(false, 'Test threw unexpected exception: ' + err.message);
QUnit.start();
Expand Down Expand Up @@ -44,7 +50,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
Expand Down Expand Up @@ -161,8 +173,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) {
Expand Down
31 changes: 26 additions & 5 deletions lib/testrunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down Expand Up @@ -73,7 +76,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 = '<none>';
}
log.add('summaries', msg.data);
if (opts.coverage) {
coverage.add(msg.data.coverage);
Expand All @@ -94,8 +101,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 + ' ... ');
}
}

Expand All @@ -105,6 +112,10 @@ function runOne(opts, callback) {
* @return {Object}
*/
function absPath(file) {
if (!file) {
return undefined;
}

if (typeof file === 'string') {
file = {path: file};
}
Expand Down Expand Up @@ -152,13 +163,23 @@ 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);
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());
Expand Down