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!


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?

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 type tests as well as the pm.expect type tests.

Consider the following…

pm.test("Status code is 200", () => {;

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", () => {

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", () => {;
    pass += 1;

if (pass == totalToPass) {
} else {

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 === "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 === "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”.


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")
    pm.expect(test.script.exec.toString(), 'check test script syntax').to.include("postman.setNextRequest(null)", "pm.test")
1 Like

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!


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");


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?

@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.