How to add parameter name to error message in Postman test

Solution 1:

This function will be like this:

function checkProperty(path, value) {
    let property = path.split(".").at(-1);
    pm.expect(eval(path)).to.eql(value,`error en ${property}: ${pm.response.code}`);
}

Example:

const res = pm.response.json();

pm.test("check response", () => {
    checkProperty("res.headers.host", "postman-echo.com1");
    checkProperty("res.url", "http://postman-echo.com/get1");
})

Tested with http://postman-echo.com/get