Test does not fail but creates an error

I’m trying to use the below syntax to trigger a GET call within a test, and perform some tests on the response.

pm.test(`example GET call`, () => {
	pm.sendRequest(`https://postman-echo.com/get`, (error, response) => {
        pm.expect(response).to.have.property('code', 201);
	});
});

This works fine as long as the tests passes. However, when the expect fails I don’t get a red block marking the test as failed. Instead I get the below error and the test run completely stops.

The log also doesn’t give me much to work with

When I do the same test from a separate Postman Request I do get the result I’m expecting

How can I make it so the test fails as shown above, when triggering the call within a test?

Took me quite a while to figure this out, but it turns out the solution is to not trigger the call in the pm.test, but the other way around.

This does not work:

pm.test(`example GET call`, () => {
	pm.sendRequest(`https://postman-echo.com/get`, (error, response) => {
        pm.expect(response).to.have.property('code', 201);
	});
});

This works just fine:

pm.sendRequest(`https://postman-echo.com/get`, (error, response) => {
       pm.test(`example GET call`, () => {
	        pm.expect(response).to.have.property('code', 201);
	});
});

Good work figuring this out!
(And props to you for sharing the solution in case others find this same issue :wink:)

1 Like