UNPKG

travis-cov

Version:

Mocha/blanket coverage reporter for travis-ci.

122 lines (94 loc) 3.48 kB
/* borrowed from PhantomJS and modified for travis-cov */ /* * Qt+WebKit powered headless test runner using Phantomjs * * Phantomjs installation: http://code.google.com/p/phantomjs/wiki/BuildInstructions * * Run with: * phantomjs runner.js [url-of-your-qunit-testsuite] * * E.g. * phantomjs runner.js http://localhost/qunit/test */ /*jshint latedef:false */ /*global phantom:true require:true console:true */ var url = phantom.args[0], threshold = phantom.args[1], page = require('webpage').create(), DOMbound=false; // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this") page.onConsoleMessage = function(msg) { console.log(msg); }; page.onInitialized = function() { page.injectJs('travisCov.js'); page.evaluate(addLogging,threshold); }; page.open(url, function(status){ if (status !== "success") { console.log("Unable to access network: " + status); phantom.exit(1); } else { var interval = setInterval(function() { if (finished()) { clearInterval(interval); onfinishedTests(); } }, 500); } }); function finished() { return page.evaluate(function(){ return !!window.qunitDone; }); } function onfinishedTests() { var output = page.evaluate(function() { return JSON.stringify(window.qunitDone); }); phantom.exit(JSON.parse(output).failed > 0 ? 1 : 0); } function addLogging(threshold) { window.document.addEventListener( "DOMContentLoaded", function() { var current_test_assertions = [], DOMbound=true; QUnit.testDone(function(result) { var i, name = result.module + ': ' + result.name; if (result.failed) { console.log('Assertion Failed: ' + name); for (i = 0; i < current_test_assertions.length; i++) { console.log(' ' + current_test_assertions[i]); } } current_test_assertions = []; }); QUnit.log(function(details) { var response; if (details.result) { return; } response = details.message || ''; if (typeof details.expected !== 'undefined') { if (response) { response += ', '; } response += 'expected: ' + details.expected + ', but was: ' + details.actual; } current_test_assertions.push('Failed assertion: ' + response); }); QUnit.done(function(result){ if (result.passed === 0){ console.log("failed: no tests run."); result.failed=1; }else if (! window._$blanket ){ console.log("failed: no coverage info."); result = result.failed > 0 ? result : {failed: 1}; }else if ( !window.travisCov.check(window._$blanket,{threshold: threshold})){ console.log("failed:"+window._$blanket); result = {failed:1}; } window.qunitDone = result; }); }, false ); }