# Skip specific tests if empty array in response

**URL:** <https://community.postman.com/t/skip-specific-tests-if-empty-array-in-response/68376>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [September 13, 2024, 10:31am UTC](https://community.postman.com/t/skip-specific-tests-if-empty-array-in-response/68376 "2024-09-13T10:31:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-hicks](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@joe-hicks](https://community.postman.com/u/joe-hicks)\
**Post date:** [September 13, 2024, 10:31am UTC](https://community.postman.com/t/skip-specific-tests-if-empty-array-in-response/68376/1 "2024-09-13T10:31:32Z")

</div>

Skipping specific tests within a single endpoint if there is an array missing from the response json

Hi All!

I have a set of tests for a single endpoint and i want to know if there is a way that i am able to skip certain tests if there is an empty array in the response json. I was hoping there was a way to do this in the pre-request script, setting something up that looks through the response json to see if there is a specific array is populated, if it isnt then it will skip certain tests. If not then is there a way to wrap the tests in an IF statement?

Below is one of the tests that i want to skip if and ‘offers’ array is not present in the response

```auto
pm.test("Offers - offers array is correct", function () {
    const responseJson = pm.response.json();
    let stock_quantity = responseJson.widget.data.offers[0].offer.stock_quantity
    let percentage_saving_label = responseJson.widget.data.offers[0].offer.percentage_saving_label
    let product_note = responseJson.widget.data.offers[0].offer.product_note;
    let display_name = responseJson.widget.data.offers[0].offer.display_name

    pm.expect(responseJson.widget.data.offers[0].offer.name).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.price).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.in_stock).to.be.oneOf([true, false]);
    pm.expect(Object.prototype.toString.call(stock_quantity)).to.be.oneOf(["[object String]","[object Null]"])
    pm.expect(responseJson.widget.data.offers[0].offer.currency_iso).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.currency_symbol).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.link).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.link_text).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.merchant_link_text).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.label).to.be.a("string");
    pm.expect(Object.prototype.toString.call(percentage_saving_label)).to.be.oneOf(["[object Number]","[object Null]"])
    pm.expect(Object.prototype.toString.call(product_note)).to.be.oneOf(["[object String]","[object Null]"])
    pm.expect(Object.prototype.toString.call(display_name)).to.be.oneOf(["[object String]","[object Null]"])
    pm.expect(responseJson.widget.data.offers[0].offer.display_labels).to.be.a("string");
    pm.expect(responseJson.widget.data.offers[0].offer.display_primary_label).to.be.a("string");

})

```

Example of the array in the response

```auto
"offers": [
                {
                    "id": 2694807,
                    "match_id": 2694807,
                    "product_key": "25996-648193497",
                    "score": 23894,
                    "mobile_match_score": 23894,
                    "percentage": 100,
                    "click_count": 0,
                    "product_type": 1000,
                    "bundle_models": [],
                    "model_matched": [
                        "Sennheiser Sennheiser E 935"
                    ],
                    "model": "Sennheiser Sennheiser E 935",
                    "model_id": 833312,
                    "an": "Generic",
                    "offer": {
                        "name": "Sennheiser E 935",
                        "price": "95.00",
                        "in_stock": true,
                        "stock_quantity": null,
                        "currency_iso": "GBP",
                        "currency_symbol": "&pound;",
                        "link": "https://www.thomann.de/gb/sennheiser_e935_dynamisches_mikrofon.htm?offid=1&affid=195&subid=hawk-custom-tracking",
                        "link_text": "View Deal",
                        "merchant_link_text": "View Deal at Thomann",
                        "label": "",
                        "percentage_saving": null,
                        "percentage_saving_label": null,
                        "money_saving": null,
                        "money_saving_label": null,
                        "product_note": null,
                        "display_name": "Sennheiser Sennheiser E 935",
                        "display_labels": "",
                        "display_primary_label": ""
                    },

```

The purpose of this is that sometimes there arent any offers for the product, so the ‘offers’ array returned can be empty, which causes my tests to fail.

Thanks!

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 18, 2024, 4:02pm UTC](https://community.postman.com/t/skip-specific-tests-if-empty-array-in-response/68376/2 "2024-09-18T16:02:13Z")

</div>

Hey @joe-hicks 👋

Welcome to the Postman Community! :postman:

You could wrap the tests in a conditional statement that checks if the array is empty of not or check that the array’s length is not zero.

```auto
    let responseJson = pm.response.json();
    if (responseJson.widget.data.offers.length === 0) {
        // do this
    } else {
        // do that
    }

```

You could even do that in a separate test rather than trying to test for everything in a single test.

---

<div class="post-metadata">

**Author:** ![joe-hicks](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@joe-hicks](https://community.postman.com/u/joe-hicks)\
**Post date:** [September 24, 2024, 11:33am UTC](https://community.postman.com/t/skip-specific-tests-if-empty-array-in-response/68376/3 "2024-09-24T11:33:27Z")

</div>

Thanks Danny!

Nice and simple and it begs the question why i didnt think of structuring it like this in the first place! 🤦‍♂️

Thanks again

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [September 27, 2024, 11:34am UTC](https://community.postman.com/t/skip-specific-tests-if-empty-array-in-response/68376/4 "2024-09-27T11:34:24Z")

</div>

Closed on September 27, 2024, 11:34am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
