Skip to content
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

Global variables declared in init context no longer work when running tests with docker #2680

Closed
geaninahutanu opened this issue Sep 12, 2022 · 1 comment
Labels
breaking change for PRs that need to be mentioned in the breaking changes section of the release notes

Comments

@geaninahutanu
Copy link

k6 run k6-tests/test-main-file.js works without problem.

docker run -v $(pwd)/k6-tests:/k6-tests -i loadimpact/k6 run /k6-tests/test-main-file.js throws an error
"msg="ReferenceError: context is not defined\n\tat first_scenario"

test-main-file.js

import {first_scenario as first_scenario} from "./first_scenario.js"
export {first_scenario as first_scenario}

const context = {
  id: __VU,
  expiresAt: '2023-08-12T09:20:36.148Z',
  requestBody: JSON.parse(open("request-template")),
}

export let options = {
  thresholds: {
    http_req_failed: ['rate < 0.01'],   // http errors should be less than 1%
    http_req_duration: ['p(95) < 10000', 'p(99) < 15000'], // 95% of requests should be below 200ms
  },
  noConnectionReuse: true,
  scenarios: {
    first_scenario: {
      exec: "first_scenario",
      executor: 'ramping-arrival-rate',
      timeUnit: '1s',
      startRate: 10,
      preAllocatedVUs: 10,
      maxVUs: 50,
      stages: [{target: 30, duration: '3s'}]
    }
  }
}

first_scenario.js

import {clone} from "./util/util.js";

export function first_scenario() {
    let request = clone(context.requestBody)
    request.expires_at = context.expiresAt
    ...
}
@geaninahutanu geaninahutanu changed the title Global variables declared in init context no longer works when running tests with docker Global variables declared in init context no longer work when running tests with docker Sep 12, 2022
@mstoykov
Copy link
Contributor

Hi @geaninahutanu,

why would you expect that you will be able to access context from the main module without importing it in the first_scenario.js ?

This is exactly that modules are supposed to prevent. Unfortunately k6 did have a bug where that was not the case until the last release, done last week. You can also look at the Breaking changes section it is basically just after the list of smaller breaking changes.

As mentioned there if you want something to be just globally available you should use the global object globalThis

I would expect that you are running the latest version in docker, but not outside.

A previous issue with a more confusing error was #2623

@mstoykov mstoykov closed this as not planned Won't fix, can't repro, duplicate, stale Sep 12, 2022
@mstoykov mstoykov added the breaking change for PRs that need to be mentioned in the breaking changes section of the release notes label Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change for PRs that need to be mentioned in the breaking changes section of the release notes
Projects
None yet
Development

No branches or pull requests

2 participants