How to debug k6 testing script in Visual Studio Code?

When trying to launch Visual Studio Code in debugging mode, to inspect a k6 testing script, I get the following message:

Uncaught Error: Cannot find module ‘k6’.

I’m writing the test scripts in TypeScript and I’m using NodeJS + Webpack + Babel. The .js file I pass to k6 is the bundle file created by Webpack.

My launch.json file looks like this:

“version”: “0.2.0”,
“configurations”: [
{
“name”: “Launch Program”,
“type”: “node”,
“request”: “launch”,
“cwd”: “{workspaceFolder}", "protocol": "inspector", "preLaunchTask": "npm: build", "program": "{workspaceFolder}/src/Main.ts”,
“outFiles”: [
“${workspaceFolder}/dist/Main.js”
],
“sourceMaps”: true
}
]

I’ve also added this to the tsconfig.json:

“sourceMap”: true

There is this import in my script:

import { Options } from ‘k6/options’;

Please, can anyone help? How can I configure it so that I can debug k6 testing scripts in Visual Studio Code?

Hi @calderano,

I am afraid this is not possible :frowning: .

k6 is not nodejs based and also has no debugger of its own. So there is no way for visual studio code to debug k6 scripts. The error you see is because it tries to run it in nodejs and “k6” is not a module nodejs recognises.

Some thoughts on this:

  • k6 can get a debugger … I have no idea what this will entail, but it will probably be a lot of work and is likely to not be compatible with anything else, which will practically negate the majority of the benefits
  • we can reimplement the k6 modules for nodejs so you can debug them. There have been people who wanted to use their (request making) nodejs code to be run in k6, but if they use these modules in nodejs … it will also work in k6. The implementation won’t be exactly the same so there will be subtle differences which means that if you are debugging something complex you might get different behaviour. Also this will be a lot of work.

Are you doing something complex enough that console.log won’t help you? As this is the only "debugger’ currently and given my reasoning above, it is unlikely to change … soon :frowning: .

Hi @mstoykov,

I was afraid of that but I was hoping there would be a way around it.
I understand. Thank you very much for the explanation.
We have just started a project using k6 and I was doing a ‘research task about it’. I believe console.log will help us.

Thank you once again!

I found it helpful to run k6 via HTTP proxy such as https://mitmproxy.org/. Also if you need to debug your own code and you factor it into a separate module, then you can use the usual node debugger tools.

is likely to not be compatible with anything else, which will practically negate the majority of the benefits

I think that integration of such a debugger with the IDE could be done with the comparable efforts.
Anyway, having a debugger for a tool with scripting capabilities is a big advantage for this tool.