Passing parameters into group/describe anonymous function

Hello,

I am trying to assemble my first k6 project which is a POC for a larger goal. I might be sumbling over general JavaScript scoping intricacies or something with the runtime k6 uses and execution contexts that I am just not getting.

For starters I want to encapsulate things about specific endpoints like extractions, assertions, parameters, headers etc into local module(s) which is not conducive to the styles I see in a lot of examples where everything is just coded procedurally in the default function export of a script.

I have tried multiple ways of group/describe usage (I like the BDD expect API) in my first sketches of this design and keep stumbling in the fact that when I pass data into the anonymous functions of these calls, the object properties end up being undefined when the describe/group function body is executed.

Endpoint module code:

request(myTsUser, myUrl) {

        describe('tsReqAllData - assertions', function (myTsUser,myUrl) {
            const allDataAnonResp = myTsUser.httpxSession.get(myUrl.toString());
            expect(allDataAnonResp.status, 'response status').to.equal(200);
            expect(allDataAnonResp).to.have.validJsonBody();
        });

    }

Suffice it to say that I am calling request() from another module which is called by my script default function. I have tried many arrangements to try to get the objects to be passed in and accessible, but everything I have done so far indicates that variables are behaving as expected until I get to trying to read a property of the objects passed into the anon function, these objects always end up as undefined. I have tried this with both group and describe, using arrow function syntax, as well as conventional anonymous function definition.

TypeError: Cannot read property ‘httpxSession’ of undefined

Any ideas what concept or k6/goja pitfall I am not getting here?

TIA
-Sean

Hi @naturaldog , welcome to the community forum!

Can you try it like this?:

request(myTsUser, myUrl) {

        describe('tsReqAllData - assertions', function () {
            const allDataAnonResp = myTsUser.httpxSession.get(myUrl.toString());
            expect(allDataAnonResp.status, 'response status').to.equal(200);
            expect(allDataAnonResp).to.have.validJsonBody();
        });

    }
2 Likes

Wow! It did work. I guess this is something about scoping/closure which I am not quite grasping yet. Thanks!

1 Like