Skip to content

Instantly share code, notes, and snippets.

@KayEss
Last active December 14, 2015 20:09
Show Gist options
  • Save KayEss/5142142 to your computer and use it in GitHub Desktop.
Save KayEss/5142142 to your computer and use it in GitHub Desktop.
Components for testing AngularJS controllers and some templates from the command line, using PhantomJS and over the file:// protocol so there's no need to run a web server for build bots etc.
(function() {
if (! jasmine) {
throw new Exception("jasmine library does not exist in global namespace!");
}
var ConsoleReporter = function() {
this.started = false;
this.finished = false;
};
ConsoleReporter.prototype = {
reportRunnerResults: function(runner) {
var dur = (new Date()).getTime() - this.start_time;
var failed = this.executed_specs - this.passed_specs;
var spec_str = this.executed_specs + (this.executed_specs === 1 ? " spec, " : " specs, ");
var fail_str = failed + (failed === 1 ? " failure in " : " failures in ");
this.log(spec_str + fail_str + (dur/1000) + "s.");
window.callPhantom(failed);
this.finished = true;
},
reportRunnerStarting: function(runner) {
this.started = true;
this.start_time = (new Date()).getTime();
this.executed_specs = 0;
this.passed_specs = 0;
},
reportSpecResults: function(spec) {
var spec_text = spec.suite.description + ' : ' + spec.description;
if (spec.results().passed()) {
this.passed_specs++;
} else {
spec_text += " FAILED";
this.log(spec_text);
}
},
reportSpecStarting: function(spec) {
this.executed_specs++;
},
reportSuiteResults: function(suite) {
var results = suite.results();
this.log(suite.description + ": " + results.passedCount + " of " + results.totalCount + " passed.");
},
log: function(str) {
var console = jasmine.getGlobal().console;
if (console && console.log) {
console.log("[test] " + str);
}
}
};
// export public
jasmine.ConsoleReporter = ConsoleReporter;
})();
var page = require('webpage').create();
var fs = require('fs');
var url = fs.absolute('test.html');
page.onConsoleMessage = function(msg) {
if ( msg.substr(0, 6) == "[test]") {
console.log(msg);
} else {
console.log("[console] " + msg);
}
};
page.onError = function(msg, trace) {
console.log("[error] " + msg);
};
page.onResourceRequested = function(request) {
console.log("[phantomjs] " + request.method + " " + request.url);
}
page.onCallback = function(failures) {
phantom.exit(failures ? 1 : 0);
}
console.log("[phantomjs] Loading page " + url);
page.open(url, function(status) {
if (status !== 'success') {
console.log("[phantomjs] Could not load!");
phantom.exit(2);
} else {
page.injectJs(fs.absolute('console-recorder.js'));
var results = page.evaluate(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.addReporter(new jasmine.ConsoleReporter());
jasmineEnv.execute();
});
}
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Spec Runner</title>
<link rel="shortcut icon" type="image/png" href="tes/lib/jasmine/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="test/lib/jasmine/jasmine.css">
<script type="text/javascript" src="test/lib/jasmine/jasmine.js"></script>
<script type="text/javascript" src="test/lib/jasmine/jasmine-html.js"></script>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="test/angular-mocks.js"></script>
<script type="text/javascript" src="test/lib/jasmine-jquery.js"></script>
<!-- include source files here... -->
<script type="text/javascript" src="controllers.js"></script>
<!-- include spec files here... -->
<script type="text/javascript" src="test/spec/jasmine-jquery.js"></script>
<script type="text/javascript" src="test/spec/hosts.js"></script>
<script type="text/javascript">
if ( !window.callPhantom ) {
(function() {
var jasmineEnv = jasmine.getEnv(), reporter = new jasmine.HtmlReporter();
jasmineEnv.updateInterval = 1000;
jasmineEnv.specFilter = function(spec) {
return reporter.specFilter(spec);
};
jasmineEnv.addReporter(reporter);
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
}
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment