'Function returning false in mocha test but should return true

I am writing tests for a typescript function using mocha and chai

it('Post new', async function () 

        const response = await postRequest();
      
        expect(response).to.be.true;
    })

The test fails because response is false. The wierd thing is that if I call postRequest() from a diffrent function it returns true everytime. I can also see that it logs in the if statement that is supposed to return true. So why is the mocha test getting false?

this is the code for postRequest:

export async function postRequest(): Promise<boolean> {

    const response = await fetch(url, params);

    if(response.status === 201) {
        console.log("here");
        return true;
    } 

    console.log("here too");
    return false;
    
}

the "here too" log gets displayed first and later the "here"log so I guess it does not wait for the fetch(?). If I log response.status it is 201.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source