grafana / k6-template-typescript

Template to use TypeScript with k6

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add other lifecycle functions to examples

liambutler opened this issue · comments

Hi folks,

I'm having a great time using K6. Has been a big help in diagnosing some bottlenecks in my team's application. It's a beast!

Our K6 tests have traditionally been written in JS, but I'd like to convert them to TypeScript in line with the rest of our code. That's when I stumbled upon this useful repo.

However, my tests are more complex than the examples in this repo's /src folder. They use a setup() step, which then passes an object to the main function. I'm getting a lot of TS errors

Screenshot 2023-04-11 at 16 00 52

Admittedly I'm not the most proficient in TS, but I imagine that this would be helpful for others as well.

Hi @liambutler,

I am pretty sure your problem is that you try to define setup as arrow function .. or you can but you need to export it as a variable.

This has nothing to do with TS or k6 to be honest.

In your case you can do:

export function setup(): {token: string; users: any[]} {
  return {token:"some", users: []}
}

Or

export let setup = (): {token: string; users: any[]} => {
  return {token:"some", users: []}
}

Thanks @mstoykov. That was something copilot suggested after multiple attempts 😅

I still believe that having more in-depth examples would make this repo even more helpful than it already is

I am okay with merging some more examples or enhancement for the current ones.

But this clearly was wrong syntax which I don't see how an example would've really helped with.

The screenshot you provided is only what your editor (vscode?) provide.

If you run the script it will tell you

Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /opt/g/src/post-file-test.ts: Unexpected token, expected "{" (13:7)

  11 | };
  12 |
> 13 | export setup(): { token: string; users: any[] } => {

Which just like any other syntax error there isn't really anything this project can do.

Additionally, as you are using copilot it likely would just generate this code again for you, and you likely will have the same problem even if we did have examples.

This literally is you export a named function in typescript.

It would be useful to separate out the two issues here:

  1. I am unfamiliar with TypeScript and my syntax was wrong. The example I provided was one of several different attempts (both with () => and function syntax). Thanks again for the helpful explanation, by the way.
  2. The test examples in this repo are basic and don't show any type definitions other than the default function returning void

Had there been an example of setup() along with some type definitions, I would have had a better idea of the correct syntax.

I don't wish to annoy you with this. I just saw an opportunity for this repo to become more helpful for people who are new to K6. Of course, you are free to disregard this.