Skip to content
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

Closed
thim81 opened this issue Oct 8, 2021 · 5 comments · Fixed by #3456
Closed

JS - Optional chaining = syntax error #2168

thim81 opened this issue Oct 8, 2021 · 5 comments · Fixed by #3456

Comments

@thim81
Copy link

thim81 commented Oct 8, 2021

When using JS that has the optional chaining syntax, K6 throws a Syntax error:

ERRO[0000] SyntaxError: file:///testing/k6/test.js: Unexpected token (6:13)
  4 | 
  5 |     const test = {}
> 6 |     if (test?.test) {
    |              ^
  7 |         console.log('test')
  8 |     }
  9 |  at <eval>:2:28542(106) 

Environment

  • k6 version: k6 v0.33.0 ((devel), go1.16.5, darwin/amd64)
  • OS and version: MacOS Big Sur

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

import http from 'k6/http';

export default function () {

    const test = {}
    if (test?.test) {
        console.log('test')
    }

    let res = http.get('https://k6.io');
    console.log(JSON.stringify(res.headers));
}

@na--
Copy link
Member

na-- commented Oct 11, 2021

@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

@thim81
Copy link
Author

thim81 commented Oct 19, 2021

@na-- Shall I close the issue, since it is not something that can be resolved in a simple manner?

@na--
Copy link
Member

na-- commented Oct 20, 2021

@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.

@mstoykov
Copy link
Contributor

From my very fast check (by making ?. be equal to . in goja) babel does not recognise ?. as well and will stop us from using it the same way as in #824 (comment) :(

@na--
Copy link
Member

na-- commented Jan 17, 2022

FWIW, the latest goja version and k6 master (soon to be v0.36.0) now support this, but our old version of Babel doesn't, so it will work only in --compatibility-mode=base 😞 More details on our future plans: #2296 (comment)

@mstoykov mstoykov linked a pull request Jul 8, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants