lundi 3 août 2020

Why is Chai HTTP error response a html document?

I'm trying to setup a testing framework using Mocha and Chai for my express server application. It's generally a pretty standard application, and I've set up a middleware error handler which catches all errors from the API route functions and sends a response accordingly. Each error response has the following attributes: name, message, status.

In my test, I run the following code:

const res = await chai
  .request(server)
  .post(API_ENDPOINT)
  .send(REQUEST_BODY)

I get the response body from res.text, which I apply JSON.parse on to retrieve whatever attributes from the response body I need to perform the relevant assertions. However, when my server throws an error (desired behavior), the res.text value becomes a HTML document instead of a stringified JSON:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>ERROR MESSAGE</pre>
</body>
</html>

Can anyone explain what is going on here? I can't find any similar threads for this issue.

Aucun commentaire:

Enregistrer un commentaire