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

Add other lifecycle functions to examples #36

Open
liambutler opened this issue Apr 11, 2023 · 4 comments
Open

Add other lifecycle functions to examples #36

liambutler opened this issue Apr 11, 2023 · 4 comments

Comments

@liambutler
Copy link

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.

@mstoykov
Copy link
Contributor

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: []}
}

@liambutler
Copy link
Author

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

@mstoykov
Copy link
Contributor

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.

@liambutler
Copy link
Author

liambutler commented Apr 13, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants