Initial Commit
This commit is contained in:
34
packages/api-stack/api/api-openapi/tests/setup/polyfills.ts
Normal file
34
packages/api-stack/api/api-openapi/tests/setup/polyfills.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Test-environment setup for @suaveplan/api-openapi.
|
||||
*
|
||||
* Wires a real in-memory OpenTelemetry `MeterProvider` BEFORE any source
|
||||
* module is imported. Tests that inspect emitted metrics can read them
|
||||
* from `testInMemoryMetricExporter`. Node ≥18 exposes `crypto.subtle`
|
||||
* and `crypto.getRandomValues` globally, so no crypto polyfill needed.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user