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.

Injecting code/comments after transformation - c8 coverage fails

See original GitHub issue

Thanks for all your work on sucrase. It’s an amazing project.

I’m a big fan of c8 for code coverage checking, and it works almost perfectly with sucrase.

Unfortunately, when you are importing other modules/files, sucrase introduces some branching logic, when it inserts this line:

"use strict";const _jsxFileName = "src/index.tsx"; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _react = require('react'); var _react2 = _interopRequireDefault(_react);

Note the obj.__esModule ? obj : { default: obj } is the specific part that fails c8

This results in my code coverage claiming line 1 has untested branching logic.

If I put the following hack in, it works perfectly:

This is no longer working, see this comment below

diff --git a/src/index.ts b/src/index.ts
index feb4362..1e2329b 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -54,6 +54,9 @@ export function transform(code: string, options: Options): TransformResult {
         sourceMap: computeSourceMap(result.code, options.filePath, options.sourceMapOptions),
       };
     }
+
+    result.code = `/* c8 ignore next */\n${result.code}`;
+
     return result;
   } catch (e) {
     if (options.filePath) {

But obviously this isn’t a great solution. Do you have any recommendations on how we could:

  1. Remove that branch logic or the _interopRequireDefault entirely
  2. Or, allowing adding custom prepends to transformation results. e.g.:
require("sucrase/dist/register").registerAll({
  prependCode: `/* c8 ignore next */`
})

Example

My ReactFast project shows the problem if you run npm run test. You’ll see a coverage report claiming line 1 is uncovered.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
alangpiercecommented, Oct 18, 2021

Hey @markwylde , thanks for the report. I think ideally Sucrase would integrate well with other tools by default, so I’d certainly be open to including /* c8 ignore next */ comments by default (except with production: true) if they make c8 coverage more accurate.

I tried playing around with it myself (thanks for sharing an example repo!) and had some trouble narrowing it down. I tried changing the output code like you did, and I also tried changes to https://github.com/alangpierce/sucrase/blob/main/src/HelperManager.ts where the helpers are defined. A few observations:

  • I couldn’t figure out a place to put /* c8 ignore next */ or /* c8 ignore start */ in the interopRequireDefault helper code to avoid the uncovered line, but maybe there’s a way to make it work.
  • Adding just a newline (no /* c8 ignore next */) to the start of the file seemed to make the problem go away.
  • Removing the snippet .replace(/\s+/g, " ") (which removes newlines and spaces for helpers, making them one long line) seemed to make the problem go away.
  • Editing App.tsx to have a blank line at the top seemed to make the problem go away.

I’m a bit confused by those last three behaviors since it looks like AST-equivalent files are giving different results. I’m now wondering if this is a c8 bug or a situation where c8 decides to give up on lines that are too long, or something like that.

In any case, one concern with adding a newline at the start of the code is that Sucrase guarantees that line numbers are the same before and after transpile (e.g. so stack traces are accurate even without source maps). Adding extra newlines to the top of the file would break that guarantee.

If you wanted to track down the problem a bit more and figure out a concrete code change, I’d probably be happy to accept a PR that makes Sucrase more c8-friendly. I probably won’t have time to dig much into this issue much myself, though.

As a separate note, if you’re trying to work around this in a more immediate timeframe, I think your best option is to write your own version of the addHook function from https://github.com/alangpierce/sucrase/blob/main/src/register.ts and then include that file with -r. (Your version will probably need to be written in plain JS.) It’s just ~15 lines, and you can modify transformedCode there if you want without needing any changes in Sucrase.

0reactions
fdc-viktor-luftcommented, Nov 15, 2022

I tested it again with

  • "@sucrase/jest-plugin": "^3.0.0"
  • "sucrase": "^3.28.0"

The issue doesn’t appear for me anymore 🙃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Injecting code/comments after transformation - c8 coverage fails
I'm a big fan of c8 for code coverage checking, and it works almost perfectly with sucrase. Unfortunately, when you are importing other...
Read more >
Find out how much code you're testing
The code coverage percentages let you estimate how much of your code is tested. If your team decides on a set minimum amount...
Read more >
Coverage | Guide
Vitest supports Native code coverage via c8 and instrumented code coverage via istanbul . Coverage Providers #. TIP. Since Vitest v0.22.0. Both ...
Read more >
Configuring Jest
For example, with the following configuration jest will fail if there is less than 80% branch, line, and function coverage, or if there...
Read more >
Telecommunication Version D and Above Questions, ...
Coverage Ends for Medicare Part A Resident Before Medication Supply ... Claim Segment contains the Other Coverage Code (308-C8) and can 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