2

I have a route similar to the below, and I am trying to wrap my head around how to test this using Mocha, Chai, and SinonJS.

router.get("/favorites", async function(req, res) {
    try {
        let cachedItem = cache.get("FAVORITES");
        if (cachedItem) {
            res.status(global.HttpCode.Ok).send(cachedItem);
        } else {
            var pool = await poolPromiseWebMart;
            let result = await pool.request().execute("dbo.GetFavorites");
            cache.put("FAVORITES", result.recordset, ONE_MINUTE * NUMBER_OF_MINUTES);
            res.status(global.HttpCode.Ok).send(result.recordset);
        }
    } catch (error) {
        return req.logHelper.log(error, "dbo.GetFavorites");
    }
});

I have just started looking into SinonJS and figuring that I will need to stub out these dependencies but not really sure where to start. Can a route even be called without the server running?

I have tried calling it using

return request(app)
  .get("/favorites")
  .then(function(response) {
     //Testing here
  })

But I run into a lot of issues with dependencies. Thanks in advance!

RJ7
  • 883
  • 9
  • 17
  • 2
    Possible duplicate of [How to unit test express Router routes](https://stackoverflow.com/questions/35234718/how-to-unit-test-express-router-routes) – lumio Apr 18 '19 at 13:20

0 Answers0