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

Vitest ignores coverage tresholds if reports fail to be created #3213

Closed
6 tasks done
christian-bromann opened this issue Apr 19, 2023 · 2 comments · Fixed by #3217
Closed
6 tasks done

Vitest ignores coverage tresholds if reports fail to be created #3213

christian-bromann opened this issue Apr 19, 2023 · 2 comments · Fixed by #3217
Labels
feat: coverage Issues and PRs related to the coverage feature

Comments

@christian-bromann
Copy link
Contributor

Describe the bug

Given I have a vitest.conf.js defined as following:

export default defineConfig({
  test: {
    include: ['tests/**/*.test.js'],
    exclude: [
      'dist', '.idea', '.git', '.cache',
      '**/node_modules/**', 'examples/**'
    ],
    coverage: {
      enabled: true,
      statements: 100,
      branches: 100,
      functions: 100,
      lines: 100
    }
  }
})

The config states that I am interested in asserting the test coverage for my tests and have them fail if they don't meet the treshold (which are in this example 100%).

Due to the recent issues with creating test coverage reports, e.g. in #3125, we found out that our tests were not validated against our given treshold causing pull requests that decreased the overall coverage in our test suite to just pass. After we fixed the issue by updating the dependencies of vitest and @vitest/c8-coverage, coverage reports were created again but we got surprised by the failing treshold validation.

I think it is a bug if Vitest should ignores the treshold validation due to missing coverage reports. If test coverage validation is enabled by providing a statements, branches, functions or lines property, Vitest should ensure that the validation happens and fails if for some reasons it can't find any coverage data.

Reproduction

Open with VS Code: Checkout with Runme

or manually check out: https://github.com/christian-bromann/vitest-coverage-bug

System Info

System:
    OS: macOS 13.3
    CPU: (10) arm64 Apple M1 Pro
    Memory: 45.38 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/.nvm/versions/node/v18.15.0/bin/node
    npm: 9.5.0 - ~/.nvm/versions/node/v18.15.0/bin/npm
  Browsers:
    Brave Browser: 111.1.49.120
    Chrome: 112.0.5615.121
    Chrome Canary: 114.0.5721.0
    Firefox Nightly: 114.0a1
    Safari: 16.4
  npmPackages:
    @vitest/coverage-c8: ^0.29.1 => 0.29.7
    vitest: ^0.30.1 => 0.30.1

Used Package Manager

npm

Validations

@AriPerkkio AriPerkkio added the feat: coverage Issues and PRs related to the coverage feature label Apr 20, 2023
@AriPerkkio
Copy link
Member

AriPerkkio commented Apr 20, 2023

In vitest@0.29.8 the #3040 introduced an API mismatch bug where vitest was unable to load @vitest/coverage-c8 package if version of @vitest/coverage-c8 was not 0.29.8 as well. In 0.30.0 this is fixed. Your reproduction case is running into this old (already fixed) bug. It's using vitest@0.29.8 and @vitest/coverage-c8@0.29.1.

As the whole coverage provider package fails to load we cannot do anything coverage related - checking thresholds here would require duplicating the logic from coverage package to vitest core package. But I think we can improve the situation by failing whole process if there is coverage.enabled: true and the coverageModule below is falsy. So instead of silently failing to load the coverage package (and disabling coverage) due to possible API mismatch bugs, we'll fail the whole process.

if (provider === 'c8' || provider === 'istanbul') {
const { default: coverageModule } = await loader.executeId(CoverageProviderMap[provider])
return coverageModule
}

The error message could clearly indicate that @vitest/coverage-<x> failed to load, and maybe even recommend to check the package versions.

@christian-bromann
Copy link
Contributor Author

Awesome, thanks @AriPerkkio for the quick turnaround!

@github-actions github-actions bot locked and limited conversation to collaborators Jun 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: coverage Issues and PRs related to the coverage feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants