Code Snippet:
pm.test(“Verify publish date is sort from newest to old order”,function () {
var l = require(‘lodash’),
var jsonData = pm.response.json().results[0];
responseArray = JSON.parse(jsonData);
pm.test(‘Date for article published is in sorted order’, () => {
var expectedSortedOrder = l.orderBy(responseArray, [‘publishedDate’],[‘desc’]);
pm.expect(responseArray).to.eql(expectedSortedOrder);
});
})
Thanks all but now I am getting a different type of error. any assistance would be appreciated
New Code Snippet;
pm.test("Verify publish date is sort from newest to old order",function () {
var jsonData = pm.response.json().results;
responseArray = JSON.parse(jsonData);
pm.test('Date for article published is in sorted order', () => {
var expectedSortedOrder = _.orderBy(responseArray, ['publishedDate']);
console.log(expectedSortedOrder);
pm.expect(responseArray).to.eql(expectedSortedOrder);
Get used to console logging your variables. They are important for troubleshooting.
This way you will also be able to tell which line is causing the issue.
var jsonData = pm.response.json().results;
console.log(jsonData);
Looking at your example response. This should already been an array of objects.
Confirm by looking at the console log or create a test for it.
pm.test("jsonData should be an array", () => {
pm.expect(jsonData).to.be.an("array").that.is.not.empty;
});
It will be the following line that is causing the issue.
responseArray = JSON.parse(jsonData);
You’ve already parsed the data, you don’t need to do it again unless the data is embedded in a string which might require parsing. Therefore its failing the conversion as the data already contains an array of objects.
You should be able to sort directly on the “jsonData” array.