vitest: Coverage All not working in projects (monorepo)
Describe the bug
vitest --coverage
Does not display coverage for all files
vitest.config.ts
import { defineConfig } from "vitest/config";
const isCI = process.env.CI === "true";
export default defineConfig({
test: {
watch: false,
silent: isCI,
reporters: isCI ? ["verbose", "json", "junit"] : ["default", "hanging-process"],
passWithNoTests: true,
outputFile: {
json: "test-results.json",
junit: "test-results.xml",
},
coverage: {
all: true,
clean: true,
enabled: isCI,
provider: "v8",
include: ["**/*.ts"],
reporter: isCI ? ["lcov", "json-summary", "text-summary"] : "text",
},
benchmark: {
reporters: isCI ? ["verbose", "json"] : "default",
outputFile: "benchmark-results.json",
},
testTimeout: 300,
},
});
vitest.workspace.ts
export default ["apps/*", "packages/*"];
packages/telemetry/vitest.config.ts
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { defineProject } from "vitest/config";
import pkg from "./package.json" assert { type: "json" };
export default defineProject({
test: {
name: pkg.name,
root: dirname(fileURLToPath(import.meta.url)),
},
});
working with
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { defineProject } from "vitest/config";
import pkg from "./package.json" assert { type: "json" };
export default defineProject({
test: {
name: pkg.name,
root: dirname(fileURLToPath(import.meta.url)),
coverage: {all: true}
},
});
Reproduction
System Info
sh: envinfo: command not found
"@vitest/coverage-v8": "^0.34.3",
"@vitest/runner": "^0.34.3",
"@vitest/snapshot": "^0.34.4",
"vite": "^4.4.9",
"vitest": "^0.34.3"
Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: closed
- Created 9 months ago
- Comments: 34 (13 by maintainers)
I don’t think this is coverage related. When you run
npm run <command> -w <project>
, it will change the current working directory to that path. Vitest doesn’t traverse parent tree up to see if there are other configurations. It’s same as if you didcd packages/telemetry
+npm run test
.You can see this by adding
process.cwd()
logging inpackages/telemetry/vitest.config.ts
and running the commands:To run specific projects, you can use
--project
flag when #4561 is released:I think you are looking for non-implemented
--project
option: #4519This is working. As I said, for aggregated result Vitest expects a single
vitest
executable at the root repository/workspace level. It knows what projects you have and their configs and can instrument all your projects from a single command. How else would it know what to merge if you are running completely isolated executables for each project?All you need is
"scripts": { "test": "vitest" }
in your root workspacepackage.json
, andvitest.workspace.ts
file.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:
vitest.shared.ts:
packages/is-even/vitest.config.ts:
What I have been doing to generate
lcov.info
files for use in SonarQube code coverage is to run avitest
command in each package, with a command like this from the root package.json:packages/is-even/package.json:
sonar-project.properties:
When I run
yarn testc
from the root, a folder likepackages/is-even/coverage
is generated in each package:The trouble I have now though is for the
App.tsx
file, which has the same name and directory structure in both packages, SonarQube coverage isn’t reported correctly on one of those files (I think because the name conflicts happen when thelcov.info
reports are loaded by SonarQube)packages/is-even/coverage/lcov.info
So I need a way to either:
lcov.info
(not yet supported by vitest according to the answer on https://github.com/vitest-dev/vitest/discussions/4227 - is there another way folks have found to do this?)When I tried adding a
"test": "vitest --coverage"
command in the rootpackage.json
of the monorepo (like what I gather should be done from the comments), I got a lot of runtime errors in the tests likeReferenceError: window is not defined
and those sorts of things, and I wasn’t able to fix all those errors to get the command working.Apologies for the essay - any feedback is very welcome! Thank you!