Array has 3 potential states and im using an IF/ELSE statement to check states before running my tests
I need some guidance, i have the below if/else statement at the collection level of my test suite. There are several endpoints in the collection and all share the same json structure but depending on the endpoint, an array can have 3 states (populated, empty or not present). I need a way of specifying that the array is populated without checking for a specific object, just that it has data in it.
pm.test("Filters - Category array is correct", function () {
let responseJson = pm.response.json();
if (responseJson.widget.data.filters === undefined ) {
pm.expect(responseJson.widget.data.filters).to.be.undefined
console.log("Filters array is not present");
} else if (responseJson.widget.data.filters.length == 0) {
pm.expect(responseJson.widget.data.filters).to.be.empty
console.log("Filters array is empty")
} else (responseJson.widget.data.filters === true)
pm.expect(responseJson.widget.data.filters[0].category).to.eq('category');
My issue is that for the else, its running at the same time as the else if because they are both correct, the filter array is empty but still present so the test is failing because the ‘category’ object isnt present.
Is there a way that i can write the else to be something like } else (responseJson.widget.data.filters === populated)
i know thats not a function but its the only way i can really explain what my desired outcome is!
Thanks!