Files
suaveplan/packages/communication/messaging/imessage-adapter/tests/setup/polyfills.ts
2026-05-27 23:29:53 +01:00

39 lines
1.2 KiB
TypeScript

/**
* Test-environment setup for @suaveplan/imessage-adapter.
*
* Wires a real in-memory OpenTelemetry `MeterProvider` BEFORE any source
* module loads, so the meter resolved at module load is the real
* provider's meter (and not the noop fallback). Tests inspect emitted
* instruments by calling `flushTestMetrics()`.
*
* Server-tier (bun:sqlite required): no DOM, no browser globals.
*/
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(): Promise<
ReturnType<typeof testInMemoryMetricExporter.getMetrics>
> {
await testMeterProvider.forceFlush();
const collected = testInMemoryMetricExporter.getMetrics();
testInMemoryMetricExporter.reset();
return collected;
}