Skip to content

fix: extraneous coverage for --project (fix #6331) #7885

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

Merged
merged 17 commits into from
Jun 26, 2025

Conversation

gtbuchanan
Copy link
Contributor

@gtbuchanan gtbuchanan commented Apr 24, 2025

Description

Coverage was reported for projects outside of the specified project filter. For example, given a monorepo with two projects, p1 and p2, vitest run --coverage --project=p1 reported coverage for code from p2 at 0% because no tests from p2 were run.

Resolves: #6331

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Coverage was reported for projects outside of the specified project
filter. For example, given a monorepo with two projects, p1 and p2,
`vitest run --coverage --project=p1` reported coverage for code from p2
at 0% because no tests from p2 were run.
@gtbuchanan
Copy link
Contributor Author

@AriPerkkio Does this align with what you were thinking? I still need to familiarize myself with how the tests are written, but I wanted to ensure I was on the right track first.

Also, should I move the duplicate code to a common location (like BaseCoverageProvider)?

Copy link
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change would only filter out uncovered files outside the workspace project's root. We should filter out covered files too.

To do this, we'll need to make this.testExclude an array, and create one for each project with their roots.

this.testExclude = new TestExclude({
cwd: ctx.config.root,

Copy link

netlify bot commented Apr 25, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 11107dc
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/685c29ca9f575b0008d296bc
😎 Deploy Preview https://deploy-preview-7885--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@gtbuchanan
Copy link
Contributor Author

That should do it. It seems to work for my project. I will add tests when I figure that out 🙂

As an aside, I wasn't able to move the duplicate code to BaseCoverageProvider because vitest/node doesn't reference test-exclude. I figured it wasn't worth adding the extra reference for this fix.

Copy link
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far.

As an aside, I wasn't able to move the duplicate code to BaseCoverageProvider because vitest/node doesn't reference test-exclude. I figured it wasn't worth adding the extra reference for this fix.

That's okey for now. In #7837 we'll remove test-exclude completely and utilize packages that are available in vitest package. That allows re-using logic from BaseProvider.

For test cases we'll need to verify following:

  • When --project filter is passed, files outside that project are excluded. This means covered and uncovered files.
  • When --project filter is not passed, files in projects and in the root should be visible.

You can add fixtures in test/coverage-test/fixtures/workspaces and create test cases in new file test/coverage-test/test/workspace.test.ts for example.

@AriPerkkio
Copy link
Member

Hey @gtbuchanan, do you have any time to finalize this PR? I'm also happy to take over and finish it if you prefer that. Thanks for all the work so far! 💯

@gtbuchanan
Copy link
Contributor Author

gtbuchanan commented Jun 23, 2025

@AriPerkkio I would have liked to finish it myself, but I'm running into some issues:

  1. I figured out how to write the tests, but the one testing my fix is actually failing. I'm trying to debug the coverage test in VS Code using these steps, but clicking "Run and Debug" immediately stops debugging. Do you have any tips? Is there a specific file I'm supposed to have open?
  2. Obviously, my branch is now outdated. Out of curiosity, I added my unit tests on top of main to make sure one would fail, but they actually passed. I'm assuming my test is just wrong since main doesn't appear to have a fix for this. Will you glance at my tests to see if something stands out?

EDIT:
Disregard 2. There must have been some caching involved. I picked the PR back up on my work computer and the test fails there as expected. Now to reapply the fix...

@gtbuchanan gtbuchanan marked this pull request as ready for review June 23, 2025 16:25
@gtbuchanan
Copy link
Contributor Author

I didn't figure out how to debug the coverage tests in VS Code, but I was able to get everything working! It seems to work without updating isIncluded, but let me know if I'm missing something.

Copy link
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must have been some caching involved. I picked the PR back up on my work computer and the test fails there as expected. Now to reapply the fix...

The test cases are running against built assets so you'll need to run pnpm build or pnpm dev before running the test cases. That might explain the caching issue.

I didn't figure out how to debug the coverage tests in VS Code

These test cases are running "Vitest inside Vitest" so it's likely that VS Code extension is unable to inspect those.

@gtbuchanan gtbuchanan requested a review from AriPerkkio June 24, 2025 17:58
Copy link
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small improvements, otherwise this is looking good!

Btw as this PR is from a Github organization account (energyworldnet), Vitest maintainers cannot push any changes to your PR branch. Usually I would fix small errors like this myself and push changes to your branch directly, but now it's not possible due to permission requirements.

Copy link
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for the PR @gtbuchanan! 💯

@AriPerkkio AriPerkkio merged commit 761beee into vitest-dev:main Jun 26, 2025
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow project filtered test runs to report coverage of only filtered projects
2 participants