-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Coverage All not working in projects (monorepo) #4181
Comments
Start a new pull request in StackBlitz Codeflow. |
Run test in workspce: npm run test:coverage -w @demo/telemetry |
|
|
I don't think this is coverage related. When you run You can see this by adding console.log("cwd", process.cwd())
|
I don't understand the problem. Does coverage work if you add The coverage option is never inherited: https://vitest.dev/guide/workspace.html#configuration When you run vitest from the monorepo root, Vitest will use coverage option from the root config. When you run vitest inside a workspace project, it will use coverage from the project's config. |
The problem is that when running coverage all in a package, all files in that package are not taken into coverage reports. Only if i define |
I don't understand why no one has added a label bug yet |
No it doesn't. Only shows coverage for affected files, not all files in workspace project |
This is how it works, yes. What seems to be the problem?
It shows what you asked it to show in the config. AGAIN, if you run vitest from the root of the project, it takes the coverage options (where you specify |
Yes, because the documentation and typescript types say that I cannot override coverage in defineProject |
AGAIN, i want to see coverage for all files in |
What I expect from vitest is the ability to show coverage all files inside the mono repository package. I thought it was enough to specify coverage all in the root of the monorepository, but it doesn't work that way. I cannot specify |
You can even add another In short, you are meant to run |
You are running separate Vitest command for each folder, and you don't use the Vitest workspace feature by doing so, so it cannot work even in theory - each Vitest instance doesn't know about another one. |
I just need the opposite and with |
Then use |
But it seems to me that there should be an inheritance mechanism from the root config |
No, there shouldn't be. With this logic, Vitest will have to traverse all folder to the root of the file system and merge them - which makes zero sense. |
And then how will vitest workspaces work ? In the monorepository root, I want to run all the tests (in CI) and with full coverage. But when I'm developing, I want to run tests in a specific directory (package/project) with coverage of all the files for this project. |
When you run "vitest" in a folder, there is only one config - the one in the folder, unless you manually merge it there.
You run vitest packages/telemetry |
I would like to add a command to the package.json of the package/project, but what will it look like then? |
And there will be support for the |
@sheremet-va It seems that, thanks to your comments, I realized that workspaces work when running from the root of the project. It's great that I understood this, but how can I selectively run tests in a specific package now? Preferably using the package.json of this package? |
Is it a bug?
|
What am I looking at? |
Double test execution |
@sheremet-va Another error. Coverage not working.
|
Hey folks - just following through this thread when debugging my own coverage issues, and wondering if we could clarify what the expected configuration is for vitest providing coverage in monorepos? I can explain fully my situation and what I'm struggling with - brain dump incoming! My monorepo (using package-based Nx and yarn workspaces) looks like this:
vitest.workspace.ts: export default ['packages/*']; vitest.shared.ts: import {defineConfig} from 'vitest/config';
export default defineConfig({
test: {
// The cache setting improves test performance by caching test artifacts.
// Storing the cache in the node_modules folder keeps it ignored by version control systems and simplifies project management.
cache: {
// When this runs it is relative to the projects in `packages`, hence the `../../` to get to node_modules at the root of the repo.
dir: '../../node_modules/.vitest/assets',
},
// The coverage setting enables test coverage reporting, helping you understand how much of your code is covered by tests.
// This information can be used to identify areas of the code that require additional testing.
coverage: {
// Specify the minimum percentage of statement coverage.
// Statement coverage measures the percentage of code statements that are executed by your tests.
// A statement is any individual line of code in your JavaScript or TypeScript file.
statements: 80,
// Specify the minimum percentage of branch coverage.
// Branch coverage measures the percentage of code branches that are executed by your tests.
// A branch is any conditional statement (such as an `if` statement or a `switch` statement)
// that can result in multiple possible outcomes.
branches: 70,
// Specify the minimum percentage of function coverage.
// Function coverage measures the percentage of code functions that are executed by your tests.
// A function is a reusable block of code that performs a specific task.
functions: 70,
// Specify the minimum percentage of line coverage.
// Line coverage measures the percentage of code lines that are executed by your tests.
// A line is any individual line of code in your JavaScript or TypeScript file, including blank lines and comments.
lines: 80,
// These reporters are required for displaying code coverage information:
// text to show the results in the console
// lcov to produce reports for use in SonarQube
// html to allow devs to view interactive reports in their browsers with custom test:report commands
reporter: ['text', 'lcov', 'html'],
// Generates the coverage reports even if the test run fails - allows us to use test:report to view reports with up to date results.
reportOnFailure: true,
// Exclude .stories files from coverage checks
exclude: ['**/*.stories*'],
},
testTimeout: 30000,
// Sets the print limit for errors in the console to a high value so that we can see all errors from React Testing Library.
globalSetup: '../../setDebugPrintLimit.ts',
// Sets the test reporter format to 'verbose', which gives detailed information about each test run.
// Other options include 'default' and 'tap', depending on your preferences and requirements.
reporters: ['verbose'],
// We don't want vitest to fail if it encounters a package that has no tests
passWithNoTests: true,
},
}); packages/is-even/vitest.config.ts: import {defineProject, mergeConfig} from 'vitest/config';
import baseTestConfig from '../../vitest.shared';
const config = defineProject({
test: {
// The environment setting determines the environment to run tests in.
// 'happy-dom' is chosen for its better performance and lightweight DOM emulation compared to other environments like 'jsdom'.
environment: 'happy-dom',
// The globals setting enables global variables in tests.
// This is useful when using testing libraries that rely on global variables, such as Jest's 'expect' function.
globals: true,
// Will call .mockClear() on all spies before each test.
// .mockClear() will clear all recorded calls to a mocked function or object, but does not reset any other behavior or implementation details.
clearMocks: true,
// The include setting ensures that only test files are executed, avoiding accidental execution of non-test code.
include: ['**/*.test.[t]s?(x)'],
// The setupFiles setting allows you to execute specific files before running tests.
// This is useful for setting up global configurations, mocks, or utilities required by your test suite.
setupFiles: ['./test/setupTests.ts'],
},
});
export default mergeConfig(baseTestConfig, config); What I have been doing to generate "testc": "nx run-many --target=testc" packages/is-even/package.json: "test": "vitest --run",
"test:report": "nx run testc; open ./coverage/index.html",
"testc": "vitest --run --coverage" sonar-project.properties: ...
# Reports
sonar.javascript.lcov.reportPaths=packages/**/coverage/lcov.info When I run
The trouble I have now though is for the
....
TN:
SF:src/App.tsx
FN:11,App
FNF:1
FNH:1
FNDA:3,App
.... So I need a way to either:
When I tried adding a Apologies for the essay - any feedback is very welcome! Thank you! |
This should be the way to run coverage for the whole monorepo, yes. I don't really know why it doesn't work from what you described - I would expect it to work. It either doesn't use |
I’m unsure if this is the right issue, but I’ll comment here first. @sheremet-va - let me know if I should create a new issue dedicated to "Vitest Workspace + Nx monorepo." I think I am running into similar issues to @ahayes91 (I can create a minimal repro repo if desired). TL;DR - Nx wants More detail, assuming only bare minimum Nx knowledge. Any additional info is meant to inform, zero condescending tone from me here! Nx delivers great DX wins for monorepos by (essentially) caching each project. Nx workspace/repo == vitest workspace; Nx project == vitest project. Nx (and I) want to run tests for projects one at a time. This way a subsequent "full test run" ( However, Vitest’s workspace docs suggest (imply?) that there should be a single Now, I may be wrong at that. What I want is to run a vitest project in the context of a workspace run. I am not sure how to do that from the docs. I have tried both the What (I think) I want is
Are these 3 things possible together in Vitest today? I’m finally getting back into Vitest world and super happy to see some formal workspace support following #256 (I had one comment there before). Thanks so much in advance and love seeing the 1.0 betas; that's what I've upgraded to. |
I think you are looking for non-implemented
This is working. As I said, for aggregated result Vitest expects a single All you need is |
I think #4519 represents what I envision. I will follow that.
I will share thoughts on this in the other issue. TL;DR - workspace-root relative paths to merge. Thank you so much! Edit: I started a discussion to gather more thoughts for now: #4544 |
To run specific projects, you can use # run a single project
vitest --project e2e
# run several projects
vitest --project e2e --project unit |
Describe the bug
Does not display coverage for all files
vitest.config.ts
vitest.workspace.ts
packages/telemetry/vitest.config.ts
working with
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-yorpqm?file=packages%2Ftelemetry%2Fvitest.config.ts&view=editor
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: