diff --git a/src/test/testrunner.js b/src/test/testrunner.js index 7e3d54dc..0b032e81 100644 --- a/src/test/testrunner.js +++ b/src/test/testrunner.js @@ -1,48 +1,57 @@ -var - webpage = require('webpage'), - urls = [ - 'src/test/index.html' - ] -; +const webpage = require('webpage'); +const urls = ['src/test/index.html']; -(function test() {'use strict'; - var page, url = urls.shift(); - if (!url) return phantom.exit(0); - console.log('Loading: ' + url); - page = webpage.create(); - page.open(url, function (status) { - if (status === 'success') { - setTimeout(function () { - var results = page.evaluate(function() { - // remove the first node with the total from the following counts - var passed = Math.max(0, document.querySelectorAll('.pass').length - 1); - var resultHeader = document.querySelector('#wru strong'); - return { - // retrieve the total executed tests number - total: ''.concat( - passed, - ' tests (', - resultHeader ? resultHeader.textContent.replace(/\D/g, '') : 'no', - ' assertions)' - ), +async function runTests() { + for (const url of urls) { + console.log(`Loading: ${url}`); + const page = await new Promise((resolve, reject) => { + const page = webpage.create(); + page.onLoadFinished = status => { + if (status === 'success') resolve(page); + else reject(new Error(`Failed to load: ${url}`)); + }; + page.open(url); + }); + + try { + const results = await evaluateTestResults(page); + printTestResults(results); + if (results.passed === 0 || results.failed + results.errored > 0) { + console.error('Tests failed.'); + process.exit(1); + } + } catch (error) { + console.error(error); + process.exit(1); + } finally { + page.close(); + } + } + console.log('All tests completed successfully.'); + process.exit(0); +} + +async function evaluateTestResults(page) { + return await page.evaluate(() => { + const passed = Math.max(0, document.querySelectorAll('.pass').length - 1); + const resultHeader = document.querySelector('#wru strong'); + return { + total: `${passed} tests (${resultHeader ? resultHeader.textContent.replace(/\D/g, '') : 'no'} assertions)`, passed: passed, failed: Math.max(0, document.querySelectorAll('.fail').length - 1), errored: Math.max(0, document.querySelectorAll('.error').length - 1) - }; - }); - page.stop(); - page.close(); - console.log('- - - - - - - - - -'); - console.log('total: ' + results.total); - console.log('- - - - - - - - - -'); - console.log('passed: ' + results.passed); - console.log('failed: ' + results.failed); - console.log('errored: ' + results.errored); - console.log('- - - - - - - - - -'); - if (results.passed === 0 || 0 < results.failed + results.errored) { - phantom.exit(1); - } else test(); - }, 1000); - } else phantom.exit(1); - }); -}()); \ No newline at end of file + }; + }); +} + +function printTestResults(results) { + console.log('- - - - - - - - - -'); + console.log(`total: ${results.total}`); + console.log('- - - - - - - - - -'); + console.log(`passed: ${results.passed}`); + console.log(`failed: ${results.failed}`); + console.log(`errored: ${results.errored}`); + console.log('- - - - - - - - - -'); +} + +runTests();