c8 icon indicating copy to clipboard operation
c8 copied to clipboard

Code coverage incorrectly reports 100% coverage when importing module with rewire

Open freebeans opened this issue 3 years ago • 4 comments

  • Version: v16.14.2
  • Platform: Linux 5.15.0-46-generic

Libraries:

  • AVA 5.0.1
  • rewire 6.0.0
  • c8 7.12.0

There seems to be a problem with the code coverage reported by c8 when the module under test is imported using rewire.

Rewire wraps the module with setters and getters so that private variables and functions can be stubbed.

I'm using AVA as the test runner.

My test file have no tests, I'm only importing AVA, rewire and my module:

test = require('ava')
rewire = require('rewire')

myModule = rewire('../lib/myModule')

Invoking c8 ava yields 100% coverage on the module.

If I simply require() the module instead, I get 10% or so (which is expected since the module does some initialization).

Swapping c8 by nyc yields the expected 10% coverage when using rewire, so it seems the instrumentation inside rewire messes up c8's.

I struggled to find anything related to this incompatibility. Are there any other reports on this issue?

freebeans avatar Nov 10 '22 16:11 freebeans

Feel free to edit the post or the title for clarification. I'm not a native speaker.

freebeans avatar Nov 10 '22 16:11 freebeans

See also #34, #325. I ended up converting my entire codebase to explicitly pass in dependencies I wanted to mock and giving up on things like rewire, esmock for this reason; they just don't work with c8.

jakebailey avatar Nov 11 '22 04:11 jakebailey

Is it worth it though? I'd rather keep the module code untouched.

I'm new to test driven development and I don't use any specific features from c8, so I'll be using nyc this time.

I'm affraid this might be my fault since I can't find many issues related. #34 feels like it might be related, but it's 4 years old now.

freebeans avatar Nov 11 '22 19:11 freebeans