Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 additions & 32 deletions src/content/docs/reference/test-consumer.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
---
title: '@metrics/test-consumer'
title: "@metrics/test-consumer"
---

This module is a memory based consumer for metrics streams to be used in tests. The purpose of the module is to make writing tests and asserting metrics easier.
It takes a metric stream generated by @metrics/client and makes the collected metrics available as an array.
It takes a metric stream generated by [@metrics/client](/reference/client/) and makes the collected metrics available as an array.

⚠️ You should _never_ use this in produciton code, however it is very convenient when writing tests which produce metrics using [`@metrics/client`](https://metrics-js.github.io/reference/client/)
⚠️ You should _never_ use this in production code, however it is very convenient when writing tests which produce metrics using `@metrics/client`.

## Examples usage

Below is a sample test showing how this could be used:

```js
const test = require('tap');
const Metrics = require('@metrics/client');
const TestConsumer = require('@metrics/test-consumer');

test('some test case', async () => {
const metrics = new Metrics();
// This sets up the metrics client to be used
const testHelper = new TestConsumer(metrics)
// .start sets up the stream
testHelper.start();

// Code which triggers a count metric

testHelper.stop(); // .stop ends the streams and now we can get the result.

testHelper.getResults().then(result => {
t.equal(result.name, "some_counter"); // Validate our metrics was collected
res.labels.forEach((metricLabel) => {
if (metricLabel.name === "value") {
t.equal(metricLabel.value, 2); // We expect two counts to have happened
}
});
});
const assert = require("node:assert");
const { test } = require("node:test");
const MetricsClient = require("@metrics/client");
const TestConsumer = require("@metrics/test-consumer");

test("some test case", async () => {
// Pass in the metrics client you want to consume.
const metrics = new MetricsClient();
const testHelper = new TestConsumer(metrics);
// Sets up the consumer to capture events.
testHelper.start();

// 👋 Your code which produces some kind of metric goes here.

// Ends the streams, now we can get the result.
testHelper.stop();

const result = await testHelper.getResults();
const metric = result.find((m) => m.name === "some_counter");

assert.ok(metric, "Expected to find metric some_counter");
assert.equal(metric.value, 2);
});
```

## API

- [constructor](#constructormetrics)
- [start](#start)
- [stop](#stop)
- [getResults](#async-getresults)
- [constructor](#constructormetrics)
- [start](#start)
- [stop](#stop)
- [getResults](#async-getresults)
- [createMetrics](#createmetrics)

### constructor(metrics)

Takes in the [@metrics/client](https://metrics-js.github.io/reference/client/) to collect metrics from.
Takes an instance of `@metrics/client` to collect metrics from.

### .start()

Expand All @@ -60,7 +59,7 @@ Ends the stream setup for the client, returns a Promise which resolves to an arr

### async .getResults()

Returns a promise which resolves to an array containing collected `[@metrics/metrics](https://metrics-js.github.io/reference/metric/)` objects.
Returns a promise which resolves to an array containing collected [@metrics/metric](/reference/metric/) objects.

### createMetrics

Expand Down