title: Testing a jsonwebtoken authentication express middleware
body: <p>I am currently learning to do testing in general and I am not sure how one tests an express middleware that authenticates a jsonwebtoken. An express middleware is basically a function so in theory it should be unit testable. Could someone show me how one would do so? Here is my middleware so far. I am currently using mocha and chai as my testing framework.</p>
  
  <pre><code>import jwt from "jsonwebtoken";
  import secret from "../jwtSecret"
  
  function authenticateJwt(req, res, next){
    let token;
    let authorizationHeaders = req.headers["authorization"];
    if(authorizationHeaders){
      token = authorizationHeaders.split(" ")[1];
    }
    if(token) {
      jwt.verify(token, secret, (err, decodedToken) =&gt; {
        if(err){
          res.status(403).json({
            success: false,
            error: "Invalid token provided"
          });
        } else {
          next();  
        }
      });
    } else {
      res.status(403).json({
        success: false,
        error: "No token provided"
      });
    }
  }
  
  export default authenticateJwt;
  </code></pre>