I am new to k6 and JS. I was wondering what the normal development and debugging workflow looks like for developing new test scripts? While I can write the complete script and then run it using k6, I wanted to learn how I can test small parts of the scripts frequently and easily.
For example,
import http from "k6/http";
export default function() {
let res = http.get("https://google.com");
console.log(res.status)
};
I would like to be able to run this code and log the response code without initiating a load test. I am currently using Atom with the script package. When I try to run the above code, I get the following error:
Error: Cannot find module ‘k6/http’
Would love to learn how folks go about developing and testing new scripts!
Since k6 is neither compatible with NodeJS nor runs on it, you cannot run this code outside the k6 environment. There are two main reasons for this:
The “k6/*” is a k6 internal API, which will only work when you run the script with k6.
k6 uses Goja as JS engine, and only ES5.1+ syntax is compatible with it.
You can run your test with k6 as is, in order to be able to debug them. It will perfectly show console output, when run locally. Also, you don’t need to run it with thousands of VUs to figure this out, just go with 1 VU (which is the default).
For starting to work with k6, I highly recommend you to have a look at the documentation: Running k6
Okay, I guess the best solution is to just stick with k6 run script.js -u 1 --iterations 1 for now?
That’s how I usually do it while I write my tests, just to ramp up VUs and duration additionally when I believe that I’ve got it to work. So yeah, that definitely makes sense.
k6 run script.js --iterations 1 would be sufficient, as -u 1 is the default.