Check for condition and exit for any test execution

I want to k6 if an ENV is not set.

I do the following:
export function setup() {

if(XYZ == undefined) {
    abort('XYZ needs to be defined')


I know for sure that XYZ is undefined - not sure why its not aborting and still runs the group

Hi @steven.chaves,
welcome to the community forum :tada:

It seems you’re using a bad syntax for invoking abort. It is part of the k6/execution API so you have to import it and use it invoking test.abort as defined in the documentation.

import exec from 'k6/execution';

export const options = {

export default function () {
  exec.test.abort('XYZ needs to be defined')

On top of @codebien words, please find an example below:

// aborts.js
import exec from "k6/execution";

if (__ENV.XYZ === undefined) {
   exec.test.abort("please provide XYZ");

export default function () {
   console.log("hey ", __ENV.XYZ);

and running this should fail

k6 -i 1 -u 1 run abort.js

and this one not:

XYZ=lorem k6 -i 1 -u 1 run abort.js

Let us know if that answers!


1 Like

Anyway that k6 Support team can add this example in the API docs because its not clear really how to use it at all. Thanks for the help

we have an example on the same page I linked before. Check it here: k6/execution
We are happy to get some suggestions for improving the documentation. Please, can you explain with more accuracy what part you think we should improve to make it better?