vitest-dev / vitest-dev/vitest

Support fileTemplate/filePrefix alongside junit classnameTemplate

Open
#10,341 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement: pending triage
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.