Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customisation of istanbul instrumentation options #7109

Open
4 tasks done
duncan-thacker opened this issue Dec 20, 2024 · 3 comments
Open
4 tasks done

Allow customisation of istanbul instrumentation options #7109

duncan-thacker opened this issue Dec 20, 2024 · 3 comments
Labels
enhancement: pending triage feat: coverage Issues and PRs related to the coverage feature

Comments

@duncan-thacker
Copy link

Clear and concise description of the problem

I am trying to combine coverage results from vitest with coverage results from cypress (bundled with vite, instrumented with vite-plugin-istanbul). Merging coverage files does not work correctly because vitest instrumentation skips comments, types etc as requested and implemented in #5423, and vite-plugin-istanbul does not skip coverage of these (and also does not allow overriding this behaviour).

Without control over instrumentation for at least one of these (vitest OR vite-plugin-istanbul) there is no way for me to resolve the conflict.

Suggested solution

Provide some extra options in the coverage block that will override default instrumentation, such as

coverage: {
   include: './src/**',
   instrumentation: {
       preserveComments: true,
   } 
}

Alternative

I am also going to raise a ticket for vite-plugin-istanbul to make a similar change, but ideally both packages would implement this change for maximum compatibility.

Additional context

Istanbul instrumentation is configured here:

this.instrumenter = createInstrumenter({
produceSourceMap: true,
autoWrap: false,
esModules: true,
compact: false,
coverageVariable: COVERAGE_STORE_KEY,
// @ts-expect-error missing type
coverageGlobalScope: 'globalThis',
coverageGlobalScopeFunc: false,
ignoreClassMethods: this.options.ignoreClassMethods,
parserPlugins: [
...istanbulDefaults.instrumenter.parserPlugins,
['importAttributes', { deprecatedAssertSyntax: true }],
],
generatorOpts: {
importAttributesKeyword: 'with',
},

For comparison, the equivalent in vite-plugin-istanbul is here. https://github.com/iFaxity/vite-plugin-istanbul/blob/44f780a997cb53956298b39faf6cd2284ea0a151/src/index.ts#L112-L120

Validations

@AriPerkkio
Copy link
Member

As far as I know, preserveComments just removes comments during the transform. Source maps are still pointing correctly, so it should work when you are merging the results with other tools.

because vitest instrumentation skips comments, types etc as requested and implemented in #5423

The #5423 is about V8 provider, not Istanbul. Istanbul already excludes lines that don't contain runtime code.

vite-plugin-istanbul does not skip coverage of these

Could you clarify what you mean by this? Coverage of comments?

If this is something that would help end-users, we can expose this option in public API. But I'm not sure this option is exactly doing what you expect here. Setting up a minimal reproduction that highlights the issue would help. 👍

@AriPerkkio AriPerkkio added the feat: coverage Issues and PRs related to the coverage feature label Dec 21, 2024
@duncan-thacker
Copy link
Author

Okay - I've done my best with a repro here: https://github.com/atropos-tech/coverage-merge-fail-repro

When I say "skip coverage of comments", I really mean skipping instrumentation. I don't know enough about the internals to say for sure, but I do know that the statement maps output in each case do not line up. The repro has more detail in the README but I'm thinking it might have something to do with the following block in the JSON output of cypress + vite-istanbul-plugin:

"inputSourceMap": {
  "version": 3,
  "sources": [
    "<REDACTED>/src/UnderTest.tsx"
  ],
  "mappings": "AAAA,OAAO,WAAW;AAUlB,wBAAwB,UAAU,EAAE,MAAM,GAAmB;AACzD,MAAI,MAAO,QAAO,oCAAC,cAAK,UAAQ;AAChC,SAAO,oCAAC,cAAK,UAAQ;AACzB;",
  "names": []
}

I don't understand how this format works and can't find any good reference material anywhere, but this block doesn't appear in vitest output so it's possible the difference in statement maps is due to this. Grateful for any further insight you might provide.

@AriPerkkio
Copy link
Member

Did you try switching preserveComments value by modifying node_modules/@vitest/coverage-istanbul/**? Does that actually help?

When I say "skip coverage of comments", I really mean skipping instrumentation.

Comments are not instrumented, they cannot be executed in any way. https://github.com/istanbuljs/istanbuljs/blob/main/packages/istanbul-lib-instrument/src/visitor.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement: pending triage feat: coverage Issues and PRs related to the coverage feature
Projects
None yet
Development

No branches or pull requests

2 participants