k6
k6 copied to clipboard
[Feature] Support for import.meta.env object
Feature Description
As a developer, I want to get same experience as for modern builders, such as vite. Vite provides import.meta.env object.
https://vite.dev/guide/env-and-mode
For an intellisence I suggest to use same vite way:
// file: k6-env.d.ts
/// <reference types="k6" />
interface ImportMetaEnv {
readonly K6_SOME_URL: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
}
And the script code is:
import http from 'k6/http';
export default function (){
const payload = JSON.stringify({
email: `user+${import.meta.env.__VU}`, // auto completes!
password: 'test'
});
http.post(import.meta.env.K6_SOME_URL, payload) // auto completes!
}
Suggested Solution (optional)
Due to k6 specific, here is a sample of predefined envs:
__ITER: A numeric counter with the current iteration number for a specific VU. Zero-based.__VU: Current VU number in use. k6 assigns the value incrementally for each new VU instance, starting from one. The variable is 0 when executing the setup and teardown functions.
Here is how vite handles import.meta object and other declarations:
- https://github.com/vitejs/vite/blob/8679a43de710052c5c84bb6c253829ab999b040a/packages/vite/client.d.ts#L4
- https://vite.dev/guide/env-and-mode#intellisense-for-typescript
Optionally, it would be great to automatic import vars from files same as vite way:
- .env # loaded in all cases
- .env.local # loaded in all cases, ignored by git
Or include env support from existing extension
- https://github.com/szkiba/xk6-dotenv
Already existing or connected issues / PRs (optional)
No response
Hi @vitalics, Thanks for the issue, sorry for the slow reply 🙇
My immediate reaction to the import.meta.env is that ... this isn't where it belongs. Env variables are global for the whole execution and are neither ESM specific nor specific for the current module - which is what import.meta is about.
A quick search showed that it was not approved for the Common API project, basically on those grounds and others.
We do have __ITER and __VU under the k6/execution module as different fields as there is more nuance when you get down to it. Arguably __ENV has a bunch of issues and we would likely want it somewhat redone. But I would expect it is going to go under k6/execution if anything.
I am not certain I got what the original problem was that lead to you wanting __ITER and __VU under it as well? Was it lack of knowledge for k6/execution and its abilities? Or was it some other features you wanted.
Hi @mstoykov. I got your position about import.meta.env. Did I get it right that your team would like to implement globalThis instead of providing import.meta.env as Vite and other builders do?