tape-testing / tape

tap-producing test harness for node and browsers
MIT License
5.77k stars 307 forks source link

Code coverage options for Tape? #543

Closed cagross closed 3 years ago

cagross commented 3 years ago

Hello. In my project I’ve created many unit tests with Tape. I’m just now learning about the concept of code coverage, and would like to incorporate such reporting in my development. Does Tape offer a way to report on coverage? I believe the answer is ‘no,’ in-part because the devs want to keep Tape lightweight. If so, no worries—that’s a great reason. But if I wanted to obtain coverage information on my unit tests, what options would I have?

Possibilities

Thanks in advance.

ljharb commented 3 years ago

tape does not offer anything directly; however, it works great with https://npmjs.com/nyc - which then produces reports that codecov, coveralls, etc can consume. I use nyc + tape + codecov on about 250 repos.

cagross commented 3 years ago

OK thanks for that. I had a cursory look at nyc, and its integration with codecov. But I'm still a little unclear on what exactly nyc and codecov do. I think the setup works like this:

Is that a reasonably accurate top level assessment? If not, what am I misunderstanding?

Raynos commented 3 years ago

nyc node test/index.js produces code coverage output, it does this by instrumenting node, it's test framework agnostic.

nyc report --html can show you a HTML report on your laptop.

codecov is a tool that integrates with github & pull requests, it allows you see code coverage HTML report onlines without having to check out the branch yourself and run nyc to generate a report.

ljharb commented 3 years ago

It's not possible for coverage to be computed without instrumenting the environment - jest's coverage works in the same way - and it's a very intrusive thing that's tricky to get right. nyc does get it right as far as I'm able to tell, and by virtue of that is test-runner agnostic.

cagross commented 3 years ago

OK thanks to both of you. I now understand codecov's role in this. I am still a little unsure on what you mean by nyc instrumenting Node, and how it can then produce coverage output while being test runner agnostic. But that's probably because I'm just a novice :-) I will close this issue and go read up on that separately.

Raynos commented 3 years ago

nyc instruments a javascript program with counters for code coverage per statement, line and branch.

The idea of code coverage for a test suite is to just run a javascript program ( for example a test suite ) and then to see what code was covered or not.

In theory you can enable code coverage on your application, run your app, click around, close the app and look at the code coverage report of all the clicking you've done.

It's outputting coverage of your application code and you can either drive it manually or let your test suite drive it.

cagross commented 3 years ago

OK thanks for the discussion and explanations. I think I'm starting to get it.

In theory you can enable code coverage on your application, run your app, click around, close the app and look at the code coverage report of all the clicking you've done.

So in my case, what's happening is that nyc would do the following:

  1. enable code coverage on my application.
  2. execute all my Tape unit tests.
  3. generate a coverage report.

Am I understanding that correctly? If so, then cool--I believe I understand things now.