bennyhat / protractor-istanbul-plugin

Protractor plugin that collects istanbul coverage results from a page and dumps them to coverage.json files (one per spec).
MIT License
15 stars 7 forks source link

abandoned

Many apologies. I don't have time to keep up with the questions and issues I get on this. Most of them are meritted, as integration testing setups all vary wildly. However, this, paired with the limited time on my part, the fact that protractor's plugin API changes rapidly, and other factors, means I can't really keep this thing going.

Please fork if desired. If you'd like to take-over the NPM module, please let me know. Thanks, Ben

protractor-istanbul-plugin

Build Status Coverage Status

Protractor plugin that collects istanbul coverage results from a page and dumps them to coverage.json files (one per spec).

what

This plugin will gather JavaScript coverage details for each spec executed by protractor and dump them to a json file for further processing by istanbul.

caveats and other details:

how

basic

If you just need to get it running and dumping json files to its default output directory of ./coverage then you can add it into your protractor configuration file like this (path is used b/c sometimes protractor can be a bit flaky with actually just using the package option):

  ...
  exports.config = {
    ...
    plugins : [{
      path: 'node_modules/protractor-istanbul-plugin'
    }],
    ...

When it has completed it will have written one json file per spec to ./coverage. The files will be named with uuid.v4 strings. These files can then be bundled into an lcov.info file and html report using istanbul:

istanbul report --include=coverage/*.json

logging and assertions

If you would like it to log any failures from trying to gather coverage data, then you can turn that on using the logAssertions option:

  ...
 plugins : [{
    path: 'node_modules/protractor-istanbul-plugin',
    logAssertions: true
  }],
  ...

If you would like it to actually fail the tests if coverage data was not gathered, then you can turn that on using the failAssertions option (it's a good idea to turn on the logging too, in this case):

  ...
 plugins : [{
    path: 'node_modules/protractor-istanbul-plugin',
    logAssertions: true,
    failAssertions: true
  }],
  ...

edges cases

In some cases, the coverage data can get removed by test actions, such as history clears and server side navigation. To get around this, the plugin is able to accept an array of slightly modified functions that it will wrap with coverage preservation. Each function that gets wrapped needs to have two extra properties (until I can find a less janky way of doing it): boundParent, the function's expected parent object during regular test calls, and boundName, the function name that it will be stored under for the parent object.

...
// minor fix-up of the wrappable functions
commonTestUtils.clearHistory.boundParent = commonTestUtils;
commonTestUtils.clearHistory.boundName = 'clearHistory';
...
  plugins : [{
    path: 'node_modules/protractor-istanbul-plugin',
    functions: [ commonTestUtils.clearHistory ]
  }],
...
// afterwards a jasmine test runs in tests/someSpec.js
...
describe('my spec', function () {
  ...
  searchButton.click();
  expect(searchResultMessage).toBe('something');
  commonTestUtils.clearHistory();
  ...
}
...

Now, when the commonTestUtils.clearHistory function gets called by a spec, the plugin will first store the __coverage__ object into memory, then call clearHistory, then restore __coverage__ from memory back onto the page for gathering later after the full spec has finished. NOTE: this should rarely have to be used unless your test setup has some pretty entrenched problems.

all options

Here are all of the options, along with their associated defaults:

why

There are a few tools out there that kind of fill this niche, namely the grunt-protractor-coverage tool, which makes similar assumptions about istanbul already being run, etc.

However, the protractor-istanbul-plugin was written with the following needs in mind:

misc. and TODO

This is confirmed to work with Protractor v2.1.0.

As for future plans: