Exception raised in test GoError context cancelled

Hey!

Somehow I have started getting weird error which seems to be pointing to GO language in my performance scripts:

ERRO[0051] Exception raised in test "12 API Call". Failing the test and continuing. 
GoError: context canceled at API is 200 (file:///Users/path/to/k6test/file.js:1:1(0))  source=console

My script looks like this:

describe('12 API Call', (t) => {
    const response = http.get(Configuration.ApiName, data.data);
    check(response, {
      'API is 200': (r) => r.status === 200
    });
    t.expect(response.status).as('response status').toBeBetween(200, 299);

    sleep(1); 
  });

Options that I am passing look like this:

  loadTestNoAbort: {
    scenarios: {
      test_suite_one: {
        executor: 'constant-vus',
        vus: 10,
        duration: '20s',
        exec: 'testSuiteAll'
      }
    },
    thresholds: {
      http_req_duration: [
        {
          threshold: 'avg<2000', // if http req duration is avg over 2000, test aborts
          abortOnFail: false // abort test if thresholds breached use true
          // delayAbortEval: '5s' // delay of abort so data can be gathered in metrics
        }
      ],
      http_req_failed: [
        {
          threshold: 'rate<0.50', // During the whole test execution, the error rate must be lower than 1%.
          abortOnFail: false // abort test if thresholds breached use true
          // delayAbortEval: '5s' // delay of abort so data can be gathered in metrics
        }
      ]
    }
  }

Hi @sillymoomoo

The issue is probably related to mixing the checks and k6-chai-js APIs. The following line is not correct:

t.expect(response.status).as('response status').toBeBetween(200, 299);

If you are using k6-chai-js, use expect():

expect(response.status, 'response status').is.within(200, 299);

For example, the following script works well:

import { check } from 'k6';
import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.2/index.js'
import http from 'k6/http';

const url = 'http://test.k6.io/';
export default function () {
    describe('12 API Call', () => {
        const response = http.get(url);
        check(response, {
            'API is 200': (r) => r.status === 200
        });
        expect(response.status, 'response status').is.within(200, 299);
    });
}

I get a similar error if I replace the expect() line with what you have.
I would probably not mix the two, decide which option works best for you, and has the better behavior, and use that.

check(response, {
  'API is 200': (r) => r.status === 200
});

vs.

expect(response.status, 'response status').is.within(200, 299);

To decide based on your case, the following blog can help you understand the differences between the core API (check) and the newer k6-chai-js.

I hope this helps.

Cheers!

1 Like