Jest Setup
Integrate Spekra with your Jest test suite to track test stability and identify flaky tests.
Prerequisites
- Node.js 18 or later
- Jest 29 or later
- A Spekra account with an API key
Installation
Install the Spekra Jest reporter:
npm install @spekra/jestConfiguration
Add the reporter to your jest.config.js or jest.config.ts:
module.exports = {
reporters: [
'default',
['@spekra/jest', {
apiKey: process.env.SPEKRA_API_KEY,
source: 'my-unit-tests', // Required: identifies your test suite
}],
],
// ... rest of your config
};
Or if you're using TypeScript configuration:
import type { Config } from 'jest';
const config: Config = {
reporters: [
'default',
['@spekra/jest', {
apiKey: process.env.SPEKRA_API_KEY,
source: 'my-unit-tests', // Required: identifies your test suite
}],
],
};
export default config;
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:
npm test
Your test results will appear in your Spekra dashboard.
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 |
| Git info | Branch, commit SHA, author |
| CI info | Job URL, workflow name |
Detecting Flaky Tests
Unlike Playwright's built-in retry mechanism, Jest doesn't retry tests by default. To enable flaky test detection, you have two options:
Option 1: Use jest-retry
npm install jest-retrymodule.exports = {
// Enable retries
testRetries: 2,
};
Option 2: Run tests multiple times in CI
You can run your test suite multiple times in CI to detect flaky tests:
- name: Run tests (attempt 1)
run: npm test
- name: Run tests (attempt 2)
run: npm test
if: always()
Spekra will analyze results across runs to identify tests that pass and fail inconsistently.
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