parcel-bundler / parcel

The zero configuration build tool for the web. πŸ“¦πŸš€
https://parceljs.org
MIT License
43.27k stars 2.26k forks source link

istanbul code coverage generation plugin #9248

Open rschoenbichler opened 9 months ago

rschoenbichler commented 9 months ago

πŸ™‹ feature request

A transformer plugin that instruments the build to record code coverage in an istanbul.js compatible format

πŸ€” Expected Behavior

include via .parcelrc transformer section

😯 Current Behavior

not present

πŸ’ Possible Solution

After some local trials, the istanbul-lib-instrumentation transformer always fails with a don't know how to turn this value into a node error. I've not found any other library so far for code coverage augmentation.

πŸ”¦ Context

I'm trying to augment a react/typescript/esnext application to retrieve code coverage information during runtime.

πŸ’» Examples

import { createInstrumenter } from "istanbul-lib-instrument";
import { Transformer } from "@parcel/plugin";

const parcelInstrumenter = new Transformer({
    async transform(p) {
        const instrumenter = createInstrumenter({ produceSourceMap: true, esModules: true });
        const codeIn = await p.asset.getCode();
        const mapIn = await p.asset.getMap();

        const instrumented = instrumenter.instrumentSync(codeIn, p.asset.filePath, mapIn);
        p.asset.setCode(instrumented);
        p.asset.setMap(instrumenter.sourceMap);

        return [p.asset];
    },
});

export default parcelInstrumenter;
mischnic commented 9 months ago

That sounds like a good third-party plugin. What problem did you have with the plugin code you posted?

rschoenbichler commented 9 months ago

That sounds like a good third-party plugin.

Absolutely, I just think that the parcel eco-system as a whole would benefit from this feature. So the awareness on the parcel side makes sense to me.

What problem did you have with the plugin code you posted?

I was trying to instrument a react / typescript / es-next module application. I've added the transformer as last step to the tsx transformer pipeline. The instrumentation failed in some deep recursive stack in the istanbul code traversal with a meaningless "can't turn this into a node" failure.

I know this is not parcels responsibility. Looking at the plugin I've posted, I think the code and the transformer API make sense. I just don't know where to go from here. Maybe istanbul isn't even a viable option because it is nested too deep in the babel ages. Which is why I thought reaching out to you makes sense, and maybe this is something that parcel want's to push as well and take it on your own agenda too?

mischnic commented 9 months ago

That transformer plugin should work.

I've added the transformer as last step to the tsx transformer pipeline.

Try putting it at the start of the pipeline instead.

Which is why I thought reaching out to you makes sense, and maybe this is something that parcel want's to push as well and take it on your own agenda too?

No, we already have quite few people working on Parcel itself.

rschoenbichler commented 9 months ago

Putting it to the start causes Istanbul to fail at the first typescript specific code segment (in my case a generic type)