0

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

RH-st
  • 97
  • 2
  • 10
  • Did you read the docs? They are here: https://learning.postman.com/docs/sending-requests/cookies/ The correct syntax is completely laid out for you. – matt Sep 26 '21 at 15:05

1 Answers1

1

'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");
    });
    
});
lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20
  • Looks like I'm close, but I'm still getting `CookieStore: programmatic access to "theActualURLWasDeleted" is denied"}` with this code. I did enable the url in the whitelist. Thoughts? Just trying to log cookieNames. – Kreidol May 24 '23 at 00:54