Unable to define JSON.parse(responseBody) in Collection tests tab

Hi everyone,

I’m currently trying to optimize my tests in Postman and wanted to use the Tests tab of my Collections for test I run almost all the time for each request.

I was just wondering if you guys knew why I can’t set a jsonData directly in this tab. Instead I have to call this variable in the Tests tab for all my requests.

Console is showing :

ReferenceError: jsonData is not defined

What I want to do and does not work :

postmancollectiontestissue|637x326

Thank you !

I don’t see where you are consuming jsonData in your screenshot. I see it is being declared, but not actually used.
Are you able to post your code that is actually failing?

I was able to get it collection tests working using two different methods:

let jsonData = JSON.parse(responseBody);

and

let jsonData = pm.response.json();

The screenshot I attached was more to show how I want my global template in my collection to be.
jsonData depends on what the response of my request is so it’s normal (to me) not to use it at a collection level.
Here’s an example for a request :


I tried to update my collection with one of your method but it doesn’t seem to work better, unfortunately

I think I understand what you’re trying to do now.

In your request tests, you are trying to reference a variable you declared in your Collection tests.

You’re trying to save a line of code in each of your request tests. Got it :+1:

Unfortunately I don’t have an answer on that, but maybe somebody else will.

The best idea I could come up with is to stuff it into a collection variable on the collection test script, and then reference that in the request-level test script.

But that still requires you to declare a variable in the test script and set it equal to the collection variable value, and in fact would be a longer line of code than just declaring it in the test script as const jsonData = pm.response.json();

So tl;dr I don’t think there’s a way to automatically declare and assign to a variable for every request test script (at least not a way that will have any practical benefit)

1 Like