Generate local variable in a function then use in another function?

I have variables which are test-specific thus I only want to set their values at the Tests tab.

These variables are repetitive across my tests.

I also have functions which will be using these variables.

Is it possible to hoist the value of the vars declared within a function to another function?

With the following implementation, I am getting ReferenceError: isActualResponseAsExpected is not defined

When not declared via function (i.e. declared within the Tests tab), I am able to use the variables I’ve declared.

Collection Pre-request Script:

utils = {
	isActualResponseAsExpected: function(pm, actualResponse, expectedResponse) {
		if ((JSON.stringify(actualResponse)) === (JSON.stringify(expectedResponse))) {
			var isActualResponseAsExpected = true;
		} else {
			var isActualResponseAsExpected = false;
		}
	}

	verifyResponse: function(pm, isActualResponseAsExpected) {
		pm.test("Verify response", function () {
			pm.expect(isActualResponseAsExpected).to.be.equal(true);
		});
	}
}

Tests tab:

utils.verifyResponse(pm, isActualResponseAsExpected);

Not sure if this is the best way but this is how I got it to work:

Collection Pre-request Script:

utils = {
	isActualResponseAsExpected: function(pm, actualResponse, expectedResponse) {
		if ((JSON.stringify(actualResponse)) === (JSON.stringify(expectedResponse))) {
			pm.variables.set("isActualResponseAsExpected", true);
		} else {
			pm.variables.set("isActualResponseAsExpected", false);
		}
	}

	verifyResponse: function(pm, isActualResponseAsExpected) {
		pm.test("Verify response", function () {
			pm.expect(isActualResponseAsExpected).to.be.equal(true);
		});
	}
}

Tests tab:

utils.verifyResponse(pm, pm.variables.get("isActualResponseAsExpected"));

What is it you are trying to achieve?

I wouldn’t embed tests into the function as that causes issues with returning data to the function.

Ideally, the function in the pre-requests would be written similar to the following…

utils = {
	isActualResponseAsExpected: function(pm, actualResponse, expectedResponse) {
		if ((JSON.stringify(actualResponse)) === (JSON.stringify(expectedResponse))) {
            return true
		} else {
            return false
		}
	}
}

This will return data and the undefined issue will go away.

Then the tests are in the tests tab.

let actualResponse1 = utils.isActualResponseAsExpected(pm, "true", "true") // true
let actualResponse2 = utils.isActualResponseAsExpected(pm, "true", "false") // false

pm.test("Verify Response 1", function () { // pass
    pm.expect(actualResponse1).to.be.equal(true);
});

pm.test("Verify Response 2", function () { // fail
    pm.expect(actualResponse2).to.be.equal(true);
});

Although I’m not really seeing the point of this utility function, as the test functions can handle this better and the code seems superfluous. I can’t see the added value.

Hi @michaelderekjones,

Thanks for your inputs! I’m creating the test as a function as I will be using it across my tests, and I’d like to maintain it in one place (Collection Pre-request Script) instead of going through each Tests tab.

As for the isActualResponseAsExpected declaration, I am checking this so in case it is false, that is when I will log the actual response only, as if I test first, the test throws an error which ends the process and I am unable to log.

After setting isActualResponseAsExpected to false, I’d like my verifyResponse to fail to show up in my reports.