-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JS - Optional chaining = syntax error #2168
Comments
@thim81, optional chaining was added to the standard fairly recently, in ECMAScript 2020 it seems, and is not yet supported by k6. We don't use V8 or any of the other browser runtimes to execute JS code. Instead we use goja, a JS runtime written in Go, which unfortunately implements only some parts of the JS standards after ECMAScript 5.1. For big parts of the rest, k6 can automatically transpile scripts with Babel by default, but we haven't updated the version of the bundled Babel in a while, so it doesn't support optional chaining either... 😞 So, until either goja supports it natively (which is the preferred approach in terms of performance) or we update the bundled Babel (unlikely to happen soon, because it will probably cause other issues), the only way to get support for this would be to manually transpile your code yourself before you run it with k6. You can see this project as an example way to do this: https://github.com/grafana/k6-template-es6 |
@na-- Shall I close the issue, since it is not something that can be resolved in a simple manner? |
@thim81, you can leave the issue open, so that other people who stumble on the same problem can more easily find it. We will close it when we add support for this JS feature in k6. Most likely when goja adds support for it and we update our dependency for it in k6. I don't think the goja author is planning to work on this specific thing any time soon, but he might accept a good PR if someone is interested in contributing it... 🤷♂️ Alternatively, they are some other potential means for getting this in k6 in the future. It's very unlikely we'll update the bundled Babel in k6 any time soon, but, for example, in the future we might switch to using esbuild to automatically transpile newer JS features to something that goja and k6 understand 🤷♂️ That has its own downsides and problems, so it's not a guarantee, just a potential option down the line. |
From my very fast check (by making |
FWIW, the latest goja version and k6 |
When using JS that has the optional chaining syntax, K6 throws a Syntax error:
Environment
Expected Behavior
K6 understands the optional chaining "?." syntax and threats the "if (jsonData?.id) {" as "if (jsonData && jsonData.id) {"
Actual Behavior
K6 throws an error:
ERRO[0000] SyntaxError: file:///testing/k6/test.js: Unexpected token (6:13)
Steps to Reproduce the Problem
Enter a script with an IF condition
The text was updated successfully, but these errors were encountered: