How to use feature of fail( [err] ) to stop the iteration inside chaijs describe() block

expect() throws an exception inside a describe() block. pls see this code with describe()-expect()

export const options = {
vus: 1,
iterations: 1
};

import exec from ‘k6/execution’;
import { check, group, fail } from ‘k6’;
import { describe, expect } from ‘https://jslib.k6.io/k6chaijs/4.3.4.3/index.js’;

export default function testSuite() {

describe('first describe', () => {
    const iteration = exec.vu.iterationInScenario;
    expect(iteration, 'response status').to.equal(1);
    console.log(`passed on ${exec.vu.iterationInScenario} iteration`)
});
describe('second describe', () => {
    const iteration = exec.vu.iterationInScenario;
    expect(iteration, 'response status').to.equal(0);
    console.log(`passed on ${exec.vu.iterationInScenario} iteration`)
});

}

But fail( [err] ) throws an error which stops that particular iteration. See same code with group()-check()-fail()

export const options = {
vus: 1,
iterations: 1
};

import exec from ‘k6/execution’;
import { check, group, fail } from ‘k6’;
import { describe, expect } from ‘https://jslib.k6.io/k6chaijs/4.3.4.3/index.js’;

export default function testSuite() {

group('first describe', () => {
    const iteration = exec.vu.iterationInScenario;
    if(!check(iteration, { 'iteration equals' : ( ) => iteration == 1 })){
        fail('iteration is not 1')
    }
    console.log(`passed on ${exec.vu.iterationInScenario} iteration`)
});

group('second describe', () => {
    const iteration = exec.vu.iterationInScenario;
    if(!check(iteration, { 'iteration equals' : ( ) => iteration == 0 })){
        fail('iteration is not 0')
    }
    console.log(`passed on ${exec.vu.iterationInScenario} iteration`)
});

}

I want to stop the vu iteration when any expect() fails.
How can I achieve fail([err]) functionality with describe()-expect() ?

@lucky041295

describe() + expect() works as it is intended, because describe() catches the exceptions to allow the other describe() to run. If you want to prevent the other describe() to run, you should connect the describe() functions with && as the documentation suggests
https://k6.io/docs/javascript-api/jslib/k6chaijs/describe/

export const options = {
vus: 1,
iterations: 1
};

import exec from ‘k6/execution’;
import { check, group, fail } from ‘k6’;
import { describe, expect } from ‘https://jslib.k6.io/k6chaijs/4.3.4.3/index.js 1’;

export default function testSuite() {

describe('first describe', () => {
    const iteration = exec.vu.iterationInScenario;
    expect(iteration, 'response status').to.equal(1);
    console.log(`passed on ${exec.vu.iterationInScenario} iteration`)
}) &&
describe('second describe', () => {
    const iteration = exec.vu.iterationInScenario;
    expect(iteration, 'response status').to.equal(0);
    console.log(`passed on ${exec.vu.iterationInScenario} iteration`)
});
}
4 Likes