Environment variable scope when set in a function defined in a collection or folder prerequest script

I find it useful to define functions in collection or folder pre-request scripts for operations that are performed in multiple requests in the collection or folder. When I set an environment variable in such a script, the value persists for the duration of the function call, but when the function returns, the value is no longer set in the environment. It’s as if these scripts create an additional variable scope for a local environment.

Here’s an export of a simple test collection that demonstrates the behavior.

`{`
`  "info": {`
`    "_postman_id": "aedb7344-f32d-4395-b42a-b5153359682a",`
`    "name": "Environment scope test",`
`    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"`
`  },`
`  "item": [`
`    {`
`      "name": "Folder",`
`      "description": "",`
`      "item": [`
`        {`
`          "name": "Environment scope test",`
`          "event": [`
`            {`
`              "listen": "test",`
`              "script": {`
`                "id": "080e8815-6322-473c-8fe8-4d6a1e4cdfef",`
`                "type": "text/javascript",`
`                "exec": [`
`                  "requestFunction = function() {",`
`                  "  const value = 'requestFunction';",`
`                  "  pm.environment.set('testVariable', value);",`
`                  "  pm.test('in function: testVariable environment variable is set to \"' + value + '\"', function() {",`
`                  "    pm.expect(pm.environment.get(\"testVariable\")).to.equal(value);",`
`                  "  });",`
`                  "};",`
`                  "",`
`                  "pm.environment.clear('testVariable');",`
`                  "collectionFunction();",`
`                  "pm.test('after collectionFunction: testVariable environment variable is set to \"collectionFunction\"', function() {",`
`                  "  pm.expect(pm.environment.get(\"testVariable\")).to.equal('collectionFunction');",`
`                  "});",`
`                  "",`
`                  "folderFunction();",`
`                  "pm.test('after folderFunction: testVariable environment variable is set to \"folderFunction\"', function() {",`
`                  "  pm.expect(pm.environment.get(\"testVariable\")).to.equal('folderFunction');",`
`                  "});",`
`                  "",`
`                  "requestFunction();",`
`                  "pm.test('after requestFunction: testVariable environment variable is set to \"requestFunction\"', function() {",`
`                  "  pm.expect(pm.environment.get(\"testVariable\")).to.equal('requestFunction');",`
`                  "});"`
`                ]`
`              }`
`            }`
`          ],`
`          "request": {`
`            "method": "GET",`
`            "header": [],`
`            "body": {},`
`            "url": {`
`              "raw": "https://www.getpostman.com/",`
`              "protocol": "https",`
`              "host": [`
`                "www",`
`                "getpostman",`
`                "com"`
`              ],`
`              "path": [`
`                ""`
`              ]`
`            }`
`          },`
`          "response": []`
`        }`
`      ],`
`      "event": [`
`        {`
`          "listen": "prerequest",`
`          "script": {`
`            "id": "df03439f-ddcb-4e7a-9b55-dcf0e821ebde",`
`            "type": "text/javascript",`
`            "exec": [`
`              "folderFunction = function () {",`
`              "  const value = 'folderFunction';",`
`              "  pm.environment.set('testVariable', value);",`
`              "  pm.test('in folderFunction: testVariable environment variable is set to \"' + value + '\"', function() {",`
`              "    pm.expect(pm.environment.get(\"testVariable\")).to.equal(value);",`
`              "  });",`
`              "};"`
`            ]`
`          }`
`        },`
`        {`
`          "listen": "test",`
`          "script": {`
`            "id": "0da73ed7-eac4-4ce6-9e21-6dec17b94ec7",`
`            "type": "text/javascript",`
`            "exec": [`
`              ""`
`            ]`
`          }`
`        }`
`      ]`
`    }`
`  ],`
`  "event": [`
`    {`
`      "listen": "prerequest",`
`      "script": {`
`        "id": "50474e9f-bfe8-4a54-8497-1922d151d304",`
`        "type": "text/javascript",`
`        "exec": [`
`          "collectionFunction = function() {",`
`          "  const value = 'collectionFunction';",`
`          "  pm.environment.set('testVariable', value);",`
`          "  pm.test('in collectionFunction: testVariable environment variable is set to \"' + value + '\"', function() {",`
`          "    pm.expect(pm.environment.get(\"testVariable\")).to.equal(value);",`
`          "  });",`
`          "};"`
`        ]`
`      }`
`    },`
`    {`
`      "listen": "test",`
`      "script": {`
`        "id": "64e501a7-b109-4431-a5c7-c94bf17b04de",`
`        "type": "text/javascript",`
`        "exec": [`
`          ""`
`        ]`
`      }`
`    }`
`  ]`
`}`

Is this behaviour by design? I haven’t found any mention of it in the discussion of variable scopes.

Possibly related: when I want to define a function in a collection or folder pre-request script for use in a request test script, I have found it necessary to use the following pattern.
funcname = function(params) = { body }
If I define the function as a function rather than as a variable, the function is not recognized by the request test script.
function funcname(params) { body }

@JessePelton I can see that you’ve used pm.environment.clear, which wipes out the entire environment. If you’re looking to remove a specific environment variable, use pm.environment.unset instead. Passing a variable name to pm.environment.clear has no special effect, but will still wipe out the entire environment.

Additionally, the execution scope is not shared across different scripts, so you will have to re-declare any functions that you’d like to use. We do understand that there is room for improvement here :smile:

The sandbox API is documented here: http://getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference