Check that each datetime in array is between specified datetimes

I am calling an endpoint using before and after timestamp parameters like this:
{{baseUrl}}/activity?before=2024-09-04T13:55:02.325Z&after=2024-09-03T12:34:47.830Z

Response body would be served as an array like this:

[
    {
        "eventType": "account-created",
        "invokedAt": "2024-09-04T13:54:52.153Z"
    },
    {
        "eventType": "account-preference-updated",
        "invokedAt": "2024-09-04T13:54:56.231Z",
        "changes": [
            {
                "property": "values",
                "prior": "",
                "new": "newValue"
            }
        ]
    },
    {
        "eventType": "account-preference-updated",
        "invokedAt": "2024-09-04T13:58:34.342Z",
        "changes": [
            {
                "property": "values",
                "prior": "newValue",
                "new": "newerValue"
            }
        ]
    }
]

How can I write a test that will evaluate whether the invokedAt timestamps are between the specified params? I will be using variables for the params to make it easy to comapre within the test.

The closest solution I have found was never fully answered, so I am not sure whether there could be an easily utilized Postman feature for it.

This is what I tried to build from that and some other various answers:

//Validate that each invokedAt timestamp is between the endpoint param timestamps
jsonData.forEach((obj, i) => {
    let invokedTime = obj.invokedAt;
    console.log(invokedTime);
    pm.test(`Object ${i} invoked time [${invokedTime}] within range `, () => {
        pm.expect((invokedTime).isBetween("2024-09-03T12:34:47.830Z",  "2024-09-04T13:55:02.325Z")).to.be.true;
    })
});

It fails with error:
Object 1 invokedTime[2024-09-04T13:54:56.231Z] between | TypeError: invokedTime.isBetween is not a function

isBetween is not a valid Chai assertion so the error is correctly reporting that its not a function.

Format it using moment, and then parse it to an integer so it then becomes a simple number comparison.

var response = pm.response.json();

var moment = require('moment');

let begin=parseInt(moment("2024-09-03T12:34:47.830Z").format("YYYYMMDDHHmmss"));
let end=parseInt(moment("2024-09-04T13:55:02.325Z").format("YYYYMMDDHHmmss"));

response.forEach((obj, i) => {
    let invokedTime = parseInt(moment(obj.invokedAt).format("YYYYMMDDHHmmss"));
    pm.test(`Object ${i} invoked time [${invokedTime}] within range `, () => {
        pm.expect(invokedTime).to.be.within(begin, end);
    })
});

1 Like

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