Injecting code/comments after transformation - c8 coverage fails
See original GitHub issueThanks 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:
- Remove that branch logic or the
_interopRequireDefault
entirely - 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:
- Created 2 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top GitHub Comments
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 withproduction: 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:
/* c8 ignore next */
or/* c8 ignore start */
in theinteropRequireDefault
helper code to avoid the uncovered line, but maybe there’s a way to make it work./* c8 ignore next */
) to the start of the file seemed to make the problem go away..replace(/\s+/g, " ")
(which removes newlines and spaces for helpers, making them one long line) seemed to make the problem go away.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 wherec8
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 modifytransformedCode
there if you want without needing any changes in Sucrase.I tested it again with
"@sucrase/jest-plugin": "^3.0.0"
"sucrase": "^3.28.0"
The issue doesn’t appear for me anymore 🙃