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

Strange behavior with import statement #1

Open
pomeh opened this issue Jan 19, 2023 · 0 comments
Open

Strange behavior with import statement #1

pomeh opened this issue Jan 19, 2023 · 0 comments

Comments

@pomeh
Copy link

pomeh commented Jan 19, 2023

Hello,

In the lib documentation, you instruct to import the jslib url to use the new URL syntax, and it works as expected:

$ cat url.js && \
  echo "----------" && \
  docker run --rm -v $(pwd)/:/usr/local/me docker.io/grafana/k6:0.42.0 run /usr/local/me/url.js

import { URL } from "https://jslib.k6.io/url/1.0.0/index.js";

export default function main() {
    console.log('main');

    console.log('url', new URL('http://example.com'));

    console.log('end');
}
----------

          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: /usr/local/me/url.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

time="2023-01-19T10:36:21Z" level=info msg=main source=console
time="2023-01-19T10:36:21Z" level=info msg="url \"http://example.com/\"" source=console
time="2023-01-19T10:36:21Z" level=info msg=end source=console

running (00m00.0s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [ 100% ] 1 VUs  00m00.0s/10m0s  1/1 iters, 1 per VU

     data_received........: 0 B 0 B/s
     data_sent............: 0 B 0 B/s
     iteration_duration...: avg=410.55µs min=410.55µs med=410.55µs max=410.55µs p(90)=410.55µs p(95)=410.55µs
     iterations...........: 1   1953.872967/s

The log line msg="url \"http://example.com/\" proves it worked.


When I comment the import statement, I get an expected error ReferenceError: URL is not defined, that's okay:

$ cat url.js && \
  echo "----------" && \
  docker run --rm -v $(pwd)/:/usr/local/me docker.io/grafana/k6:0.42.0 run /usr/local/me/url.js

// import { URL } from "https://jslib.k6.io/url/1.0.0/index.js";

export default function main() {
    console.log('main');

    console.log('url', new URL('http://example.com'));

    console.log('end');
}
----------

          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: /usr/local/me/url.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

time="2023-01-19T10:36:30Z" level=info msg=main source=console
time="2023-01-19T10:36:30Z" level=error msg="ReferenceError: URL is not defined\n\tat main (file:///usr/local/me/url.js:6:27(9))\n\tat native\n" executor=per-vu-iterations scenario=default source=stacktrace

running (00m00.0s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [ 100% ] 1 VUs  00m00.0s/10m0s  1/1 iters, 1 per VU

     data_received........: 0 B 0 B/s
     data_sent............: 0 B 0 B/s
     iteration_duration...: avg=163.35µs min=163.35µs med=163.35µs max=163.35µs p(90)=163.35µs p(95)=163.35µs
     iterations...........: 1   2399.612223/s

Things are getting strange when I import the lib but with a different syntax (import whatever from "https://jslib.k6.io/url/1.0.0/index.js";):

$ cat url.js && \
  echo "----------" && \
  docker run --rm -v $(pwd)/:/usr/local/me docker.io/grafana/k6:0.42.0 run /usr/local/me/url.js

import whatever from "https://jslib.k6.io/url/1.0.0/index.js";

export default function main() {
    console.log('main');

    console.log('uu', new URL('http://example.com'));

    console.log('end');
}
----------
time="2023-01-19T10:48:01Z" level=info msg=main source=console
time="2023-01-19T10:48:01Z" level=info msg="uu \"http://example.com/\"" source=console
time="2023-01-19T10:48:01Z" level=info msg=end source=console

          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: /usr/local/me/url.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

time="2023-01-19T10:48:01Z" level=info msg=main source=console
time="2023-01-19T10:48:01Z" level=info msg="uu \"http://example.com/\"" source=console
time="2023-01-19T10:48:01Z" level=info msg=end source=console

running (00m00.0s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [ 100% ] 1 VUs  00m00.0s/10m0s  1/1 iters, 1 per VU

     data_received........: 0 B 0 B/s
     data_sent............: 0 B 0 B/s
     iteration_duration...: avg=560.56µs min=560.56µs med=560.56µs max=560.56µs p(90)=560.56µs p(95)=560.56µs
     iterations...........: 1   1447.502696/s

Note: for an unknown reason, this docker command did not show all k6 output, so I had to change it as follows:

docker run --name k6 -v $(pwd)/:/usr/local/me docker.io/grafana/k6:0.42.0 run /usr/local/me/url.js > /dev/null && \
docker logs k6 && \
docker rm k6

The JS statement import whatever from "https://jslib.k6.io/url/1.0.0/index.js"; import the lib, and stores its defaults export into whatever variable, and this variable is not used.

But then, the new URL works ! Like if the URL was added in a global namespace when the https://jslib.k6.io/url/1.0.0/index.js file is executed.

This even works with multiple files: if I have 3 files A, B and C using new URL, if only file A import the jslib url, both B and C files can use the new URL syntax without explicitly importing the lib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant