Skip to content

Commit fcd516e

Browse files
authored
docs: update test-consumer example with count value (#26)
1 parent db52173 commit fcd516e

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

src/content/docs/reference/test-consumer.md

+31-32
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,53 @@
11
---
2-
title: '@metrics/test-consumer'
2+
title: "@metrics/test-consumer"
33
---
44

55
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.
6-
It takes a metric stream generated by @metrics/client and makes the collected metrics available as an array.
6+
It takes a metric stream generated by [@metrics/client](/reference/client/) and makes the collected metrics available as an array.
77

8-
⚠️ 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/)
8+
⚠️ You should _never_ use this in production code, however it is very convenient when writing tests which produce metrics using `@metrics/client`.
99

1010
## Examples usage
1111

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

1414
```js
15-
const test = require('tap');
16-
const Metrics = require('@metrics/client');
17-
const TestConsumer = require('@metrics/test-consumer');
18-
19-
test('some test case', async () => {
20-
const metrics = new Metrics();
21-
// This sets up the metrics client to be used
22-
const testHelper = new TestConsumer(metrics)
23-
// .start sets up the stream
24-
testHelper.start();
25-
26-
// Code which triggers a count metric
27-
28-
testHelper.stop(); // .stop ends the streams and now we can get the result.
29-
30-
testHelper.getResults().then(result => {
31-
t.equal(result.name, "some_counter"); // Validate our metrics was collected
32-
res.labels.forEach((metricLabel) => {
33-
if (metricLabel.name === "value") {
34-
t.equal(metricLabel.value, 2); // We expect two counts to have happened
35-
}
36-
});
37-
});
15+
const assert = require("node:assert");
16+
const { test } = require("node:test");
17+
const MetricsClient = require("@metrics/client");
18+
const TestConsumer = require("@metrics/test-consumer");
19+
20+
test("some test case", async () => {
21+
// Pass in the metrics client you want to consume.
22+
const metrics = new MetricsClient();
23+
const testHelper = new TestConsumer(metrics);
24+
// Sets up the consumer to capture events.
25+
testHelper.start();
26+
27+
// 👋 Your code which produces some kind of metric goes here.
28+
29+
// Ends the streams, now we can get the result.
30+
testHelper.stop();
31+
32+
const result = await testHelper.getResults();
33+
const metric = result.find((m) => m.name === "some_counter");
34+
35+
assert.ok(metric, "Expected to find metric some_counter");
36+
assert.equal(metric.value, 2);
3837
});
3938
```
4039

4140
## API
4241

43-
- [constructor](#constructormetrics)
44-
- [start](#start)
45-
- [stop](#stop)
46-
- [getResults](#async-getresults)
42+
- [constructor](#constructormetrics)
43+
- [start](#start)
44+
- [stop](#stop)
45+
- [getResults](#async-getresults)
4746
- [createMetrics](#createmetrics)
4847

4948
### constructor(metrics)
5049

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

5352
### .start()
5453

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

6160
### async .getResults()
6261

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

6564
### createMetrics
6665

0 commit comments

Comments
 (0)