K6chaijs: expected() placeholders ${this} and ${actual} are not replaced automatically

Hi, I’ve noticed that the “Automatic messages” of chai are not working in k6chaijs.
I’m using version

The placeholders of the expected() ${this} and ${actual} are not being replaced automatically and instead this is shown:

Also, this issue seems to have been present even in the documentation available here: expect() (k6.io)

Where this expectation:

expect([1, 2, 3]).to.have.lengthOf(3);

Is translated to:

   ✓ expected ${this} to have a length of 3 got ${actual}

Actual behavior should be the same as presented here: Using chai with k6

Hi @bastarnae!
Welcome to the forum!

I think this behavior was intentionally disabled since (It works as you want in in order to get in the summary this:

     █ Testing check aggregation

       ✗ expected response status to equal 200
        ↳  10% — ✓ 2 / ✗ 8

instead of

     █ Testing check aggregation

       ✗ response status: expected 199 to equal 200
        ↳  0% — ✓ 0 / ✗ 6
       ✓ response status: expected 200 to equal 200
       ✗ response status: expected 201 to equal 200
        ↳  0% — ✓ 0 / ✗ 2

If you want to get the second format anyway with version, you can do this:

import chai, { describe, expect } from "https://jslib.k6.io/k6chaijs/";

chai.config.aggregateChecks = false;

or, if you prefer the first format, but you want to see the errors, then you can use this:

import chai, { describe, expect } from "https://jslib.k6.io/k6chaijs/";

chai.config.logFailures = true;

I hope this is a help for your issue.

1 Like

Hi @bandorko , thanks for having me in the forum!

Also, thank you for your detailed reply, both variants are helpful.

Seems that there are some discrepancies when having:

chai.config.aggregateChecks = false;

For example, the below expectations:

const expected = {
     Message: 'OK'

    expect(JSON.parse(arrResp.body), 'response body').to.deep.equal(expected);


But when the:

chai.config.aggregateChecks = true;

It shows:

But I guess both messages are ok, just the layout seems different.

You can see the real difference between the two variant, if you run the test more than one iterations, and when the expression in expect() gets different values through the iterations.

The second format would create new line for every different value.