Skip to content

Commit

Permalink
added node runner for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gtanner committed Feb 14, 2012
1 parent e5917be commit 413590d
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 47 deletions.
5 changes: 4 additions & 1 deletion Jakefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ task('build', ['clean'], function () {
util.puts(fs.readFileSync("build/dalek", "utf-8"));
});

desc("runs the unit tests in node");
task('test', [], require('./test/runner').node);

desc("starts a webserver to point at to run the unit tests");
task('test', [], require('./test/runner'));
task('btest', [], require('./test/runner').browser);
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ properly.

# Testing

Tests run in the browser, and you can launch them with :
jake test
Tests run in node or the browser, and you can launch them with :

jake test

or to run in the browser:

jake btest


Coming soon, nodeJS running of tasks!

Expand Down
2 changes: 1 addition & 1 deletion configure
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
npm install -g jake
npm install connect
npm install connect jsdom
git submodule init
git submodule update
2 changes: 1 addition & 1 deletion lib/plugin/compass.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var exec = require('phonegap/exec'),

// Start watch timer to get headings
var id = utils.createUUID();
timers[id] = setInterval(function() {
timers[id] = window.setInterval(function() {
exec(successCallback, errorCallback, "Compass", "getHeading", []);
}, frequency);

Expand Down
8 changes: 3 additions & 5 deletions lib/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
delete modules[id];
};

if (!context.require) {
context.require = require;
context.define = define;
}
})(window);
context.require = require;
context.define = define;
})(this);
184 changes: 184 additions & 0 deletions test/reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
From the jasmine-node project: https://github.com/mhevery/jasmine-node
The MIT License
Copyright (c) 2010 Adam Abrons and Misko Hevery http://getangular.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

//
// Imports
//
var util;
try {
util = require('util')
} catch(e) {
util = require('sys')
}


//
// Helpers
//
function noop() {}

printRunnerResults = function(runner){
var results = runner.results();
var specs = runner.specs();
var msg = '';
msg += specs.length + ' test' + ((specs.length === 1) ? '' : 's') + ', ';
msg += results.totalCount + ' assertion' + ((results.totalCount === 1) ? '' : 's') + ', ';
msg += results.failedCount + ' failure' + ((results.failedCount === 1) ? '' : 's') + '\n';
return msg;
};

ANSIColors = {
pass: function() { return '\033[32m'; }, // Green
fail: function() { return '\033[31m'; }, // Red
neutral: function() { return '\033[0m'; } // Normal
};

NoColors = {
pass: function() { return ''; },
fail: function() { return ''; },
neutral: function() { return ''; }
};

//
// Reporter implementation
//
TerminalReporter = function(config) {
this.print_ = config.print || util.print;
this.isVerbose_ = config.verbose || false;
this.onComplete_ = config.onComplete || noop;
this.color_ = config.color? ANSIColors: NoColors;
this.stackFilter = config.stackFilter || function(t) { return t; }

this.columnCounter_ = 0;
this.log_ = [];
this.start_ = 0;
};

TerminalReporter.prototype = {
// Public Methods //
log: noop,

reportSpecStarting: noop,

reportRunnerStarting: function(runner) {
this.printLine_('Started');
this.start_ = Number(new Date);
},

reportSuiteResults: function(suite) {
var specResults = suite.results();
var path = [];
while(suite) {
path.unshift(suite.description);
suite = suite.parentSuite;
}
var description = path.join(' ');

if (this.isVerbose_)
this.log_.push('Spec ' + description);

outerThis = this;
specResults.items_.forEach(function(spec){
if (spec.description && spec.failedCount > 0) {
if (!outerThis.isVerbose_)
outerThis.log_.push(description);
outerThis.log_.push(' it ' + spec.description);
spec.items_.forEach(function(result){
if (!result.passed_) {
var errorMessage = result.trace.stack || result.message;
if(outerThis.teamcity_) {
outerThis.log_.push("##teamcity[testFailed name='" + escapeTeamcityString(spec.description) + "' message='[FAILED]' details='" + escapeTeamcityString(outerThis.stackFilter(outerThis.stackFilter(errorMessage))) + "']");
} else {
outerThis.log_.push(result.message.indexOf('timeout:') == 0 ?
' TIMEOUT:' + result.message.substr(8) :
' ' + outerThis.stackFilter(errorMessage) + '\n');
}
}
});
} else {
if (outerThis.isVerbose_) {
outerThis.log_.push(' it ' + spec.description);
}
}
});
},

reportSpecResults: function(spec) {
var result = spec.results();
var msg = '';
if (result.passed()) {
msg = this.stringWithColor_('.', this.color_.pass());
// } else if (result.skipped) { TODO: Research why "result.skipped" returns false when "xit" is called on a spec?
// msg = (colors) ? (ansi.yellow + '*' + ansi.none) : '*';
} else {
msg = this.stringWithColor_('F', this.color_.fail());
}
this.print_(msg);
if (this.columnCounter_++ < 50) return;
this.columnCounter_ = 0;
this.print_('\n');
},

reportRunnerResults: function(runner) {
var elapsed = (Number(new Date) - this.start_) / 1000;
var owner = this;

this.printLine_('\n');
this.log_.forEach(function(entry) {
owner.printLine_(entry);
});
this.printLine_('Finished in ' + elapsed + ' seconds');

var summary = printRunnerResults(runner);
if(runner.results().failedCount === 0 ) {
this.printLine_(this.stringWithColor_(summary, this.color_.pass()));
}
else {
this.printLine_(this.stringWithColor_(summary, this.color_.fail()));
}

this.onComplete_(runner, this.log_);
},

// Helper Methods //
stringWithColor_: function(str, color) {
return (color || this.color_.neutral()) + str + this.color_.neutral();
},

printLine_: function(str) {
this.print_(str);
this.print_('\n');
}

};


