30 days of postman, day 26 - submit issue

I am working on day 26 of 30 days of postman.

When I try the submit, I get this message on the 5th test, and I’ve narrowed it down to the following test that is offending:

pm.expect(JSON.parse(collVar.value), ‘check collection variable array’).to.be.an(‘array’)

message:

Any ideas on what I should check? I did my own test for an array, which passed, so I’m not sure what is wrong:

pm.test(“lists variable holds an array”, function () {
Array.isArray(pm.collectionVariables.get(“links”));
});

image

That is saying that instead of returning a JSON object which would normally start with a [ or {, its finding the letter ‘h’ instead.

This is probably caused by the JSON.parse function, but the problem will be with collVar.value not returning a JSON object.

Copy\clone the submit request and console log collVar.value just before the failing test.

What is it returning?

collVar is being set using a find function. Usually with these errors, its looking for a specific folder or key name, and it might just be a spelling thing.

This is the whole test.

pm.test("Script added correctly", () => {
    let bingRequest = collection.item[0].item.find(req => { return req.name === "bing"})

    let test = bingRequest.event.find(event => {return event.listen === "test"})
    pm.expect(test.listen, 'check test script').equals("test")
    pm.expect(test.script.exec.toString(), 'check cheerio').to.include("cheerio.load")

    let collVar = collection.variable.find(variable => { return variable.key === 'links'})
    pm.expect(collVar.key, 'check collection variable set').to.equal('links')
    pm.expect(JSON.parse(collVar.value), 'check collection variable array').to.be.an('array')

    pass += 1
})

the collection variable is defined at the top of the tests tab.

let collection = pm.response.json().collection

console log that as well, and ensure that it has a top level element called variable.

I added a console.log(collVar.value); before the offending test, and this is what I get:

result:

It would seem that that the value is an array, but I’m not sure it needs to be parsed as I can’t see quotes around the console logged element. It’s not a string that needs to be parsed.

Looking at this more closely.

The JavaScript find function returns an array. collVar will be an array, however the test is looking for the “value” element under that array, which could be a string that needs to be parsed, but appears that its just another array.

It’s already an array, and you should be able to work directly with the elements.

The ‘collection’ variable is already parsed right at the top of the tests tab, so I don’t understand why it would need to be done again for any of the elements.

I hate it when I see array elements within JSON stored as string, because why on earth would you want to do this, as its then difficult to work directly with the elements.

let collection = pm.response.json().collection

The entire response has been parsed at this point.

In the cloned submit request, can you just remove the JSON.parse from the following line. (The pm.expect line).

If this does work, then it sounds like a problem with the submit request and one for Postman to check.

If you were accessing the actual collection variable directly, then you would need to parse it, as its stored as a string at that point. But this code is not actually working directly with the collection variable, but the “collection code” returned by the Postman API. It’s not quite the same. The find function will return an array, and does not need to be parsed further. It would appear that the “value” element which the test is checking is also an array, which doesn’t need to be parsed again.

You can check this by console logging the colvar variable on its own, and expand the elements and check that “value” is an array, and not a string. If you can expand the element, its an array. You won’t be able to expand it properly if its a string.

It would be nice to know if anyone else doing this challenge recently is having the same issue? As I would expect this to affect everyone.

Interesting. Thank you for this thorough response.

Yes, I can expand the console output of the array & it lists all elements.

When I remove the JSON.parse() from that last pm.expect, the test successfully passes!

It could very well be that I’m doing more work in the request than is required. I ended up having a lot of non-url type entries in my array, so I ended up adding code to loop through the elements and remove elements that didn’t hold an https url.

Does your submission now pass from the original request? After fixing the non-url types in the array?

If you’ve amended the tests in the submission request and submit for a badge, it will fail the submission.

No, the submission does not pass the original test. So, when submitting for badge, this fails :frowning:

I got it to work by doing a JSON.stringify() to the variable value & then persisting that value to the collection variable (which resulted in the variable string starting with the beginning square bracket).