Globals in Test block the Monitor

I’m playing around with setting up a monitor for the tests that I’m creating. However, the monitor doesn’t seem to find any tests.
It looks like the constants I’ve defined on a global level on top of the test file are blocking the monitor somehow.

I’ve tried to see what would happen when I put a pm.test on top of the test file. Below is a snippet of that, and below it the result of the Monitor.

pm.test("test", () => console.log("test"));

const resHeaders = pm.response.headers;
const jsonData = pm.response.json();

pm.test("Status code is 200", () => {

image

As you can see, the first pm.test is found by the monitor, but the second one for the status code is not. When you define the constants on top of the test file the Monitor will find no tests at all.

Is there a way to make the Monitor see all the tests, despite the global constants?

Hi @paulgroothuisrvc

I did some digging around and was able to reproduce the issue that you had with accessing global variables.

I dug around and found the information that you need from the Learning Documentation: Monitoring your API

Essentially, when using a Monitor, you are not able to use existing Global Variables (not sure exactly why) but it looks like the workaround that is identified is that you can created and use new Global Variables from within the Monitor, but they are reset when the Monitor is complete.

Hope this helps!

Thank you for your reply. However, the documentation you provided seems to regard the Globals built in in Postman, and not variables/constants declared globally in tests.

Regardless, I’ve tried to use the Globals, Collection variables, and Environment variables to work around my issue, as I think you suggested, but no matter what I do, as soon I do basically anything other than “pm.test” the Monitor just seems to stop.
Take the example snippet below. Instead of the constants like before I set global variables. But when I run the Monitor with this, still only the first test is “seen” and the Status code test and the rest of the test file is skipped. Even the “postman.setNextRequest” I have at the end of the file is skipped.

pm.test("test", () => console.log("test"));

pm.globals.set("resHeaders", pm.response.headers);
pm.globals.set("jsondata", pm.response.json());

pm.test("Status code is 200", () => {

The only thing I found that sorta works is to every time declare the variables inside of each individual test, which feels like a really dirty solution. I would love to have a better way of doing this.