Skip to content

Commit

Permalink
Merge pull request #18 from grafana/add-exit-on-error
Browse files Browse the repository at this point in the history
Implement exitOnError config option
  • Loading branch information
2Steaks authored Sep 5, 2024
2 parents 4b2617e + a62e59a commit 8eddf2d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
8 changes: 4 additions & 4 deletions build/k6chaijs.min.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/assert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { check } from 'k6';
import exec from 'k6/execution';
import { Assert, AssertionArgs } from './types';
import chai from './config';
import { isFunction, regexTag, truncate } from './utils';
Expand Down Expand Up @@ -148,6 +149,10 @@ export function assert(): Assert {
console.warn(truncatedExpectation);
}

if (chai.config.exitOnError) {
exec.test.abort(truncatedExpectation);
}

throw new chai.AssertionError(
truncatedExpectation,
error,
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chai from 'chai';
import {
AGGREGATE_CHECKS,
EXIT_ON_ERROR,
LOG_FAILURES,
TRUNCATE_VARIABLE_THRESHOLD,
TRUNCATE_MSG_THRESHOLD
Expand All @@ -12,6 +13,7 @@ chai.config.truncateVariableThreshold = TRUNCATE_VARIABLE_THRESHOLD; // individu
chai.config.truncateMsgThreshold = TRUNCATE_MSG_THRESHOLD; // whole check() message must be below X chars.
chai.config.aggregateChecks = AGGREGATE_CHECKS; // the {#this} and {#exp} are not interpolated to aggregate checks.
chai.config.logFailures = LOG_FAILURES; // console.warn(full_message)
chai.config.exitOnError = EXIT_ON_ERROR;

chai.Assertion.addMethod('anonymize', anonymize);
chai.Assertion.addMethod('validJsonBody', validJsonBody);
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const AGGREGATE_CHECKS = true;
export const EXIT_ON_ERROR = false;
export const LOG_FAILURES = false;
export const TRUNCATE_VARIABLE_THRESHOLD = 100;
export const TRUNCATE_MSG_THRESHOLD = 300;
1 change: 1 addition & 0 deletions src/custom-typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare namespace Chai {
truncateMsgThreshold: number;
aggregateChecks: boolean;
logFailures: boolean;
exitOnError: boolean;
}

export interface ChaiUtils {
Expand Down
21 changes: 21 additions & 0 deletions tests/exit-on-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import chai, { describe, expect } from '../build/k6chaijs.min.js';
import { sleep } from 'k6';

// Set exitOnError=true to exit k6 on the first failed check.
chai.config.exitOnError = true;

export let options = {
iterations: 3
};

export default function testSuite() {
let fakeResponse = {
status: 401
};

describe('Testing check aggregation', () => {
expect(fakeResponse.status, 'response status').to.equal(200);
});

sleep(1);
}

0 comments on commit 8eddf2d

Please sign in to comment.