Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

load testing - SyntaxError when using yield in K6

I am trying to use javascript generator and yield in k6.

When I try to run the script I get this error:

SyntaxError: ...yield is a reserved word

Is it possible to use yield in k6?

question from:https://stackoverflow.com/questions/65843697/syntaxerror-when-using-yield-in-k6

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unfortunately this is not natively supported in the JavaScript VM used by k6 (goja). According to this comment generators might eventually be supported, but there's no current plans for it.

That said, you can workaround this by using the template-es6 project to transform your script to an ES5 variant with Babel, which can polyfill support for generators.

  1. First clone the template-es6 Git repo locally.

  2. Install all dependencies with yarn add or npm install.

  3. Add @babel/plugin-transform-runtime to the plugins list in the .babelrc. It should look like this:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "usage",
        "corejs": 3
      }
    ]
  ],
  "plugins": [
    "@babel/plugin-transform-runtime"
  ]
}
  1. Install the plugin with yarn add -D @babel/plugin-transform-runtime or npm install --save-dev @babel/plugin-transform-runtime.

  2. Modify the main.js script and install whatever other dependencies you need.

  3. Run npm run-script webpack to bundle everything.

  4. Finally run the script with k6 with k6 run --compatibility-mode=base build/app.bundle.js. You can also run it without --compatibility-mode=base, but since it's already transformed to an ES5 script you can avoid the extra transformation done by k6, which improves performance and memory usage.

Yeah, this is not as straightforward as we'd like it to be, but it should be familiar to JavaScript developers and we hope to improve support of these features in the future.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...