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

Istanbul: Coverage ignore doesn't work for default in switch #6153

Open
6 tasks done
akellbl4 opened this issue Jul 17, 2024 · 6 comments
Open
6 tasks done

Istanbul: Coverage ignore doesn't work for default in switch #6153

akellbl4 opened this issue Jul 17, 2024 · 6 comments
Labels
feat: coverage Issues and PRs related to the coverage feature upstream

Comments

@akellbl4
Copy link
Contributor

Describe the bug

If switch...case is used and default statement is not covered by tests and /* istanbul ignore next -- @preserve */ used for ignoring the branch, it still shows up as uncovered.

I tried different combinations with double slash comment and wrapping default into a block, which produced the same result.

Reproduction

https://stackblitz.com/edit/github-rzcfqv

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/coverage-istanbul: ^2.0.3 => 2.0.3 
    vitest: ^2.0.3 => 2.0.3

Used Package Manager

npm

Validations

@AriPerkkio AriPerkkio added upstream feat: coverage Issues and PRs related to the coverage feature and removed pending triage labels Jul 17, 2024
@AriPerkkio
Copy link
Member

Looks like esbuild doesn't respect comments inside switch:

https://esbuild.egoist.dev/#W1siaW5kZXgudH...:

switch (option) {
  ...
  /* This is removed -- @preserve */
  /* istanbul ignore next -- @preserve */
  default: {
    /* This is preserved -- @preserve */
    /* istanbul ignore next -- @preserve */
    return 'Not a foo, not a bar';
   }
}

Becomes:

switch (option) {
  ...
  default: {
    /* This is preserved -- @preserve */
    /* istanbul ignore next -- @preserve */
    return "Not a foo, not a bar";
  }
}

I don't think there are any work-arounds for this unfortunately.

@akellbl4
Copy link
Contributor Author

akellbl4 commented Jul 18, 2024

@AriPerkkio thanks for looking into it! I'll file an issue to esbuild. Should this issue be closed or left for tracking purposes?

@AriPerkkio
Copy link
Member

We can leave this as open as it's now marked as upstream issue. It's good for others who run into same issue. And maybe in future once Vite moves from esbuild to Rolldown this issue could get fixed.

@hyrious
Copy link

hyrious commented Jul 28, 2024

I haven't use vitest's coverage tool before so maybe I'm wrong. Does vitest support sourcemaps in coverage report? At least I know c8 supports it which is working perfectly on my tiny projects (like this one). In addition integrating esbuild in tests like this is quite simple. Here's the source code of my tiny ts loader.

Sourcemap support is obviously useful here because the report can find the original comments from the source instead of from the transpiled js.

@AriPerkkio
Copy link
Member

@hyrious sure it does support. Without source maps code coverage in Vitest would be useless as most users are using code transforms.

@vitest/coverage-v8 works just fine with the reproduction as it does exclusion when comparing to the source code. Note that the issue title mentions Istanbul coverage.

image

Istanbul's instrumentation is done to the transpiled code, not the source codes.

const code = this.instrumenter.instrumentSync(
sourceCode,
id,
sourceMap as any,
)

@douglasjunior
Copy link

Same situation with /* istanbul ignore else */

image

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

No branches or pull requests

4 participants