-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Support for writing tests in TypeScript #422
Comments
I'm definitely open to this, but someone would have to PR a change to |
I think this should be pre-processed via the installed TypeScript compiler on the host instead of bundling the TS compiler inside k6. A first check to detect if the script passed is The TypeScript definition file would simply help the experience writing the tests in code editors. I'd like to have a look at this. |
I've spent some time on it (using tsc as a preprocessor). I'll describe what I've done, in case you are interested: In my case, merchant-api.ts is a library generated from a Swagger api definition using a code gen (or at least will be, because right now it is just a placeholder that exports a type and a class, later I will tweak it so that the library is the one actually calling k6's http functions) I got it working, but the import statement is a little messed up, so I need to manually replace "./merchant-api" with "./merchant-api.js" in the js before k6 accepts it (otherwise I get ERRO[0000] GoError: open /Users/lars.holmberg/git/k6/merchant-api: no such file or directory)
I run it this way (you also need a tsconfig.json and lots of other stuff, probably also
|
The import change is on purpose, because otherwise we open for a distinction between how relative imports work locally vs remotely (you can |
Makes sense. I'm actually quite ok with the way it works now. Type info would still be nice (and higher prio than adding ts support in js/compiler imho), but because I can force TS to accept the un-typed calls, this is not blocking me any more (I have two full days of TS experience now :) |
So one thing that I could not get to work was making my main test file a ts file. The imports for k6 and http won't work in TS unless you disregard the TSC failure, as it still emits the js files. I ended up making my main test file a js file to get around this, and additional modules I created to support testing were ts files. This allowed my unit tests to target my modules, and everything seemed to work okay. One complication I did run into though, which isn't really solvable I think loading k6/http and check from other ts modules same issue there, and also would break the tests. I wanted to separate the low level test steps from the test file, for reuse in standard integration tests. |
liclac wrote:
This seems like a show stopper for our case. I tried to make my main script file in JS, but due to complex login system I needed to import our libraries written in TypeScript to login in tests. These libraries are added via NPM. It's not enough to compile TS modules to JS via TSC because I would have to manually add ".js" in all the used TS modules, which is virtually impossible as the import dependencies goes rather deep. I started adding ".js" manually but at some point the dependencies go to code that is out of my control. And then I get the error ERRO[0001] GoError "The system cannot find the file specified." or ERRO[0008] GoError: "The file couldn't be found on local disk". The bottom line is: k6 doesn't work even with with JS modules, unless the JS modules (including all imports in the chain) are maintained by you so that you can manually change the imports in the code. |
Can I just add my voice to the others who are asking for this feature. Great idea! |
A |
Looks like there is a good example of typescript integration here: https://github.com/go-automate/k6-typescript-framework Edit: see also: https://medium.com/better-programming/performance-testing-in-typescript-5b5444510b83 |
There is a project template at https://github.com/k6io/template-typescript And the TypeScript definition https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/k6 @mstoykov I think this issue can be closed. If anyone wants any specifics, I suggest creating a new issue. |
Looks good! What about adding a link to this template from the documentation? |
Yes!, It is on Examples page and soon on the Modules page -> grafana/k6-docs#123 Hopefully, users can find the project template. I will figure out if there are other places to add it. |
I am closing this as well then. Thanks @ppcano |
I am just wondering, how do I run the K6 tests without using webpack? After running |
It would be really nice to be able to write test cases in TypeScript instead of plain javascript. I miss static typing/compile time checks.
I think this could be accomplished by just creating TS declaration files for the k6 functions that are exposed to the test and publishing them to https://github.com/DefinitelyTyped/DefinitelyTyped
Some more context: We’re looking at being able to use code for API calls based on code generated from a Swagger API definition, so the test case can call something like:
customer.addOrder(“pizza”, 1)
instead of
http.post(“http://some-api-endpoint/customer/orders“, { “name”: “pizza”, “quantity”: 1 })
We could of course generate pure javascript but then that doesn't give us type checks or anything like that, so we wouldnt get IDE support & it would be easier to make mistakes.
The text was updated successfully, but these errors were encountered: