# Custom Counter and Rate for k6chaijs expect assertions

**URL:** <https://community.grafana.com/t/custom-counter-and-rate-for-k6chaijs-expect-assertions/97729>\
**Category:** OSS Support\
**Created:** [May 3, 2023, 2:26am UTC](https://community.grafana.com/t/custom-counter-and-rate-for-k6chaijs-expect-assertions/97729 "2023-05-03T02:26:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![icedlatte](https://avatars.discourse-cdn.com/v4/letter/i/eb9ed0/32.png) [@icedlatte](https://community.grafana.com/u/icedlatte)\
**Post date:** [May 3, 2023, 2:26am UTC](https://community.grafana.com/t/custom-counter-and-rate-for-k6chaijs-expect-assertions/97729/1 "2023-05-03T02:26:02Z")

</div>

Hi!

I have read from the docs that `k6chaijs` has better error handling than `checks` so I’m using describe - expect in my k6 frontend tests.

What would be a proper way to create a custom metric for failing expects, count of total tests executed and use this as threshold (to make sure that expects were actually executed), just like the check would? [Thresholds](https://k6.io/docs/using-k6/thresholds/#failing-a-load-test-using-checks)

In this sample snippet, I would need to place this inside a try-catch block because otherwise, I would get

```auto
ERRO[0015] Uncaught (in promise) Error
running at ue (https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:2:1165(57))
frontendat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:4:55024(125)

```

That also means that the catch block becomes the catch all for any error, like navigation timeout, in theory.

```auto
  describe('Sample describe block', async () => {
    try {
      expect(1, `1`).to.equal(2);
    } catch (error) {
      console.error(error);
    } finally {
// this is run on k6 browser 
      await page.close();
      await browser.close();
    }
  });

```

I would like to use these in my thresholds and define whether or not to abort the run if the expects have been failing.

Thanks in advance for the guidance!

---

<div class="post-metadata">

**Author:** ![imiric](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/imiric/32/36245_2.png) [@imiric](https://community.grafana.com/u/imiric)\
**Post date:** [May 9, 2023, 11:28am UTC](https://community.grafana.com/t/custom-counter-and-rate-for-k6chaijs-expect-assertions/97729/5 "2023-05-09T11:28:03Z")

</div>

Hi there!

Within your `catch` block you can determine whether the thrown object is an assertion error, and if so, change the value that is added to the metric.

Something like this:

```javascript
import { chai, describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.0/index.js';
import { Rate } from 'k6/metrics';

const someAssertionMetric = new Rate('someAssertion');

export let options = {
  thresholds: {
    someAssertion: [
      { threshold: 'rate > 0.9', abortOnFail: true, delayAbortEval: '10s' },
    ],
  },
};

export default function testSuite() {
  describe('Dummy example', () => {
    let assertionPassed = true;
    try {
      expect(Math.floor(Math.random () * 5) + 1).to.equal(1);
    } catch (e) {
      if (e instanceof chai.AssertionError) {
        console.log(e.message); // e.g. "expected 4 to equal 1"
        assertionPassed = false;
      }
    }
    someAssertionMetric.add(assertionPassed);
  });
}

```

You can use the metric in a threshold just like any other metric.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![icedlatte](https://avatars.discourse-cdn.com/v4/letter/i/eb9ed0/32.png) [@icedlatte](https://community.grafana.com/u/icedlatte)\
**Post date:** [June 5, 2023, 6:25am UTC](https://community.grafana.com/t/custom-counter-and-rate-for-k6chaijs-expect-assertions/97729/7 "2023-06-05T06:25:19Z")

</div>

thanks for this @imiric ! apologies for the delayed response as i have been working on a different project recently. this helps!
