title: How do i test index.js for nodejs with env param?
body: <p>I have the following index.js for my node js. When i run my coverage, it is telling me that else path is not taken for this line: <code>if(enableCompression) {</code>.</p>
  
  <p>My <code>.env</code> is setting <code>process.env.ENABLE_COMPRESSION = 'true'</code> by default.</p>
  
  <p>How do I test that path? I tried setting <code>process.env.ENABLE_COMPRESSION = 'false'</code> in my test case  but it does not rerun the index.js again as the server is already started. </p>
  
  <p>How do i go about doing it? I am using sinon for my unit test.</p>
  
  <p><strong>index.js:</strong></p>
  
  <pre><code>require('dotenv').config({ path: require('find-config')('.env') });
  
  const PORT = process.env.PORT;
  const ENABLE_COMPRESSION = process.env.ENABLE_COMPRESSION;
  
  let express = require('express');
  let compression = require('compression');
  
  //logging
  let log4js = require('log4js');
  log4js.configure('./config/log4js.json');
  
  //Please do not remove this line, since CLI uses this line as guidance to import new controllers
  let app = express();
  
  let enableCompression = ENABLE_COMPRESSION === "true";
  
  if(enableCompression) {
      app.use(compression());
  }
  
  app.use(log4js.connectLogger(log4js.getLogger("app"), { level: 'auto' }));
  
  let log = log4js.getLogger("api-app");
  
  //importing route
  let routes = require('./api/rest/apiRest');
  
  //register the route
  routes(app);
  
  //reached here throw error 404, means no routes to handle inc. request
  app.use((req, res, next) =&gt; {
      const error = new Error('Not found');
      error.status = 404;
  
      next(error);
  });
  
  // catch errors
  app.use((error, req, res, next) =&gt; {
  
      log.error("ERROR - " + req.url + " - " + error.message);
  
      res.status(error.status || 500);
      res.json({
          error: {
              message: error.message
          }
      });
  });
  
  //start application
  module.exports = app.listen(PORT, () =&gt; {
      log.info('API server started on: ' + PORT);
  });
  </code></pre>