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

ReferenceError: global is not defined (version 4.3.4.2 and 4.3.4.3) #16

Closed
bdshadow opened this issue Jul 19, 2024 · 3 comments · Fixed by #17
Closed

ReferenceError: global is not defined (version 4.3.4.2 and 4.3.4.3) #16

bdshadow opened this issue Jul 19, 2024 · 3 comments · Fixed by #17
Assignees
Labels
bug Something isn't working

Comments

@bdshadow
Copy link

I' getting the following error

time="2024-07-19T21:02:20Z" level=error msg="ReferenceError: global is not defined\n\tat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:1:3127(15)\n\tat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:1:2968(14)\n\tat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:1:3037(5)\n\tat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:1:250(11)\n\tat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:3:14482(13)\n\tat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:1:250(11)\n\tat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:3:53295(13)\n\tat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:1:250(11)\n\tat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:3:53540(4)\n\tat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:1:250(11)\n\tat https://jslib.k6.io/k6chaijs/4.3.4.3/index.js:3:53642(268)\n\tat go.k6.io/k6/js.(*requireImpl).require-fm (native)\n\tat webpack://k6-typescript-tests/external%20commonjs%20%22https://jslib.k6.io/k6chaijs/4.3.4.3/index.js%22:1:34(36)\n\tat file:///test/perf/ebox/api/all-api-scenarios-seq.debug.js:75:12(3)\n" hint="script exception"

while running the following test:

import { expect } from "https://jslib.k6.io/k6chaijs/4.3.4.3/index.js"
import { sleep } from 'k6';

export const options = {
  vus: 1,
  duration: '10s'
};

export default function() {
  sleep(2 + Math.random() * 2);
  expect(200, "status is 200").to.equal(200);
}

The test is run in k8 by using https://github.com/grafana/k6-operator. When version is changed to 4.3.4.1, everything works fine, the problem starts from version 4.3.4.2.
Also, everything works fine locally.
I also tried to use https://github.com/szkiba/xk6-chai and update the dependency to "k6/x/chai", but it was the same ReferenceError only complaining about k6chaijs.min.js.
I'm a little lost in what theoretically can be the problem and what else can I check.

@mstoykov
Copy link
Contributor

Hi @bdshadow, I recently found this out and discuss it in grafana/k6#3864 also related to grafana/k6#3860

Can you tell version of k6 you are using and compatibility-mode ?

As mentioned in the first issue this is coming from chaijs and has been fixed for years but a new version hasn't been released so that it works in other places apart from NodeJS.

A workaround will be for you (or here in chaijs) for us to do the globalThis.global = globalThis so that it can be used.

if you put in global.js

globalThis.global = globalThis;

and then in your file you have

import "./global.js"
import { expect } from "https://jslib.k6.io/k6chaijs/4.3.4.3/index.js"
// rest of your code

That should make it work

@mstoykov mstoykov added the bug Something isn't working label Jul 20, 2024
@mstoykov mstoykov self-assigned this Jul 20, 2024
@bdshadow
Copy link
Author

@mstoykov thank you for your reply.

First of all, I want to confirm that the described workaround works

Can you tell version of k6 you are using and compatibility-mode ?

We use k6 operator of version v0.0.13, which uses alpine image with the following k6:

/ # k6 version
k6 v0.51.0 (go1.22.4, linux/amd64)
Extensions:
  github.com/grafana/xk6-browser v1.6.0, k6/x/browser [js]
  github.com/grafana/xk6-browser v1.6.0, k6/x/browser/async [js]
  github.com/grafana/xk6-loki v0.0.0-20240621131830-5248b47b4efd, k6/x/loki [js]
  github.com/grafana/xk6-output-timescaledb v0.2.1, timescaledb [output]
  github.com/szkiba/xk6-chai v0.1.1, k6/x/chai [js]

About compatibility mode:

env:
  - name: K6_COMPATIBILITY_MODE
    value: "base"

@mstoykov
Copy link
Contributor

Hi @bdshadow I would expect that given that you use base you are also transpiling the source code from ECMAScript Modules to CommonJS with babel/webpack/esbuild as v0.51.0 doesn't support ECMAScript Modules in base compatibility mode.

This has finally been implemented but isn't realeased and is under heavy testing in order to be part of v0.53.0 to be released in August.

But base again will not actually include global being alias to globalThis as it is meant to be a pure ECMAScript implementation as possible.

I will try to push the fix through the upstream and see how it goes. But I would expect it will be a while, and I doubt there will be a backport for v4.X of chai given that v5 exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants