Possibility to test if my id is sorted in ascending order in my body

Using pure JavaScript and broken down in smaller pieces

pm.test("All ids are ascending", () => {
    const response = pm.response.json();

    // Extract all ids in an array
    const ids = response.data.map(item => item.id);
    
    // Check that all are ascending
    function isAscending(a) {
        return a.every((x, i) => {
            return i === 0 || x >= a[i-1];
        });
    }
    
    pm.expect(isAscending(ids)).to.be.true;
});

Simplified version

pm.test("All ids are ascending", () => {
    const response = pm.response.json();

    const isAscending = response.data.map(item => item.id).every((current, index, ids) => index === 0 || current >= ids[index-1]);
    
    pm.expect(isAscending).to.be.true;
});

P.S. Test and make sure the tests actually fail when needed.

2 Likes