Async test fails to assert response body

48 Views Asked by At

I can't explain why my test passed although the HTTP status code differs from the expected value.

Here is the test:

describe("Test", (): void => {  
    test(`${prefix}` + ": fake field",
    async (): Promise<void> => {
    const stakingRequest : StakingRequest = new StakingRequest(address, address, address, address, amount, "TestField");
        console.log("Request:" + JSON.stringify(stakingRequest))
        console.log("Endpoint:" + `${config.api.path}${EndPoint.STAKE}`)

        await new Promise(resolve => setTimeout(resolve, wait));

        await instance.post(`${config.api.path}${EndPoint.STAKE}`, stakingRequest.createRequest())
        .then(
            r => {
                console.log("Response:");
                console.log(r.data);
                console.log(r.status);
                console.log(r.statusText);
                expect(r. status).toBe(HttpStatusCode.BadRequest); //actual result HttpStatusCode.OK
            }
        ).catch(error => {
            if (error.response) {
                expect(error.response).toBe("")
            }
        })
}, timeout);
});

The problem is that the request returns HttpStatusCode.OK (200). However, the test seems to ignore any assertions (expect()) and executes successfully.

0

There are 0 best solutions below