Tests for empty response body

Hi ,
I have below test:

pm.test("Verify the status and name"  , function () {
    var jsonData = pm.response.json();
     pm.expect(jsonData[0].name, "Name is not available").not.equal(null);
  });

for some situation we get valid response but in some situation, we get empty response . So I wanted to test “name” value should not be null in the response. When the response is Not null, in this case my tests Passed. But when get empty response, in this scenario, I am getting below error:

Verify the status and name | TypeError: Cannot read property ‘name’ of undefined.

This is not a ideal test report. Instaed of this I wanted someting like that:(Valid assertion error)

Verify the status and name | AssertionError: Name is not available : expected undefined to not equal null.

How can I achieve this?

This is something handling type error and providing generic error.

What is the response body for that request?

The assertion that you have is looking for a value in the name key and it’s not there (undefined), that’s why it’s failing. This part .not.equal(null); also looks not quite right to me. :thinking:

There is a difference between something being in the response body and having null as a value and something not being part of the response.

Would this be what you need?

pm.expect(jsonData[0].name, 'Name is not available').to.not.be.undefined;

if we are getting response with data like below:

[
    {
        "sku": "22",
        "name": "U.S. Roaming Pass",
        "expiryDate": "2020-10-02T11:10:23",
        "isActive": true,
        "oneTimePasses": [
            {
                "sku": "2000023",
                "name": "U.S. Roaming - 100MB/Talk/Text (30 Days)",
                "description": "Get 100 MB of extra data, 100 minutes of local/back to Canada calling, and 100 SMS of global texting while roaming in the U.S., Puerto Rico, or the U.S. Virgin Islands. Roaming pass valid for up to 30 days.",
                "price": 0,
                "purchasedDate": "2020-09-02T11:39:24",
                "packages": [],
                "duration": {
                    "amount": "30",
                    "unit": "Days"
                }
            }
        ],
        "cap": [
            {
                "amount": "100",
                "unit": "Megabytes",
                "type": "data"
            },
            {
                "amount": "Unlimited",
                "unit": "Units",
                "type": "text"
            },
            {
                "amount": "Unlimited",
                "unit": "Minutes",
                "type": "voice"
            }
        ],
        "usages": [
            {
                "amount": "0",
                "unit": "Megabytes",
                "type": "data"
            },
            {
                "amount": "n/a",
                "unit": "Units",
                "type": "text"
            },
            {
                "amount": "n/a",
                "unit": "Minutes",
                "type": "voice"
            }
        ],
        "isExpired": false,
        "correlationId": null,
        "operationResult": false
    }
]

My tests passed as Name in not null here. But if response comes blank than I am getting type error. I wanted to show proper assertion fail error as name is null here (as name is not present in the response)

Can you give an example or what you mean by blank?

If the name is not part of the response, it would be undefined unless the response looks like this:

[
    {
        "name": null
    }
]

sometime name is part of response and sometime it does not part of response. so when it does not part of rersponse I would like to show Assertion error instead of type error. That is my requiement.

If it’s part of the response, it will have a value. If it’s not part of the response it’s undefined because it’s not there.

pm.test("Verify the status and name"  , function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData[0].name, 'Name is not available').to.not.be.undefined;
});

When getting empty in the response, still I am getting Type error , instead of proper assertion error.

aahh…Dany is right pm.expect(jsonData[0].name, 'Name is not available').to.not.be.undefined; do not leads you anywhere… you need change your scope? what you what to test for jsonData[0].name ?

Yes … I want to test jsonData[0].name. If we have Data it should be validated successfully but if we have empty response, it should throw Assertion error ,not Type error.

Type error in the response misleading to more on script error.

Can you post the response body returned when you see that error.

I am getting below blank response:

Can you show the screenshot of the response Body when it is coming blank ?

To check if response JSON object contains key XYZ:

    pm.test("respons has asset key", function() {
      pm.expect(pm.response.json()).to.have.property('name') ;
    });

Then you can add additional assertions:

    pm.test("Name is a string", function() {
      pm.expect(pm.response.json().name).to.be.a('string');
    });
    
    pm.test("Model match respons - name", function() {
      pm.expect(pm.response.json().name).to.equal("U.S. Roaming Pass");
    });

Edit:
If you have more than a ‘name’ to check:

    pm.test("Check if response contains all the keys", function () {
     pm.expect(pm.response.json()[0]).to.be.an('object').that.has.all.keys('id', 'name');
    });