Install Firebase SDK as external library


I have k6 project created from k6 typescript template and I want to include firebase SDK package as external library.
Running npx webpack shows no error but running k6 itself fails at this error:

time="2021-11-02T11:28:32Z" level=error msg="file:///src/dist/firebaseUsers.js: Line 4303:15 Unexpected token function (and 4 more errors)"

Looking at code problem seems to be async keyword (there is async function). I have googled that async is implemented in ES6+ plus, so I have tried to change TypeScript/babel transpilation config option target(k6-template-typescript/tsconfig.json at 848c76e4c91a1efa84de89c06612af56e2958be2 · grafana/k6-template-typescript · GitHub) to es6. Also I am setting --compatibility-mode=extended when doing k6 run.

Does it mean that async functions are not supported by k6 itself? I am bit confused that there is k6 es6 template then, but maybe it means that k6 supports part of es6 standard, but not async functions.

Thanks for answer, I am quite javascript/ecmascript/typescript/babel/webpack begginer :slight_smile:

Hi @arteal, welcome to the forum :tada

k6 supports es5+ natively (read the JS VM goja we use is supporting this features) and ES6+the exponentiation operator (**) but without generators (I think) through babel for (at this time) classes, super and the import/export syntax. This changes constantly as we got Promise implemented in goja a few weeks ago and are still not using it at this point and also template literals were also supported through babel, but are now supported internally in goja.

async/await was added in ECMAScript 2017 and ES6 is technically ECMAScript 2015, this is when they decided to move to yearly releases and start naming it after the year it was released.

async/await (and generators for that matter) are a bit special and can’t just be transpiled by babel (or something else) they need to have support in the JS VM, which it currently doesn’t. Hopefully, this will change soon, but until then using async will not be possible.

The target as far as I am aware is to what ECMAScript version to try to transpile to, but in this case, again this doesn’t matter as you can’t just transpile await/async.

Using big (I see that one js file I got is 700kb)js libraries for load testing is not a good idea as each VU will need to load and execute it, which I would expect that even if it works will add 2+ MB of memory usage per each VU.

I would recommend that (given what firebase is being used for) to either completely call firebase outside the script or add a few endpoints to a server to do what you need with it and call those from k6.

Extensions is also a possibility, but IMO it will take way longer and will likely still have problems.

I have no idea what the firebase API looks like but if it’s a fairly well-documented REST API and you only need a few calls you need you can implement the relevant parts by hand in just pure k6 instead of pulling a whole ECMAScript.

Hope this helps you somewhat and please write back with your solution if you come to one :wink:

1 Like