Created
May 5, 2016 21:01
-
-
Save munkyjunky/7c55bafa2f11ba4c72d79a5ef314127b to your computer and use it in GitHub Desktop.
Coverage collection for Nightwatch.js browser tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI I created an npm module based on this: https://github.com/aberonni/nightwatch-coverage