question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Coverage with Istanbul

See original GitHub issue

I’m currently implementing coverage reporting with Istanbul and have coverage being written on a per-test basis, using a custom command.

Is there a way to hook a function into a global afterEach hook that will run with the same browser context as the test ran with, so it would have access to the same window object in the browser (to access the coverage variable)?

Are there any plans on adding coverage reporting into Nightwatch directly?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
munkyjunkycommented, Apr 26, 2017

@KBPratap I did manage to get it working, but it’s a bit of a faff to get working.

First you need to have a version of your website with instrumented code. You then need to run nightwatch against this instrumented version of your site, and before calling .end() in your tests you need to get the coverage information out of the window and write it to a file. I did this by creating a custom command, .finish, which does this, then calls .end, and stores coverage information into a data store.

In the global after in nightwatch I then write out reports to the file system from the store of data.

This isn’t a great method, but works for me. It has some limitations though, as if you get a full page load at any point in your test then you lose coverage information from the previous page. This wasn’t an issue for my project, but may be for you.

I haven’t been working with nightwatch in any projects recently so can’t say if there’s a better way of doing it, bit this worked for me.

var fs = require('fs');
var store = require('path/to/coverage/store');
var yaml = require('yaml-js');

var config = '.istanbul.yml';

/**
 * Wrapper around browser.end, which gives us chance to write coverage details before
 * the browser session is closed without having to manually do it in each test.
 *
 * @param callback {function}
 * @returns {exports}
 */
exports.command = function(callback) {

	var browser = this;

	fs.access(config, fs.R_OK, function(err) {

		var options = err ? {} : yaml.load(fs.readFileSync(config));

		// 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){

			// Make sure something was returned - this will fail if browser.end() has been called
			if (response.status === 0 && response.value !== null) {
				store.addReport({
					name: browser.currentTest.module + '/' + browser.currentTest.name.toLowerCase().replace(/\W/gi,'-').replace(/\-+/g, '-'),
					data: response.value
				});
			}

			// End the browser session
			browser.end(callback);

		});

	});

	return this;

};
0reactions
aberonnicommented, Apr 14, 2020

FWIW I setup an example repo that does something similar to what is illustrated above https://github.com/aberonni/nightwatch-test-coverage-example

Read more comments on GitHub >

github_iconTop Results From Across the Web

Istanbul, a JavaScript test coverage tool.
JavaScript test coverage made simple. How Istanbul works. Istanbul instruments your ES5 and ES2015+ JavaScript code with line counters, so that you can...
Read more >
Measure your code coverage using Istanbul (with a demo)
Provides visual and informative reports. Provides ability to enforce minimum code coverage threshold. After some research, we found several good coverage tools ...
Read more >
Learn how to use the Istanbul JavaScript Code Coverage Tool
Istanbul gives us four code coverage metrics: Statements: How many of the statements in you code are executed. Branches: Conditional statements create branches ......
Read more >
Using Istanbul for Code Coverage in React | Pluralsight
Istanbul is a JavaScript code coverage tool that works by analyzing your codebase and providing you with the coverage insights you need in ......
Read more >
karma-coverage-istanbul-reporter - npm
A karma reporter that uses the latest istanbul 1.x APIs (with full sourcemap support) to report coverage. About. This is a reporter only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found