Skip to content

Commit

Permalink
[FEATURE] Add iframe coverage support (#194)
Browse files Browse the repository at this point in the history
Adds helper module to modify the karma config such that coverage reporting works within iframes.

Co-authored-by: Tobias Sorn <tobias.sorn@sap.com>
  • Loading branch information
matz3 and tobiasso85 committed Jun 16, 2020
1 parent 751a2f9 commit 3b5e67b
Show file tree
Hide file tree
Showing 17 changed files with 856 additions and 27 deletions.
58 changes: 43 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@
**Table of Contents**
- [About](#about)
- [Quickstart](#quickstart)
- [Installation](#installation)
- [Configuration](#configuration)
- [Execution](#execution)
- [Karma configuration requirements](#karma-configuration-requirements)
- [Installation](#installation)
- [Configuration](#configuration)
- [Execution](#execution)
- [Karma Configuration Requirements](#karma-configuration-requirements)
- [Options](#options)
- [url](#url)
- [type](#type)
- [paths](#paths)
- [configPath](#configPath)
- [mode](#mode)
- [html](#html)
- [script](#script)
- [testpage](#testpage)
- [urlParameters](#urlParameters)
- [config](#config)
- [tests](#tests)
- [url](#url)
- [type](#type)
- [paths](#paths)
- [configPath](#configpath)
- [mode](#mode)
- [html](#html)
- [script](#script)
- [testpage](#testpage)
- [urlParameters](#urlparameters)
- [config](#config)
- [tests](#tests)
- [API](#api)
- [helper](#helper)
- [configureIframeCoverage](#configureiframecoverage)
- [License](#license)


Expand Down Expand Up @@ -323,6 +326,31 @@ ui5: {
}
```

## API

### helper

This plugin also comes with a helper module to be used in your Karma configuration file.

#### configureIframeCoverage

Enables code coverage for iframes.
Can only be used in combination with the [karma-coverage](https://github.com/karma-runner/karma-coverage) plugin (v2.0.0+).

Must be called from the karma configuration function after the coverage plugin has been configured.
The `config` object must be passed as a parameter.

```js
module.exports = function(config) {
config.set({

// ...

});
require("karma-ui5/helper").configureIframeCoverage(config);
};
```

## License
(c) Copyright 2019 SAP SE or an SAP affiliate company

Expand Down
20 changes: 20 additions & 0 deletions helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Enables coverage reporting when an iframe is used.
*
* Will set/overwrite the config option:
* - config.coverageReporter.instrumenterOptions.istanbul.coverageGlobalScope
*
* @param {object} config karma configuration object
*/
const configureIframeCoverage = (config) => {
// set the coverageGlobalScope
config.coverageReporter = config.coverageReporter || {};
config.coverageReporter.instrumenterOptions = config.coverageReporter.instrumenterOptions || {};
config.coverageReporter.instrumenterOptions.istanbul = config.coverageReporter.instrumenterOptions.istanbul || {};
config.coverageReporter.instrumenterOptions.istanbul.coverageGlobalScope =
"(function() { var g=window;while(!g.__karma__&&g!==g.parent){g=g.parent;}; return g; })();";
};

module.exports = {
configureIframeCoverage
};
2 changes: 2 additions & 0 deletions lib/client/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ require("./discovery.js");
testWindow = null;
runTestPage(i + 1);
} else {
// Also merge coverage results from karma window
mergeCoverage(window.__coverage__);
karma.complete({
coverage: coverageMap ? coverageMap.toJSON() : undefined
});
Expand Down
Loading

0 comments on commit 3b5e67b

Please sign in to comment.