Functional test for stock price checker

Every time i run these test on chai it does not do what is done on the site. For example, I have made a condition where if the stock and api is found then we return an error like so

stockHasIp = await Stock.findOne(
          { stock: stockName, ips: ipa },
          (error, result) => {
            if (error) return error;
            else if (!error && result) {
              return result;
            }
          }
        );
        console.log(stockHasIp, "<= stockHasIp");
        // If stock has ip address then don't update and send error message
        if (stockHasIp) {
          return res.json({ error: "only 1 like per IP address." });
        } 

This works and will send an error if user is using same ip address on heroku. But when I run these two tests it will say stockHasIp is null (meaning it did not find it in database and thus add another like instead of sending error message)

  test("Viewing one stock and liking it: GET request to /api/stock-prices/", (done) => {
    chai
      .request(server)
      .get("/api/stock-prices")
      .query({ stock: "GE", like: true })
      .end((err, res) => {
        assert.equal(res.status, 200);
        assert.equal(res.body.stockData.stock, "GE");
        assert.equal(res.body.stockData.likes, 1);
      });
    done(); 
  });

  // DOESN'T WORK FOR GOD KNOWS WHY. WORKS WHEN USING IT ON SITE AND REFRESHES
  test("Viewing the same stock and liking it again: GET request to /api/stock-prices/", (done) => {
    chai
      .request(server)
      .get("/api/stock-prices")
      .query({ stock: "GE", like: true })
      .end((err, res) => {
        window.location.reload();
        console.log(res.body, "<= res.body");
        assert.equal(res.body.error, "only 1 like per IP address.");
      });
    done(); 
  });

The second last test shown is the problem. It should pass and I am not sure why it responds with adding another like instead of sending error message because on heroku app it will send the error message.

Your project link(s)
https://stock-price-checker-project.herokuapp.com/

GitHub for full code

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15.

Challenge: Stock Price Checker

Link to the challenge:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.