Search code examples
reactjstestingload-testingk6

Reference error when running k6: regeneratorRuntime is not defined


I had k6 up and running, but now every time I try and run a test I am getting this error: ReferenceError: regeneratorRuntime is not defined.

I have tried installing and importing babel, which some people have suggested, but it did not work.

import http from "k6/http";
import { check } from "k6";

async function registerHandlers() {
    const dataForBody = 'client_id=LoadTesting&grant_type=client_credentials&' +
      `scope=${encodeURI('StitchApi')}&` +
      `client_secret=${encodeURI(process.env.REACT_APP_CLIENT_SECRET)}`;
    const messageHeaders = {
      'Content-Type': 'application/x-www-form-urlencoded',
    };
    axios({
      method: 'post',
      url: process.env.REACT_APP_STITCH_AUTH_URL,
      headers: messageHeaders,
      data: dataForBody,
    }).then((response) => {
        return response;
    }).catch((error) =>
      global.console.log('axios error: ', error)
    )
  }
// const queries = JSON.parse(open("./easygraphql-load-tester-queries.json"));
const url = "https://mywebsite.net/Api/GraphQL/";
const authorization = registerHandlers();
console.log("AUTH!!!!!!", authorization);
const payload = JSON.stringify({
    query: `
    {
        student(id: "5asdfasdfasdfasdf") {
        name
        }
    }
    ` });
const params = {
    headers: {
        "authorization": "asdfasdfasdfasdfasdfasdfasdf",
        "content-type": "application/json",
    }
}

export default function () {
    // console.log('query: ', queries);
    let res = http.post(url, payload, params);
    check(res, {
        "status is 200": (r) => r.status === 200,
        "is authenticated": (r) => r.json().authenticated === true,
        "is correct user": (r) => r.json().user === "user",
        "caption is correct": (r) => r.html("h1").text() == "Example Domain",
    });
};

I just want my load testing to work!

EDIT:

I am using "@babel/core": "^7.4.0",

and my babel.rc file looks like:

{
  "presets": [ "es2015", "stage-0" ]
}

Solution

  • I see that you are using Promises, but k6 at the moment doesn't have an event loop. So your code won't be able to work and babel unfortunately can't help here :(.

    Additionally you are trying to use axios, but you don't import it, and it will also (probably) not work as it won't have support for k6, given it is neither a browser nor node.js ;).

    All you need to get it working though is to use k6's http library instead of axios to get your authorization and to not use async. Also global is node.js specific AFAIK>