Files
2026-05-27 23:47:53 +01:00

37 lines
1.2 KiB
TypeScript

/**
* Test setup for @suaveplan/better-auth-surrealdb.
*
* Wires a real in-memory OpenTelemetry `MeterProvider` BEFORE any source
* module is imported so the meter resolved at module-load (well, at first
* call given the lazy-init pattern) is the real provider's meter.
*
* Node ≥18 + Bun expose `crypto.subtle` / `crypto.getRandomValues` globally,
* so no crypto polyfill is required.
*/
import { metrics } from "@opentelemetry/api";
import {
AggregationTemporality,
InMemoryMetricExporter,
MeterProvider,
PeriodicExportingMetricReader,
} from "@opentelemetry/sdk-metrics";
export const testInMemoryMetricExporter = new InMemoryMetricExporter(
AggregationTemporality.CUMULATIVE
);
const reader = new PeriodicExportingMetricReader({
exporter: testInMemoryMetricExporter,
exportIntervalMillis: 60_000,
});
export const testMeterProvider = new MeterProvider({ readers: [reader] });
metrics.setGlobalMeterProvider(testMeterProvider);
export async function flushTestMetrics() {
await testMeterProvider.forceFlush();
const collected = testInMemoryMetricExporter.getMetrics();
testInMemoryMetricExporter.reset();
return collected;
}