Search code examples
postmanpostman-newman

Postman & Newman - cookieJar.getAll() requires a callback function


I am trying to call a graphql and get the data from cookies, it runs well in postman app. However when I trying to run this postman collection on the command line with Newman

In terminal: newman run postman_collection.json -e environment.json

then it gave me the error

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, 
or by rejecting a promise which was not handled with .catch(). 
The promise rejected with the reason "TypeError: CookieJar.getAll() requires a callback function".] 
{
  code: 'ERR_UNHANDLED_REJECTION'
}

And the Test script code is like this

pm.test("Get a test data", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.data.createTest.success).to.eql(true);
});

pm.test("Test data cookies set", async function () {
    const cookieJar = pm.cookies.jar();
    const url = pm.environment.get("service-url");
    const cookies = await cookieJar.getAll(url);
    const cookieNames = cookies.map(cookie => cookie.name);
    pm.expect(cookieNames).to.include("test-token");
    pm.expect(cookieNames).to.include("legacy-test-token");
});

So I assume the error is because getAll() requires a callback function, Do you know what I'm doing wrong? How can I improve it, Can you help me solve this? Many thanks


Solution

  • 'it runs well in postman app' --> I doubt it. I tried and it always passed.

    I added a callback, also changed a setting Whitelist Domain in Postman GUI.

    pm.test("Test data cookies set", function () {
        const cookieJar = pm.cookies.jar();
        const url = pm.environment.get("service-url");
        cookieJar.getAll(url, (error, cookies)=> {
            if(error) console.log(error);
            
            const cookieNames = cookies.map(cookie => cookie.name);
            pm.expect(cookieNames).to.include("test-token");
            pm.expect(cookieNames).to.include("legacy-test-token");
        });
        
    });