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

Templating issue in latest k6 release #2623

Closed
varghesevv opened this issue Jul 29, 2022 · 1 comment
Closed

Templating issue in latest k6 release #2623

varghesevv opened this issue Jul 29, 2022 · 1 comment
Labels
breaking change for PRs that need to be mentioned in the breaking changes section of the release notes question

Comments

@varghesevv
Copy link

Brief summary

javascript templating with backtick symbol is not working on latest k6 releases. It is working up to release "grafana/k6:0.39.0-24-g0127a508", not anything after that

referenced the example from this url - https://k6.io/docs/test-types/load-testing

k6 version

0.39.0-25-ge2316a28

OS

linux ubuntu

Docker version and image (if applicable)

grafana/k6:0.39.0-25-ge2316a28

Steps to reproduce the problem

working fine with this command
docker run -v ${PWD}:/test grafana/k6:0.39.0-24-g0127a508 run /test/test-1.js

not working with this
docker run -v ${PWD}:/test grafana/k6:latest run /test/test-1.js or
docker run -v ${PWD}:/test grafana/k6:0.39.0-25-ge2316a28 run /test/test-1.js

/* conf-test-1.js */
export const options = {
  stages: [
    { duration: '5s', target: 2 } 
  ],
  thresholds: {
    'http_req_duration': ['p(99)<1500']
  },
};

export function get_url(){
  return `${BASE_URL}/auth/token/login/`;
}


/* test-1.js */

import http from 'k6/http';
import { check, group, sleep } from 'k6';

import { get_url } from './conf-test-1.js'
export { options } from './conf-test-1.js'

const BASE_URL = 'https://test-api.k6.io';
const USERNAME = 'TestUser';
const PASSWORD = 'SuperCroc2020';

export default () => {
  const loginRes = http.post(get_url(), {
    username: USERNAME,
    password: PASSWORD,
  });

  const authHeaders = {
    headers: {
      Authorization: `Bearer ${loginRes.json('access')}`,
    },
  };

  const myObjects = http.get(`${BASE_URL}/my/crocodiles/`, authHeaders).json();
  check(myObjects, { 'retrieved crocodiles': (obj) => obj.length > 0 });

  sleep(1);
};


### Expected behaviour

Should work with javascript templates

### Actual behaviour

```time="2022-07-29T09:55:54Z" level=error msg="ReferenceError: BASE_URL is not defined\n\tat get_url (file:///test/conf-test-1.js:13:12(1))\n\tat file:///test/test-1.js:12:29(8)\n\tat native\n" executor=ramping-vus scenario=default source=stacktrace```
@varghesevv varghesevv added the bug label Jul 29, 2022
@mstoykov
Copy link
Contributor

mstoykov commented Jul 29, 2022

There is nothing wrong with the templating if you look carefully the problem is that BASE_URL isn't defined in conf_test-1.js which it isn't.

Previous to #2571 though all variables in the main module/script were globally available which was definitely a bug.

With the future addition of native ESM support this would break either way as ... well native modules don't expose their variables globally.

If you want to have access to a variable globally you should be using globalThis. So in this particular case changing to globalThis.BASE_URL = .... should fix it everywhere.

edit: I would also recommend that you use globalThis.BASE_URL wherever you use it. Otherwise, whoever reads the code after that might have really hard time figuring out where does this variable that isn't defined in this file is coming from.

Sorry for the breaking change, but this definitely was never documented or expected to work like that. If anything given that we say that we support ESM (not natively) it is expected to not work like that at all ;).

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 question
Projects
None yet
Development

No branches or pull requests

2 participants