got
在测试中使用库时是否可以断言自定义错误消息?Got
如果响应> = 400,将引发错误,因此我可以断言响应代码(通过返回的字符串),但不是我自己的自定义错误消息。
it('Responds with a 404 if not found', async () => {
const paramA = 'a param that will make my function fail';
try {
await got(
`http://localhost:${port}/myEndpointName/${paramA}`,
);
} catch (error) {
// Would Like to assert on this statement below
// expect(error.message).toEqual("A custom error message of my selection");
// But instead I have to assert on this
expect(error.message).toEqual("Response code 404 (Not Found)");
}
});
export const doStuff: RequestHandler = async function (req, res, next) {
const { paramA } = req.params;
try {
const stuffFromElsewhere: NPMPackage = await got(
`https://someothersite.com/${paramA}`,
).json();
return res.status(200).json({ paramA, "good job" });
} catch (error) {
return res.status(404).json({ message: 'A custom error message of my selection' });
}
};