Can you pass strings from other classes

Hey,

I have a class :

commmon-variables.js

export class CommonVariables {
    baseUrl = 'https://website.com'
}

When i use this in a test:

// Call the CommonVariables to be used at RunTime

const commonVariables = new CommonVariables();
const BASE_URL = commonVariables.baseUrl;

export default function () {
    const url = `${BASE_URL}/api/123123`;

Code keeps returning the folllowing:

common-variables.js: Unexpected token (2:12)
1 | export class CommonVariables {

2 | baseUrl = ‘https:/wesbite.com’
| ^
3 | }
4 |
at <internal/k6/compiler/lib/babel.min.js>:2:28536(109)
at <internal/k6/compiler/lib/babel.min.js>:14:24413(13)
at bound (native)
at s (<internal/k6/compiler/lib/babel.min.js>:1:1331(8))
at native hint=“script exception”

Hi @JayBird, welcome to the community forum :tada:

The syntax

class CommonVariables {
    baseUrl = 'https://website.com'
}

Is not supported by k6 at the moment. Actually this syntax is still not … released - it will be officially supported in ES2022 or at least that is the plan.

While this likely works in all modern browsers and newer versions of nodejs as this has been in development for years, k6 does not support the latest (of all) javascript.

You can probably use babel to transpile down to older version of JS, but for your particular case I would suggest to just not use a class but as simple object as in:

export let CommonVariables{
    baseUrl: 'https://website.com'
}

this laso lets you skip making an instance of the class with new and … IMO abusing classes :wink:

Hope this helps you

Thanks @mstoykov . I will defiantly give this ago.

Am I to take it then, that if I had some other classes that ahd helper functions in them, I am going to have the same issue as well accessing them?

If so coul you point me in the right direction on what to read up with regards to Babel.

Thanks

Am I to take it then, that if I had some other classes that ahd helper functions in them, I am going to have the same issue as well accessing them?

AFAIK the new functionality is strictly about fields, not functions, but in practice k6 has support for some class syntax through a fairly old internal babel version whcih may or may not support w/e syntax you give it. To put it bluntly I would argue against using classes unless you have to and if you have to, probably using an external version of babel to transpile them will save you a few headaches.

We do for example know of some things that our babel version should support, but doesn’t - I have no idea how many of those there are and in I general find classes in javascript to be strange - just use prototypes :wink: