3.2 KiB
3.2 KiB
Bun Test Output Customization Guide
Command Line Options
Basic Output Control
# Run with coverage report
bun test --coverage
# Use specific coverage reporter (text or lcov)
bun test --coverage --coverage-reporter=text
bun test --coverage --coverage-reporter=lcov
bun test --coverage --coverage-reporter=text,lcov
# Set custom coverage directory
bun test --coverage --coverage-dir=my-coverage
# Run only specific test name patterns
bun test --test-name-pattern="HttpClient"
bun test -t "should handle"
# Stop after first failure
bun test --bail
bun test --bail=3 # Stop after 3 failures
# Run only tests marked with .only()
bun test --only
# Include todo tests
bun test --todo
Reporters
# Use JUnit XML reporter for CI/CD
bun test --reporter=junit --reporter-outfile=test-results.xml
# Default console output (no flag needed)
bun test
Test Filtering
# Run specific test files
bun test http.test.ts
bun test "**/*integration*"
# Run tests multiple times to catch flaky tests
bun test --rerun-each=3
Configuration in bunfig.toml
You can set default options in your bunfig.toml file:
[test]
# Always generate coverage
coverage = true
# Set custom timeout
timeout = "10s"
# Set default reporter
reporter = "junit"
reporter-outfile = "test-results.xml"
# Set coverage options
coverage-reporter = "text,lcov"
coverage-dir = "coverage"
# Test environment
preload = ["./test/setup.ts"]
[test.env]
NODE_ENV = "test"
LOG_LEVEL = "silent"
Custom Test Setup
You can customize test output through your test setup files:
In test/setup.ts:
// Customize console output
console.log = (message) => {
// Custom formatting
if (typeof message === 'string') {
console.info(`[TEST] ${new Date().toISOString()} ${message}`);
}
};
// Add custom test utilities
global.testHelpers = {
logTestStart: (testName: string) => {
console.info(`🧪 Starting test: ${testName}`);
},
logTestEnd: (testName: string, passed: boolean) => {
const icon = passed ? '✅' : '❌';
console.info(`${icon} Completed test: ${testName}`);
}
};
In individual test files:
import { describe, test, expect, beforeEach } from 'bun:test';
describe('HttpClient', () => {
beforeEach(() => {
global.testHelpers?.logTestStart('HttpClient test');
});
test('should work', () => {
// Your test code
global.testHelpers?.logTestEnd('should work', true);
});
});
Package.json Scripts
Add custom test scripts with different output configurations:
{
"scripts": {
"test": "bun test",
"test:coverage": "bun test --coverage",
"test:junit": "bun test --reporter=junit --reporter-outfile=test-results.xml",
"test:watch": "bun test --watch",
"test:verbose": "bun test --coverage --coverage-reporter=text",
"test:ci": "bun test --coverage --reporter=junit --reporter-outfile=test-results.xml --bail",
"test:specific": "bun test --test-name-pattern"
}
}
Environment Variables
Control output through environment variables:
# Set log level for tests
LOG_LEVEL=debug bun test
# Silent mode
LOG_LEVEL=silent bun test
# Custom formatting
TEST_OUTPUT_FORMAT=json bun test