//
// Exports
//
exports.TerminalReporter = TerminalReporter;
112 changes: 75 additions & 37 deletions test/runner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var fs = require('fs'),
util = require('util'),
_path = require('path'),
packager = require('../build/packager'),
exec = require('child_process').exec;

function collect(path, files, matches) {
Expand All @@ -16,42 +18,78 @@ function collect(path, files, matches) {
}
}

module.exports = function () {
var connect = require('connect'),
packager = require('../build/packager'),
tests = [],
html = fs.readFileSync(__dirname + "/suite.html", "utf-8"),
doc,
modules,
specs,
app = connect(
connect.static(__dirname + "/../lib/"),
connect.static(__dirname + "/../"),
connect.static(__dirname),
connect.router(function (app) {
app.get('/', function (req, res) {
res.writeHead(200, {
"Cache-Control": "no-cache",
"Content-Type": "text/html"
module.exports = {
node: function () {
var jas = require("../thirdparty/jasmine/jasmine"),
jasmine = jas.jasmine,
TerminalReporter = require('./reporter').TerminalReporter,
tests = [],
jsdom = require("jsdom").jsdom,
document = jsdom("<html>"),
window = document.createWindow();

//Put jasmine in scope
Object.keys(jas).forEach(function (key) {
this[key] = window[key] = global[key] = jas[key];
});

//load in our modules
console.log(this);
eval(packager.modules('test'));
console.log(this);
//hijack require
require = window.require;
define = window.define;

//load in our tests
collect(__dirname, tests);
for (var x in tests) {
eval(fs.readFileSync(tests[x], "utf-8"));
}

var env = jasmine.getEnv();
env.addReporter(new TerminalReporter({
color: true,
onComplete: process.exit
}));
env.execute();
},
browser: function () {
var connect = require('connect'),
tests = [],
html = fs.readFileSync(__dirname + "/suite.html", "utf-8"),
doc,
modules,
specs,
app = connect(
connect.static(__dirname + "/../lib/"),
connect.static(__dirname + "/../"),
connect.static(__dirname),
connect.router(function (app) {
app.get('/', function (req, res) {
res.writeHead(200, {
"Cache-Control": "no-cache",
"Content-Type": "text/html"
});
tests = [];
collect(__dirname, tests);

specs = tests.map(function (file, path) {
return '<script src="' + file.replace(/^.*test/, "test") +
'" type="text/javascript" charset="utf-8"></script>';
}).join('');
modules = packager.modules('test');
doc = html.replace(/<!-- TESTS -->/g, specs).replace(/"##MODULES##"/g, modules);
res.end(doc);
});
tests = [];
collect(__dirname, tests);

specs = tests.map(function (file, path) {
return '<script src="' + file.replace(/^.*test/, "test") +
'" type="text/javascript" charset="utf-8"></script>';
}).join('');
modules = packager.modules('test');
doc = html.replace(/<!-- TESTS -->/g, specs).replace(/"##MODULES##"/g, modules);
res.end(doc);
});
})
);

app.listen(3000);

process.stdout.write("Test Server running on:\n");
process.stdout.write("http://127.0.0.1:3000\n");

exec('open http://127.0.0.1:3000');
})
);

app.listen(3000);

process.stdout.write("Test Server running on:\n");
process.stdout.write("http://127.0.0.1:3000\n");

exec('open http://127.0.0.1:3000');
}
};
15 changes: 15 additions & 0 deletions test/test.contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
describe("Contact", function () {
var Contact = require('phonegap/plugin/Contact'),
exec = require('phonegap/exec');

describe("save", function () {
it("calls exec when saving", function () {
var c = new Contact(),
s = jasmine.createSpy(),
e = jasmine.createSpy();

c.save(s, e);
expect(exec).toHaveBeenCalledWith(s, e, "Contacts", "save", [c]);
});
});
});

0 comments on commit 413590d

Please sign in to comment.