Linting K6 scripts

I have gone through the documentation and searched K6 community topics as well but I couldn’t find anybody having problem with linting. I am using ESLint for linting. During eslint --init I get a series of questions. I do not which options are good or appropriate for K6. When I am asked a question like “Does your code run on node or browser”, I don’t know which one to select. Same with import/export question. I know K6 team writes blogs on various topics around K6. Please write a blog/tutorial for linting as well.

Linting questions and answers:
? How would you like to use ESLint? To check syntax, find problems, and enforce code style

? What type of modules does your project use? JavaScript modules (import/export)

? Which framework does your project use? None of these

? Does your project use TypeScript? No

? Where does your code run? I think I selected none

? How would you like to define a style for your project? Use a popular style guide

? Which style guide do you want to follow? Airbnb: GitHub - airbnb/javascript: JavaScript Style Guide

? What format do you want your config file to be in? JSON

I am getting an error like the one below. How to resolve it. I can’t disable the rule itself otherwise I won’t get error for custom variables
const __ENV: {
[name: string]: string;
}
Environment variables. https://k6.io/docs/using-k6/environment-variables

‘__ENV’ is not defined.eslintno-undef

First of all, thank you for pointing this out!

We currently don’t have any eslint-config which you use, but it would be neat to have IMO.

There are a few things you could do to fix the error right now. The first and simplest way is to define the globals you want to use in a comment at the top of your file:

/* global __ENV */
const hostname = __ENV.MY_HOSTNAME

This might not be the neatest way of doing it though, and would have to be repeated in every file of your project.

The “better” way would be to add an .eslintrc.json to your project and configure the globals there:

{
    "globals": {
        "__ENV": "readonly"
    }
}

The documentation for configuring eslint can be found here: Configuring ESLint - ESLint - Pluggable JavaScript linter

I hope that helps and if you have any questions, feel free to ask!

2 Likes

@allansson Thanks for providing the solution but there are many more bugs like that. I don’t have a lot of experience in setting up linting which is why I am looking for a K6 specific eslint config

I added an issue to add linting information in our docs: Add information on how to lint k6 scripts · Issue #192 · loadimpact/k6-docs · GitHub

2 Likes

I ended up adding the following to my .eslintrc.json:

    "globals": {
        "__ENV": "readonly"
    },
    "rules": {
        "import/no-unresolved": "off", // k6 is actually golang, can't really import it
        "no-restricted-globals": "off", // required by k6, e.g. "init" context
        "import/extensions": "off" // .js ending is ok
    }

After which, I could fix the linting errors as I would normally

1 Like

Hey @metasyn
How do I handle open() using ESLint rules. Open() is from K6 itself. Please see the link below. Thank you

const data = open("…/common/login.json");

JavaScript API: open (k6.io)