title: Testing for redirection using mocha and chai
body: <p>Testing for redirection using mocha and chai for expressjs always fails  "Uncaught AssertionError: expected redirect with 30{1-3} status code but got 200".
  Below is the test for registration page. After successful registration the test code is expecting a redirection to the login page. So the expected response code is <code>302</code>. But the response code I am getting is <code>200</code>.</p>
  
  <pre><code>it("Registration Check", done =&gt; {
      chai
        .request(server)
        .post("/register")
        .send({
          name: "pkv",
          email: "pkv@pkv.com",
          password: "pkv",
          cpassword: "pkv"
        })
        .end((err, res, body) =&gt; {
          res.should.have.status(302);
          done();
        });
    });
  </code></pre>
  
  <p>The express js code under test is as follows.On successful registration the response is redirected to the login page</p>
  
  <pre><code>   if (userInfo.password == userInfo.cpassword) {
    if (regexEmail.test(userInfo.email)) {
      models.user
        .find({
          email: userInfo.email
        })
        .then(user1 =&gt; {
          if (user1.length) {
            res.json({
              Err: "Error"
            });
            console.log("Already Existing user");
          } else {
            var newuser = new models.user({
              name: userInfo.name,
              email: userInfo.email,
              password: cpass
            });
  
            newuser.save(function(err, user) {
              if (err) {
                console.log("error");
              } else {
                models.user.find(function(err, response) {
                  res.redirect("/login");
                });
              }
            });
          }
        })
        .catch(err =&gt; {
          res.send("Error in database connection");
        });
    } else {
  
      res.json({
        err: "error"
      });
    }
  } else {
    res.json({
      Response: " Password Mismatch"
    });
  }
  </code></pre>