Postman throws "AssertionError" in console and Test Results pane instead of failed test

Hi! I have a strange case

In one test, after making the main request, I have to make an extra api request to validate that the main request updated some info correctly

So I go something like

pm.sendRequest(SCAN_REQUEST, (err, res) => {
            pm.expect(res.code).to.eq(200);
            scanRequest = Object.keys(res.json().Data.Entities[0]);
            pm.expect(scanRequest.includes('Error')).to.be.false
        })

But when I run it, if the scanRequest object includes the “Error” element, instead of a failed test in the Test Results pane, I get a red sign with “There was an error in evaluating the test script: Error: expected true to be false”, and in console I get “AssertionError: expected true to be false”

Anyone knows what I am doing wrong?

Thanks

Hmm… ok… I think I “kinda” solved it, but I’m not sure if this would be the right way

What I did was running the pm.sendrequest PRIOR to writing the tests. In the sendRequest callback I saved the response to a variable, and then in the test I asserted against that variable that holds the response, but had to put that test inside a setTimeout function cuz it was completing prior to the request returning the response…

Basically


const SCAN_REQUEST = {
    url: "url",
    method: 'POST',
    header: {
        'Authorization': "Basic blah"
    },
    body: {
        body
    }
}

let scanResponse= Object;

pm.sendRequest({
    url: "url",
    method: "POST",
    header: {
        'Authorization': "Basic blah"
    }
}, (err, res) => {

});

pm.sendRequest(SCAN_REQUEST, (err, res) => {
    scanResponse = Object.keys(res.json().Data.Entities[0]);
})

setTimeout(() => {
    pm.test('Validate permissions', function () {
        pm.expect(scanResponse.includes('Error')).to.be.false;
    })
}, 5000)

And now test fails correctly, no AssertionError exception

Does this make sense or is there a more elegant way to achieve it?

EDIT: this executes correctly when running from Postman… but when executing the test using newman, scanResponse seems to be empty when I do the expect