Protractor coverage not generating report

2019-04-02 01:54发布

问题:

Backend of our app is in PHP and for frontend we are using AngularJs. We successfully managed to run e2e tests on local as well as on production server using protractor.

After writing loads of e2e tests for our app, we started looking for its coverage similar to that of unit testing. After searching for lot, luckily we find https://www.npmjs.com/package/grunt-protractor-coverage , exactly what we wanted.

I took help from http://lkrnac.net/blog/2014/04/measuring-code-coverage-by-protractor/ article which beautifully helps in setting up everything. I setup the config and other grunt tasks for my app, and finally our code(js files) were properly instrumented. I copied the rest of the files(html, static, etc.) to that instrumented code and provided the correct path for the proractor-config file. Tests started running as they were running before, but this time with instrumented files.

Till this point, everything is OK. But when the task for generating coverage-report was executed, we figured that we had empty coverage.json file {}. This means the report will surely be empty as it reads that file to generate report, and as far as I have figured out, this file is generated by protractor-coverage grunt task while tests are executing. It sends the information to the collector(port: 3001) using a POST req and while generating report, a GET req is being made to the same collector.

So, what I figured is, No POST req is being made to collector.

var options = {
  hostname: 'localhost',
  port: <%=collectorPort%>,
  path: '/data',
  method: 'POST',
  headers:{
    'Content-Type':'application/json'
  }
};
function saveCoverage(data){
  var req = http.request(options, function(res) {
      res.on('data', function (chunk) {
      });
  });
  req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
  });

  // write data to request body
  req.write(JSON.stringify(data));
  req.write('\n');
  req.end();
}

Each time it just shows where it should have listed down every file:

And also, that 100 everywhere is misleading, I ran tests for the source code: http://lkrnac.net/blog/2014/04/measuring-code-coverage-by-protractor/ as explained, but even if there's only one e2e test, the report must have given the actual numbers instead of giving a straight 100 for all.

It might happen that I have some wrong configuration or missed something.

Below are my files:

'use strict';

module.exports = function(grunt) {

  // Load grunt tasks automatically
  require('load-grunt-tasks')(grunt);

  // Define the configuration for all the tasks
  grunt.initConfig({

    // Project settings
    yeoman: {
      // configurable paths
      app: 'app',
      dist: 'dist-test',
      e2e: 'coverage/e2e',
      instrumentedServer: 'coverage/server/instrument',
      instrumentedE2E: 'coverage/e2e/instrumented'
    },
    // Empties folders to start fresh
    clean: {
      coverageE2E: {
        src: ['<%= yeoman.e2e %>/'],
      }
    },

    // Copies remaining files to places other tasks can use
    copy: {
      coverageE2E: {
        files: [{
          expand: true,
          dot: true,
          cwd: '<%= yeoman.app %>',
          dest: '<%= yeoman.e2e %>/instrumented/app',
          src: [
            '**/*',
            '!modules/**/*.js',
            '!editor/**/*.js'
          ]
        }, {
          expand: true,
          cwd: '.tmp/images',
          dest: '<%= yeoman.e2e %>/instrumented/app/images',
          src: ['generated/*']
        }, ]
      },
    },

    // start - code coverage settings
    instrument: {
      files: ['app/modules/**/*.js', 'app/editor/**/*.js'],
      options: {
        lazy: true,
        basePath: 'coverage/e2e/instrumented/'
      }
    },

    makeReport: {
      src: '<%= yeoman.instrumentedE2E %>/*.json',
      options: {
        type: 'html',
        dir: '<%= yeoman.e2e %>/reports',
        print: 'detail',
        //        type: 'lcov'
        //        dir: 'reports'
      }
    },


    protractor_coverage: {
      options: {
        configFile: 'test/e2e/protractor-config.js', // Default config file
        keepAlive: true, // If false, the grunt process stops when the test fails.
        noColor: false, // If true, protractor will not use colors in its output.
        coverageDir: '<%= yeoman.instrumentedE2E %>',
        args: {},
        run: {}
      },
      chrome: {
        options: {
          args: {
            baseUrl: 'https://localapp.vwo.com/v3/#/',
            // Arguments passed to the command
            'browser': 'chrome'
          }
        }
      }
    }
  });

  grunt.registerTask('default', [
    'clean:coverageE2E',
    'copy:coverageE2E',
    'instrument',
    'protractor_coverage:chrome',
    'makeReport'
  ]);
};

And my coverage.json file:

{}