Vitest
A guide for generating Trunk-compatible test reports with Vitest
You can automatically detect and manage flaky tests in your Vitest projects by integrating with Trunk. This document explains how to configure Vitest to output JUnit XML reports that can be uploaded to Trunk for analysis.
Checklist
By the end of this guide, you should achieve the following before proceeding to the next steps to configure your CI provider.
After correctly generating reports following the above steps, you'll be ready to move on to the next steps to configure uploads in CI.
Generating Reports
Trunk detects flaky tests by analyzing test reports automatically uploaded from your CI jobs. You can do this by generating Trunk-compatible XML reports from your test runs.
You can configure Vitest to produce a Trunk-compatible JUnitXML report by updating your vitest.config.ts
.
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
reporters: [
['junit', { outputFile: './junit.xml' }],
],
},
});
Report File Path
The outputFile: './junit.xml'
option specifies the path of the JUnit report. You'll need this path later when configuring automatic uploads to Trunk.
Disable Retries
You need to disable automatic retries if you previously enabled them. Retries compromise the accurate detection of flaky tests. You should disable retries for accurate detection and use the Quarantining feature to stop flaky tests from failing your CI jobs.
If you've enabled retries, you can disable them following the Vitest docs for more accurate results.
Troubleshooting
Configuration Errors and File-Level Test Failures
Issue: You might see Trunk identifying flaky tests with names that match your test file names (e.g., auth.test.ts
instead of should login successfully
) rather than individual test case names.
Root Cause: This typically occurs when Vitest encounters configuration errors that prevent it from properly parsing or running the tests in a file. Common scenarios include:
TypeScript configuration errors in
tsconfig.json
Missing dependencies or import resolution failures
Syntax errors in test setup files
Invalid Vitest configuration options
What Happens: When Vitest cannot execute the individual tests within a file due to configuration issues, it generates a single JUnit test case entry named after the file itself, regardless of how many actual test cases exist in that file.
How to Diagnose:
Run your tests locally with verbose output:
vitest --reporter=verbose
Check for configuration warnings or errors in the test output
Look for test files that show as single entries in your JUnit report when they should contain multiple test cases
How to Fix:
Check TypeScript Configuration: Ensure your
tsconfig.json
is valid and includes all necessary pathsVerify Dependencies: Make sure all imported modules are properly installed and accessible
Review Setup Files: Check any test setup files referenced in your Vitest config for errors
Validate Vitest Config: Ensure your
vitest.config.ts
doesn't contain invalid options
Try It Locally
Validate Test Execution First
Before validating your JUnit reports with Trunk, ensure Vitest can properly execute your tests:
# Run tests with detailed output to catch configuration issues
vitest run --reporter=verbose
# Check that individual test cases appear in output, not just file names
vitest run --reporter=json | jq '.testResults[].assertionResults'
If you see test files listed as single entries rather than individual test cases, you likely have configuration issues that need to be resolved before proceeding.
You can validate your test reports using the Trunk CLI. If you don't have it installed already, you can install and run the validate
command like this:
The Validate Command
curl -fsSLO --retry 3 https://trunk.io/releases/trunk && chmod +x trunk
./trunk flakytests validate --junit-paths "./junit.xml"
This will not upload anything to Trunk. To improve detection accuracy, you should address all errors and warnings before proceeding to the next steps.
Test Upload
Before modifying your CI jobs to automatically upload test results to Trunk, try uploading a single test run manually.
You make an upload to Trunk using the following command:
curl -fsSLO --retry 3 https://trunk.io/releases/trunk && chmod +x trunk
./trunk flakytests upload --junit-paths "./junit.xml" \
--org-url-slug <TRUNK_ORG_SLUG> \
--token <TRUNK_ORG_TOKEN>
You can find your Trunk organization slug and token in the settings or by following these instructions. After your upload, you can verify that Trunk has received and processed it successfully in the Uploads tab. Warnings will be displayed if the report has issues.


Next Step
Configure your CI to upload test runs to Trunk. Find the guides for your CI framework below:
Last updated