Day 16 - Failing "Scripts added correctly" test

Hi everyone -

I am having trouble passing the ā€œScripts added correctlyā€ test in my submit request for the Day 16 - Pagination challenge.

Here is the error Iā€™m seeing:

And here is the script I wrote to execute the request over and over until it hits a status code other than 200:

This script works, because the run failed on page 404, which from my research looks like itā€™s correct.

Iā€™m not sure what Iā€™m doing wrong and would appreciate any and all help!

Thanks!

Hey @altimetry-specialis8 :wave:

Welcome to the Postman Community! :postman:

Thereā€™s potentially a carriage return character hidden there and it getting picked up in the test. Did you copy and paste that code in the Tests sandbox from some other place.

You could try writing it out again or using the control/command + B to format the script, that might remove that extra hidden character?

1 Like

Danny has probably nailed it on the head in why the test is failing on the submit request.

However, your test is not doing what I think you expect it to do.

Postman uses Chai.JS under the hood for its assertion library.

Chai doesnā€™t have the concept of soft assertions.

As soon as a test fails within an pm.test block, it will stop executing any further code in that block.

This includes the pm.response.to.have.status(200) type tests as well as the pm.expect type tests.

Consider the followingā€¦

pm.test("Status code is 200", () => {
    pm.response.to.have.status(300);
    console.log("Hello");
});

The console log will never trigger if the test fails.

Therefore, your IF statement will never trigger if the status is anything other than 200.

By having that test as the first line in your test block, you are sort of negating the IF statement, as that will only ever run if the status code is 200.

You can also consider the following logicā€¦

pm.test("Status code is 200", () => {
    postman.setNextRequest("null");
    pm.response.to.have.status(200);
    postman.setNextRequest("xkcd");
});

and the following is mercilessly cribbed from how the submit requests work.

// counter for passed tests
let pass = 0;
let totalToPass = 1;

pm.test("Status code is 200", () => {
    pm.response.to.have.status(200);
    pass += 1;
});

if (pass == totalToPass) {
    console.log("pass");
    postman.setNextRequest("xkcd");
} else {
    console.log("fail");
    postman.setNextRequest("null");
}
2 Likes

Thank you for this! I tried reformatting and rewriting the same code, and Iā€™m still seeing the same error. I didnā€™t copy it from anywhere originally.

I will keep working on it.

Thank you for this info and perspective! Itā€™s really helped me to understand what goes on behind the scenes.

Iā€™ve tried reformatting like Danny suggested, and Iā€™ve moved around the logic in my test like you recommended, but Iā€™m still seeing the same error of AssertionError: pm.test: expected [ ā€¦(5) ] to include ā€˜postman.setNextRequest(null)ā€™.

Iā€™m not sure how to proceed.

This is the code for the failing test. (In the tests tab for the submit request).

pm.test("Scripts added correctly", () => {
    let xkcdRequest = collection.item[0].item.find(req => { return req.name === "xkcd"})
    let prereq = xkcdRequest.event.find(event => { return event.listen === "prerequest"})
    pm.expect(prereq.listen, 'check prerequest script').equals("prerequest")

    let test = xkcdRequest.event.find(event => { return event.listen === "test" })
    pm.expect(test.listen, 'check test script').equals("test")
    pm.expect(test.script.exec.toString(), 'check test script syntax').to.include("postman.setNextRequest(null)", "pm.test")

    pass += 1
})

In particular, its this assertion that is currently failing.

pm.expect(test.script.exec.toString(), 'check test script syntax').to.include("postman.setNextRequest(null)", "pm.test")

This line is finding the request in the collection.

let xkcdRequest = collection.item[0].item.find(req => { return req.name === "xkcd"})

This is returning the test event within the collection (aka Test tab).

let test = xkcdRequest.event.find(event => { return event.listen === "test" })

Itā€™s a bit more than just the code in the tests tab at this point, but this part of the assertion will return all of the code you have in the tests tab as a string and the test is simply testing that somewhere in the code it contains the text ā€œpostman.setNextRequest(null)ā€ and ā€œpm.testā€.

test.script.exec.toString()

My recommendation would be to clone the submit request, and then edit the test to console log the above variable just before it does the assertion to see what its returning. As it doesnā€™t seem to be returning the tests tab, but something else. It looks like its returning an array of some sort.

 let test = xkcdRequest.event.find(event => { return event.listen === "test" })
    pm.expect(test.listen, 'check test script').equals("test")
    console.log(test.script.exec.toString());
    pm.expect(test.script.exec.toString(), 'check test script syntax').to.include("postman.setNextRequest(null)", "pm.test")
2 Likes

Thank you so much for breaking this down! Iā€™m passing all tests now.

In my rush to try to figure this out, I obviously at some point removed the ā€œ.toString()ā€ from the ā€œpm.expect(test.script.exec.toString(), ā€˜check test script syntaxā€™).to.include(ā€œpostman.setNextRequest(null)ā€, ā€œpm.testā€)ā€ line. I tacked it back on to ā€œpm.expect(test.script.execā€¦)ā€ and now all tests are passing. :roll_eyes: :crazy_face:

Thank you again for the time youā€™ve spent here helping me!

Margaret

1 Like

Glad its working.

Iā€™m not 100% sure that assertion is working correctly though.

I noticed that the original error was showing ā€œpm.testā€ in the error where I would have expected it to show ā€œcheck test script syntaxā€. This is usually done to help you pinpoint any failing tests.

Expect allows you to include arbitrary messages to prepend to any failed assertions that might occur. This comes in handy when being used with non-descript topics such as booleans or numbers. (I cribbed this paragraph from the assertion styles information on the Chaijs website).

Iā€™m not sure you can actually perform an assertion on two elements like that.

For example.

pm.test("Assertion test 1", () => {
    pm.expect("hello goodbye", 'custom error message 1').to.include("hello", "blah")
});

This is actually passing.

Its probably because it thinks the second element is another custom error message, and as its the last one in the list, it is taking precedence over the first one. Therefore its only really testing that ā€œhello goodbyeā€ includes ā€œhelloā€ which it does.

That would mean that the test on the submit request is only really testing for ā€œpostman.setNextRequest(null)ā€ and ā€œpm.testā€ is only used as a custom error message.

Pretty sure that was not the original intention.

If you want to check two strings, then this does work (and displays the correct error message).

pm.test("Assertion test 2", () => {
    pm.expect("hello goodbye", 'custom error message 2').to.include("hello").and.include("Goodbye");
});

image

It passes if I correct the spelling of goodbye.

@danny-dainton What do you think. Is that assertion in the submit request working as expected?

1 Like

@michaelderekjones Iā€™ll take a look at that today :pray:t2:

Just looking at the Chai docs, you can see that itā€™s not correct or doing what is expected. :grimacing:

That second arg in the .include() function is a optional message :man_facepalming:

Iā€™ve taken a note of this and will look closer at this one and the other assertions to ensure these are actually working as expected. As ever, thank you for highlighting this @michaelderekjones :trophy:

1 Like

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