Skip to content

Instantly share code, notes, and snippets.

@munkyjunky
Created May 5, 2016 21:01
Show Gist options
  • Save munkyjunky/7c55bafa2f11ba4c72d79a5ef314127b to your computer and use it in GitHub Desktop.
Save munkyjunky/7c55bafa2f11ba4c72d79a5ef314127b to your computer and use it in GitHub Desktop.
Coverage collection for Nightwatch.js browser tests
/**
* Dependencies: js-yaml (for reading .istanbul.yml)
*
* Nightwatch coverage collection for browser testing, and writes the collected reports to the nightwatch
* reports folder. These reports can then be used in conjunction with istanbul report to generate coverage
* reports.
*/
var fs = require('fs');
var path = require('path');
var yaml = require('js-yaml');
var istanbulConfig = '.istanbul.yml';
module.exports = function(browser, done) {
fs.access(istanbulConfig, fs.F_OK, function(err){
var options = {};
// If there's no errors then read the file
if (err === null) {
options = yaml.load(fs.readFileSync(istanbulConfig));
}
// Make sure there's an instrumentation object
if (!options.instrumentation) {
options.instrumentation = {};
}
// Get the coverage from the browser
browser.execute(function(coverageVariable) {
return window[coverageVariable];
}, [ options.instrumentation.variable || '__coverage__' ], function(response){
var filePath = path.join(
'reports', //TODO: replace with nightwatch ouput_folder variable from config
browser.currentTest.module || '',
browser.currentTest.group || '',
browser.currentTest.name || ''
)
.replace(/\s/gi, '-') // normalise spaces
.replace(/[\\\/]$/, ''); // remove trailing slash
// Make sure something was returned - this will fail if browser.end() has been called
if (response.status === 0 && response.value !== null) {
// TODO: make folders if they don't exist
fs.writeFileSync(filePath + '.json', JSON.stringify(response.value));
}
done();
});
});
};
@aberonni
Copy link

FYI I created an npm module based on this: https://github.com/aberonni/nightwatch-coverage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment