Vitest Setup
Integrate Spekra with your Vitest test suite to track test stability and identify flaky tests.
Prerequisites
- Node.js 20 or later
- Vitest 1.x
- A Spekra account with an API key
Installation
Install the Spekra Vitest reporter:
npm install @spekra/vitestConfiguration
Add the reporter to your vitest.config.ts:
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
reporters: [
'default',
['@spekra/vitest', {
apiKey: process.env.SPEKRA_API_KEY,
source: 'my-unit-tests', // Required: identifies your test suite
}],
],
},
});
Set Your API Key
Add your API key to your environment:
export SPEKRA_API_KEY=sk_your_api_key_here
Secure your API key
Never commit your API key to source control. Use environment variables or a secrets manager in CI.
Run Your Tests
Run your tests as usual:
npx vitest run
Your test results will appear in your Spekra dashboard.
Watch mode
The reporter only sends results when running vitest run. Watch mode (vitest without run) does not report results to avoid noise from continuous reruns.
What Gets Captured
The reporter automatically captures:
| Data | Description |
|---|---|
| Test results | Pass, fail, skip status |
| Duration | How long each test took |
| Suite path | Describe block hierarchy |
| Test file | Path to the test file |
| Project | Vitest workspace name (if using workspaces) |
| Git info | Branch, commit SHA |
| CI info | Job URL, workflow name |
Workspace Support
If you're using Vitest Workspaces, the reporter automatically captures the workspace name:
// vitest.workspace.ts
export default [
'packages/*',
];
Each test result will include the workspace/project name, allowing you to filter by project in the Spekra dashboard.
Detecting Flaky Tests
Vitest supports test retries which helps with flaky test detection:
export default defineConfig({
test: {
retry: 2, // Retry failed tests up to 2 times
reporters: [
'default',
['@spekra/vitest', {
apiKey: process.env.SPEKRA_API_KEY,
source: 'my-unit-tests',
}],
],
},
});
Spekra tracks retry attempts and identifies tests that pass after retrying as potentially flaky.
Next Steps
- Configuration options - All available reporter options
- CI/CD integration - Set up reporting in your CI pipeline
- Understanding flaky tests - Learn how Spekra detects flaky tests