autotel
Version:
Write Once, Observe Anywhere
73 lines (72 loc) • 1.97 kB
JavaScript
// src/metric-testing.ts
function createMetricsCollector() {
const events = [];
const funnelSteps = [];
const outcomes = [];
const values = [];
return {
getEvents() {
return [...events];
},
getFunnelSteps() {
return [...funnelSteps];
},
getOutcomes() {
return [...outcomes];
},
getValues() {
return [...values];
},
clear() {
events.length = 0;
funnelSteps.length = 0;
outcomes.length = 0;
values.length = 0;
},
recordEvent(event) {
events.push(event);
},
recordFunnelStep(step) {
funnelSteps.push(step);
},
recordOutcome(outcome) {
outcomes.push(outcome);
},
recordValue(value) {
values.push(value);
}
};
}
function assertEventTracked(options) {
const events = options.collector.getEvents();
const matching = events.filter((e) => e.event === options.eventName);
if (matching.length === 0) {
throw new Error(`No events found with name: ${options.eventName}`);
}
if (options.attributes) {
const matchingWithAttrs = matching.filter(
(e) => Object.entries(options.attributes).every(
([key, value]) => e.attributes && e.attributes[key] === value
)
);
if (matchingWithAttrs.length === 0) {
throw new Error(
`Event ${options.eventName} found but attributes don't match: ${JSON.stringify(options.attributes)}`
);
}
}
}
function assertOutcomeTracked(options) {
const outcomes = options.collector.getOutcomes();
const matching = outcomes.filter(
(o) => o.operation === options.operation && o.status === options.status
);
if (matching.length === 0) {
throw new Error(
`No outcomes found with operation: ${options.operation} and status: ${options.status}`
);
}
}
export { assertEventTracked, assertOutcomeTracked, createMetricsCollector };
//# sourceMappingURL=chunk-5ZN622AO.js.map
//# sourceMappingURL=chunk-5ZN622AO.js.map