Skip to content

Commit ebe7fb8

Browse files
committed
Extended the Karma config to get better code coverage and test reports
1 parent a8bff07 commit ebe7fb8

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

config/karma.config.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
var existingKarmaConfig = require('@microsoft/sp-build-web/lib/karma/karma.config');
3+
var gulp_core_build = require("@microsoft/gulp-core-build");
4+
var htmlReporter = require('karma-html-reporter');
5+
var path = require('path');
6+
7+
var remapCoverageReporter = require('karma-remap-coverage');
8+
9+
module.exports = function (config) {
10+
existingKarmaConfig(config);
11+
// Add the HTML reporter
12+
config.reporters.push('html');
13+
config.htmlReporter = {
14+
outputDir: path.join(gulp_core_build.getConfig().tempFolder, 'karma-html-report'), // where to put the reports
15+
templatePath: null, // set if you moved jasmine_template.html
16+
focusOnFailures: true, // reports show failures on start
17+
namedFiles: true, // name files instead of creating sub-directories
18+
pageTitle: 'sp-dev-fx-controls-react unit-tests report', // page title for reports; browser info by default
19+
urlFriendlyName: false, // simply replaces spaces with _ for files/dirs
20+
reportName: 'sp-dev-fx-controls-react-report', // report summary filename; browser info by default
21+
};
22+
config.plugins.push(htmlReporter);
23+
24+
// Add the remap-coverage - code coverage for the original files
25+
config.reporters.push('remap-coverage');
26+
config.coverageReporter = {
27+
type: 'in-memory'
28+
}
29+
config.remapCoverageReporter = {
30+
'text-summary': null,
31+
html: path.join(gulp_core_build.getConfig().tempFolder, 'coverage/html'),
32+
cobertura: path.join(gulp_core_build.getConfig().tempFolder, 'coverage/cobertura.xml')
33+
};
34+
config.plugins.push(remapCoverageReporter);
35+
};

gulpfile.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
'use strict';
22

33
const gulp = require('gulp');
4+
const _ = require('lodash');
45
const build = require('@microsoft/sp-build-web');
56

67
build.initialize(gulp);
8+
9+
var buildConfig = build.getConfig();
10+
var karmaTask = _.find(buildConfig.uniqueTasks, ['name', 'karma']);
11+
karmaTask.taskConfig.configPath = './config/karma.config.js';

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@types/sinon": "2.3.4",
3434
"enzyme": "2.9.1",
3535
"gulp": "~3.9.1",
36+
"karma-remap-coverage": "^0.1.4",
3637
"react-addons-test-utils": "15.6.0"
3738
},
3839
"repository": {

0 commit comments

Comments
 (0)