vitest-dev / vitest-dev/vitest
Support fileTemplate/filePrefix alongside junit classnameTemplate
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 17.1k
- Forks
- 2k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 94
Description
Clear and concise description of the problem
When working in pnpm monorepos with lots of packages (each with their own vitest config extending a shared base config), the default generated file attribute and classname in output junits is relative to the individual package, not the project repo. This interferes with my ability to associate GitHub CODEOWNERS files with the test file, as well as other test analytics projects.
With the following config, I can add a file attribute to my vitest junit outputs, and get the proper classname, but the file attribute is missing the appropriate prefix.
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
reporters: [
['junit', {
outputFile: './junit.xml',
addFileAttribute: true,
// override the classname to reflect the relative path from the root of the repo
classnameTemplate: ({ filepath: abs }) => abs.replace(..., ""),
}],
],
},
});
results in a junit like:
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites name="vitest tests" tests="19" failures="0" errors="0" time="0.049715176">
<testsuite name="__tests__/my-test.vitest.ts" timestamp="2026-05-13T14:31:55.096Z" hostname="hostname" tests="1" failures="0" errors="0" skipped="0" time="0.00511053">
<testcase classname="ts/packages/my-package/__tests__/my-test.vitest.ts" file="__tests__/my-test.vitest.ts" name="A useful test" time="0.002876578">
</testcase>
</testsuite>
</testsuites>
I would like it to be file="ts/packages/my-package/__tests__/my-test.vitest.ts". Also relevant but not required would be testsuite name="ts/packages/my-package/__tests__/my-test.vitest.ts"
Suggested solution
Support a fileTemplate or filePrefix to configure this file attribute, analogous to the classnameTemplate field.
Alternative
Some other attribute (independent of junit reporters) indicating the location of package. Projects do not support my use case because I have multiple vitest configs. Overriding root also does not seem to fix this (no tests are detected).
Additional context
The Jest junit reporter supports filePathPrefix for this issue.
I'm happy to help put up a PR for this, but I would appreciate some more guidance first.
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that requests the same feature to avoid creating a duplicate.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the JUnit reporter implementation and its handling of classnameTemplate and addFileAttribute. Compare the requested fileTemplate or filePrefix behavior with the existing options and the Jest filePathPrefix reference. Done means JUnit output can configure the file attribute to include the repository-relative prefix, with tests covering the new option.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100