@mosaic-code/test-results-to-sqlite
v0.1.0
Published
CLI tool to convert Jest/Vitest JSON test output to SQLite
Maintainers
Readme
test-results-to-sqlite
CLI tool to convert Jest/Vitest JSON test output to SQLite. Useful for agentic workflows where you want to query test results without parsing verbose terminal output.
Installation
npm install -D @mosaic-code/test-results-to-sqliteUsage with Vitest
{
"scripts": {
"test": "vitest run --reporter=json | test-results-to-sqlite ./dev-logging.db"
}
}Or configure the reporter in vitest.config.ts:
// vitest.config.ts
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
reporters: ['json'],
}
}){
"scripts": {
"test": "vitest run | test-results-to-sqlite ./dev-logging.db"
}
}Usage with Jest
{
"scripts": {
"test": "jest --json | test-results-to-sqlite ./dev-logging.db"
}
}Or output to file first:
{
"scripts": {
"test": "jest --json --outputFile=./test-output.json && test-results-to-sqlite ./dev-logging.db --input ./test-output.json"
}
}CLI Options
test-results-to-sqlite <db-path> [options]
Arguments:
db-path Path to SQLite database (created if not exists)
Options:
--input <file> Read JSON from file instead of stdin
--clear Clear all previous runs before inserting
--help Show helpSchema
CREATE TABLE test_runs (
id INTEGER PRIMARY KEY,
started_at TEXT NOT NULL,
duration_ms INTEGER,
total INTEGER,
passed INTEGER,
failed INTEGER,
skipped INTEGER,
success INTEGER NOT NULL
);
CREATE TABLE test_results (
id INTEGER PRIMARY KEY,
run_id INTEGER NOT NULL REFERENCES test_runs(id),
file TEXT NOT NULL,
test_name TEXT NOT NULL,
status TEXT NOT NULL,
duration_ms INTEGER,
error_message TEXT,
stack_trace TEXT
);Querying Results
# Get all failures from latest run
sqlite3 ./dev-logging.db "SELECT file, test_name, error_message FROM test_results WHERE status = 'failed' AND run_id = (SELECT MAX(id) FROM test_runs)"
# Get failure history for a specific test
sqlite3 ./dev-logging.db "SELECT r.started_at, tr.status, tr.error_message FROM test_results tr JOIN test_runs r ON tr.run_id = r.id WHERE tr.test_name LIKE '%should handle edge case%' ORDER BY r.started_at DESC"
# Get slowest tests
sqlite3 ./dev-logging.db "SELECT file, test_name, duration_ms FROM test_results WHERE run_id = (SELECT MAX(id) FROM test_runs) ORDER BY duration_ms DESC LIMIT 10"
# Recent run summary
sqlite3 ./dev-logging.db "SELECT started_at, total, passed, failed, success FROM test_runs ORDER BY started_at DESC LIMIT 5"Correlating Logs with Tests
This package integrates with pino-sqlite-plus to correlate log entries with test runs. See the pino-sqlite-plus README for setup instructions on automatic test context decoration.
Query API
import { createTestResultsQuery } from '@mosaic-code/test-results-to-sqlite'
const query = createTestResultsQuery('./dev-logging.db', 'logs')
// Get failed tests with their associated logs
const failedWithLogs = query.getFailedTestsWithLogs()
for (const { test, logs } of failedWithLogs) {
console.log(`Failed: ${test.test_name}`)
console.log(`Logs: ${logs.length} entries`)
}
// Get logs for a specific test
const logs = query.getLogsForTest(runId, testFile, testName)
// Get all logs for a test run
const runLogs = query.getLogsForRun(runId)
// Get error-level logs for failed tests
const errorLogs = query.getErrorLogsForFailedTests(runId, 50)
query.close()CLI Usage
# Show failed tests with their correlated logs
test-results-to-sqlite query ./dev-logging.db --failed
# Filter by specific run
test-results-to-sqlite query ./dev-logging.db --failed --run 123
# Output as JSON for LLM consumption
test-results-to-sqlite query ./dev-logging.db --failed --format jsonExpected Logs Table Schema
The correlation expects a logs table (default: logs) with these optional correlation columns:
| Column | Type | Description | |--------|------|-------------| | test_run_id | TEXT | Identifier matching your test run | | node_env | TEXT | Node environment (development, test, production) | | time | INTEGER | Log timestamp (ms since epoch) | | level | TEXT | Log level (e.g., "50-error") | | msg | TEXT | Log message | | data | JSON | Full log data |
License
Do No Harm License
