Search code examples
node.jsgruntjscode-coveragegrunt-contrib-connect

I Need enable livereload in my coverage test report


I've been trying enable livereload for my coverage report. My tool for generate this report is karma-coverage and I generate this report in the following folder: test/coverage/html-report

 -test
 |
 |-- coverage
     |
     |-- report-html
         |
         |-- index.html

When the report was generated I use grunt-contrib-connect and serve the generated report using the following:

 var COVERAGE_BASE = './test/coverage/report-html',

   ...
   ...
   ...
   connect: {
      server: {
        options: {
          port: 9000,
          base: '.',
          open: false
        }
      },
      dist:{
        options: {
          keepalive: true,
          port: 9000,
          base: './dist',
          open: false
        }
      },
      coverage: {
        options: {
          keepalive: true,
          port: 9001,
          base: COVERAGE_BASE ,
          open: false
        }
      }
    }

When I execute my task grunt coverage, my configuraton it's

COVERAGE_TASKS = ['shell:test','open:coverage', 'connect:coverage','watch'];

grunt.registerTask('coverage', COVERAGE_TASKS);

My idea it's "intercept" the index.html and inject a script to livereload

<script src="//localhost:35729/livereload.js"></script>

Exist some way to intercept the index.html before serve with connect package and process it for adding this script?


Solution

  • May be this helps: you can add middle ware that can be used to intercept request to your connect server like this:

    grunt.initConfig({
      connect: {
        server: {
          options: {
            middleware: [
              function myMiddleware(req, res, next) {
                
                // DO YOUR THING HERE!!!!
                res.end('Hello, world!');
              }
            ],
          },
        },
      },
    });