The setup function returns the same object value, yet works differently then expected

Hi team,

I want to login in the setup function using a k6 session (httpx) and use that for the rest of the test.
However when using a return value from the setup function and using data in the main function as an argument, it gives an error.

The authenticateFlowWithSession is just a simple login

export function authenticateFlowWithSession() {
    const loginSession = new Httpx({
        headers: {
            accept: "application/json, text/plain, */*",
            "Content-Type": "application/json"
        }
    });
    let body = JSON.stringify({
        username: __ENV.USERNAME,
        password: __ENV.PASSWORD
    });
    var login = loginSession.post(`${__ENV.URL}/login/authenticate`, body);
    var cookie = login.cookies;
    let session = login.cookies.PHPSESSID[0].value;
    loginSession.addHeader("cookie", cookie);

    return { loginSession };
}

export function setup() {
    auth = authenticateFlowWithSession();
    return auth.loginSession;
}
export default function testSuite(data) {
    const authenticationData = authenticateFlowWithSession();
    const testObject = authenticationData.loginSession;
    console.log(data);
    console.log(testObject);

They both return an object from the setup function and an object when I do it in the test itself. This can be seen when I console.log them.

{
    "baseURL": "",
    "params": {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "Content-Type": "application/json",
            "cookie": "PHPSESSID=hnrjkm3g4olal6qvdchnp05n90; SpecialCookie=somecookie"
        },
        "tags": {
        }
    }
}

However when I want to use testObject it in a request like this in the main testSuite function, it works.

 var response = testObject.post(`${__ENV.URL}/somerequest`, body);

When I want to do it from the setup function in a request like this also in the testSuite function, it does not work.

var response = data.post(`${__ENV.URL}/somerequest`, body);

it returns an error.

 Object has no member 'post'

What am I missing or doing wrong?

Thanks in advance.

Anyone? :sweat_smile:

Hey @rodyb,

this is mostly how setup is expected to work, it is not expected to be used for sharing complex objects with functions. It should mostly contain data object parsable by JSON.

You can read more details in this section of the documentation: Test Lifecycle#use-data-from-setup-in-default-and-teardown.

Let me know if it helps.