Global functions via Collection level folder?

Hi,

I am able to implement this:

Collection Pre-requisite Script:

utils = {
    hello: function() {
        return 'hello';
    }
}

Request-level Test Script results to hello in my console:

console.log(utils.hello());

Following this pattern, I have tests which are doing the same thing across my requests thus I’d like to reference them as a global function.

With the following, I am getting ReferenceError: scenario is not defined when I run my collection.

Collection Pre-requisite Script:

utils = {
    verifyStatusCode: function(pm) {
        pm.test("Verify status code", function() {
            pm.expect(pm.response.code).to.be.equal(scenario.expectedResponseCode);
            }
        );
    }
}

Request-level Test Script is logged as undefined:

console.log(utils.verifyStatusCode(pm));

I am initializing scenario in my Request-level Test Script. I am able to log its value as well.

Am I missing something in terms of initializing the local var scenario which is then used by global function?

Note that I’m avoiding using eval() as advised here.

@maclazaro

This really should have been its own question.

Your function is executing fine.

The problem is with this line.

pm.expect(pm.response.code).to.be.equal(scenario.expectedResponseCode);

Where is scenario.expectedResponseCode defined?

The error in the test is basically telling you that “scenario” is undefined, which is correct, as I can’t see it referenced anywhere else in your code.

The function\test is executing. It’s just failing the conditions of the assertion.

You can also change this to…

pm.response.to.have.status(200);

You can parameterize the number, but will have to pass the value into the function.

Hi @mdjones,

I realize that, I raised a separate question here where I show the scenario declaration.