I am testing my microservices to see if they gracefully throw errors up from the source to client. And My tests are expecting an error to throw, but I am testing how to properly test for this. I am having some issues using axios and async await / promise error. How can I properly test this? just through status codes and response body?
Here is my client: My Acceptance Tests It is excepting an Error
it("Verify Throws Exception", async () => {
const shouldThrowError = await ApiHelper.makeAxiosCall({
badData,
});
expect(shouldThrowError).toThrow(new Error("Bad Request"));
expect(shouldThrowError).toBe(400); // Can I test for this to?
}, 10000);
ApiHelper
export async function updateData({
badData
}) {
let result = null;
result = await axios({
method: "POST",
url: `${process.env.MICRO_B_URL}/api/users`,
data: {
badData,
},
})
.then(({ data }) => data.data)
.catch(err => {
throw err;
});
return result
My Aggregator Service
export async function makeAnotherRestCall(
req: any,
res: Response,
next: NextFunction,
) {
const { badData } = req.body;
const hasBadData = validateData(badData)
if(hasBadData) throw new Error("Bad Request")
return res.status(200).json({
data,
});
I have middleware to format errors like so
export const handleError = (error: any, res: any, next: NextFunction) => {
return res.status(error.status || 500).json({
error: {
status: error.status || 500,
message: error.message || "Internal Server Error",
stack: error.stack,
},
});
};
Aucun commentaire:
Enregistrer un commentaire