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.

Hi devs, maybe you can bring some light to this.

Is it possible to get code coverage as featured in chrome 59 in an understandable way (reporting lines used, unused, percentage)?

I tried to use the Profiler methods but I get function offsets(lines?) and counts that does not match if I use the coverage feature manually in Chrome.

I have some tests running in localhost:3000. The execution takes 10 seconds (I have a few), so is there a way to extend the recording of the coverage to 10 secs? using setTimeout?.

I’m lost , I’m not grasping this 😦

My code:

const CDP = require('chrome-remote-interface');

CDP((client) => {
  // extract domains
  const {
    Network,
    Page,
    Profiler
  } = client;
  // setup handlers
  Network.requestWillBeSent((params) => {
    console.log(params.request.url);
  });

  // enable events then start!
  Promise.all([
      Network.enable(),
      Page.enable(),
      Profiler.enable(),
      Profiler.startPreciseCoverage()
    ])
    .then(() => {
      return Page.navigate({
        url: 'http://localhost:3000'
      });
    })
    .then(() => Profiler.getBestEffortCoverage())
    .then((data) => data.result.forEach((el) => {
      console.log('-----', el.url)
      el.functions.forEach((el) => {
        console.log('--', el.functionName)
        console.log('ranges', el.ranges)
      })
    }))
    .catch((err) => {
      console.error(err);
      client.close();
    });
}).on('error', (err) => {
  // cannot connect to the remote endpoint
  console.error(err);
});

some output

-- myFunction
ranges [ { startOffset: 907, endOffset: 1490, count: 1 } ] // But it is a 3 line function and it is not executed!! 

All the best.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cyrus-andcommented, Jun 23, 2017

The scriptSource is:

"\n\nfunction foo(x) {\n    return Math.sin(x);\n}\n\nfunction bar() {\n    let x = 0.1;\n    for (i = 0; i \u003C 10000; i++) {\n        x = foo(x);\n    }\n    return x;\n}\n\nsetInterval(bar, 1000);\n\n"

The offsets are relative to this string, so for example:

> foo = scriptSource.slice(2, 45)
'function foo(x) {\n    return Math.sin(x);\n}'
> console.log(foo)
function foo(x) {
    return Math.sin(x);
}

You can roughly get the number of lines of the script (or of any range or function) with:

> scriptSource.split('\n').length
17

For what concerns count I’m a bit puzzled, it seems to be always 1 even though the function foo is called 10000 times per second… The documentation says:

Collected execution count of the source range.

But if you’re just interested in the coverage it doesn’t really matter, I guess. You may want to ask this in the Google Group.

Cheers! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code coverage - Wikipedia
In computer science, test coverage is a percentage measure of the degree to which the source code of a program is executed when...
Read more >
Everything you need to know about code coverage - Codegrip
Code coverage is a software testing metric that determines the number of lines of code that is successfully validated under a test procedure,...
Read more >
Code coverage testing - Visual Studio (Windows)
Code coverage is counted in blocks. Block is a part of code with exactly one entry and exit point. If the program's control...
Read more >
Codecov - The Leading Code Coverage Solution
Codecov is the leading, dedicated code coverage solution. Try Codecov for free now to help your developers find untested code and deploy changes...
Read more >
Code Coverage vs Test Coverage: A Detailed Guide
Code coverage is a white-box testing technique performed to verify the extent to which the code has been executed. Code coverage tools use ......
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