运行k6时出现引用错误:regeneratorRuntime未定义。

3

我之前运行了k6,但现在每次尝试运行测试都会出现这个错误:ReferenceError: regeneratorRuntime未定义。

有人建议我安装和导入babel,但是并没有解决问题。

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",
    });
};

我只希望我的负载测试能够正常工作!

编辑:

我正在使用"@babel/core": "^7.4.0",

而我的babel.rc文件如下:

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

你的 Babel 版本是多少?请同时发布你的 .babelrc 或 babel.config.js 文件。 - Yury Tarabanko
我更新了帖子,加入了那个信息。谢谢你提醒我添加它。 - Daniel Greene
1个回答

1
我看到您正在使用 Promises,但目前的 k6 没有 事件循环。因此您的代码将无法工作,babel 也很遗憾不能在这里提供帮助 :(。
另外,您正在尝试使用 axios,但您没有导入它,并且它也(可能)无法工作,因为它既不是浏览器也不是 node.js;)。
要使其正常工作,您只需要使用 k6 的 http 库而不是 axios 来获取授权,并且不使用 async。此外,global 是特定于 node.js 的 AFAIK>。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接