Skip to content

Commit

Permalink
feat(concatjs): enable junit report for karma_web_test (bazel-contrib…
Browse files Browse the repository at this point in the history
…#2630)

This PR enables junit reports for karma_web_test, by adding karma-junit-reporter as a
peer dependency and adding an appropriate configuration that works with bazel.
karma-junit-reporter is now a peer dependency of @bazel/concatjs
  • Loading branch information
kyubisation authored and twheys committed Jan 13, 2022
1 parent a094c00 commit f28cb7d
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"karma-chrome-launcher": "2.2.0",
"karma-firefox-launcher": "1.1.0",
"karma-jasmine": "2.0.1",
"karma-junit-reporter": "2.0.1",
"karma-requirejs": "1.1.0",
"karma-sourcemap-loader": "0.3.7",
"protractor": "^5.4.2",
Expand Down
13 changes: 13 additions & 0 deletions examples/angular/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4511,6 +4511,14 @@ [email protected]:
dependencies:
jasmine-core "^3.3"

[email protected]:
version "2.0.1"
resolved "https://registry.yarnpkg.com/karma-junit-reporter/-/karma-junit-reporter-2.0.1.tgz#d34eef7f0b2fd064e0896954e8851a90cf14c8f3"
integrity sha512-VtcGfE0JE4OE1wn0LK8xxDKaTP7slN8DO3I+4xg6gAi1IoAHAXOJ1V9G/y45Xg6sxdxPOR3THCFtDlAfBo9Afw==
dependencies:
path-is-absolute "^1.0.0"
xmlbuilder "12.0.0"

[email protected]:
version "1.1.0"
resolved "https://registry.yarnpkg.com/karma-requirejs/-/karma-requirejs-1.1.0.tgz#fddae2cb87d7ebc16fb0222893564d7fee578798"
Expand Down Expand Up @@ -7421,6 +7429,11 @@ xml2js@^0.4.17:
sax ">=0.6.0"
xmlbuilder "~11.0.0"

[email protected]:
version "12.0.0"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-12.0.0.tgz#e2ed675e06834a089ddfb84db96e2c2b03f78c1a"
integrity sha512-lMo8DJ8u6JRWp0/Y4XLa/atVDr75H9litKlb2E5j3V3MesoL50EBgZDWoLT3F/LztVnG67GjPXLZpqcky/UMnQ==

xmlbuilder@~11.0.0:
version "11.0.1"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3"
Expand Down
1 change: 1 addition & 0 deletions examples/web_testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"karma-chrome-launcher": "2.2.0",
"karma-firefox-launcher": "1.1.0",
"karma-jasmine": "2.0.1",
"karma-junit-reporter": "2.0.1",
"karma-json-result-reporter": "1.0.0",
"karma-requirejs": "1.1.0",
"karma-sourcemap-loader": "0.3.7",
Expand Down
13 changes: 13 additions & 0 deletions examples/web_testing/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,14 @@ [email protected]:
dependencies:
fun-map "^3.3.1"

[email protected]:
version "2.0.1"
resolved "https://registry.yarnpkg.com/karma-junit-reporter/-/karma-junit-reporter-2.0.1.tgz#d34eef7f0b2fd064e0896954e8851a90cf14c8f3"
integrity sha512-VtcGfE0JE4OE1wn0LK8xxDKaTP7slN8DO3I+4xg6gAi1IoAHAXOJ1V9G/y45Xg6sxdxPOR3THCFtDlAfBo9Afw==
dependencies:
path-is-absolute "^1.0.0"
xmlbuilder "12.0.0"

[email protected]:
version "1.1.0"
resolved "https://registry.yarnpkg.com/karma-requirejs/-/karma-requirejs-1.1.0.tgz#fddae2cb87d7ebc16fb0222893564d7fee578798"
Expand Down Expand Up @@ -2101,6 +2109,11 @@ ws@~3.3.1:
safe-buffer "~5.1.0"
ultron "~1.1.0"

[email protected]:
version "12.0.0"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-12.0.0.tgz#e2ed675e06834a089ddfb84db96e2c2b03f78c1a"
integrity sha512-lMo8DJ8u6JRWp0/Y4XLa/atVDr75H9litKlb2E5j3V3MesoL50EBgZDWoLT3F/LztVnG67GjPXLZpqcky/UMnQ==

xmlhttprequest-ssl@~1.5.4:
version "1.5.5"
resolved "https://registry.yarnpkg.com/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.5.tgz#c2876b06168aadc40e57d97e81191ac8f4398b3e"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"karma-chrome-launcher": "3.1.0",
"karma-firefox-launcher": "2.1.0",
"karma-jasmine": "4.0.1",
"karma-junit-reporter": "^2.0.1",
"karma-requirejs": "1.1.0",
"karma-sourcemap-loader": "0.3.8",
"lit-element": "^2.2.1",
Expand Down
1 change: 1 addition & 0 deletions packages/concatjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"karma-chrome-launcher": ">=2.0.0",
"karma-firefox-launcher": ">=1.0.0",
"karma-jasmine": ">=2.0.0",
"karma-junit-reporter": ">=2.0.0",
"karma-requirejs": ">=1.0.0",
"karma-sourcemap-loader": ">=0.3.0"
},
Expand Down
1 change: 1 addition & 0 deletions packages/concatjs/web_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ nodejs_binary(
"@npm//karma-chrome-launcher",
"@npm//karma-firefox-launcher",
"@npm//karma-jasmine",
"@npm//karma-junit-reporter",
"@npm//karma-requirejs",
"@npm//karma-sourcemap-loader",
"@npm//requirejs",
Expand Down
48 changes: 48 additions & 0 deletions packages/concatjs/web_test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ try {
conf[name] = value;
}

/**
* Helper function to override nested karma config values.
*/
function overrideNestedConfigValue(conf, name, value) {
const nameParts = name.split('.');
const finalName = nameParts.pop();
for (const property of nameParts) {
if (!(property in conf)) {
conf[property] = {};
}
conf = conf[property];
}
if (conf.hasOwnProperty(name)) {
console.warn(
`Your karma configuration specifies '${name}' which will be overwritten by Bazel`);
}
conf[finalName] = value;
}

/**
* Helper function to merge base karma config values that are arrays.
*/
Expand All @@ -136,6 +155,18 @@ try {
})
}

function tryRequire(packageName) {
try {
return require(packageName);
} catch (e) {
if (e && e.code === 'MODULE_NOT_FOUND') {
return undefined;
}

throw e;
}
}

/**
* Configuration settings for karma under Bazel common to karma_web_test
* and karma_web_test_suite.
Expand Down Expand Up @@ -208,6 +239,23 @@ try {
} else {
conf.client = {requireJsShowNoTimestampsError};
}

// Enable the junit reporter if the XML_OUTPUT_FILE environment variable
// is defined and the karma-junit-reporter package is installed.
// The configuration for the junit reporter will be created or overridden
// with the configuration required for bazel to work properly.
const testOutputFile = process.env.XML_OUTPUT_FILE;
const karmaJunitReporterPlugin = testOutputFile ? tryRequire('karma-junit-reporter') : undefined;
if (karmaJunitReporterPlugin) {
mergeConfigArray(conf, 'plugins', [
karmaJunitReporterPlugin,
]);

mergeConfigArray(conf, 'reporters', ['junit']);
overrideNestedConfigValue(conf, 'junitReporter.outputDir', path.dirname(testOutputFile));
overrideNestedConfigValue(conf, 'junitReporter.outputFile', path.basename(testOutputFile));
overrideNestedConfigValue(conf, 'junitReporter.useBrowserName', false);
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions packages/concatjs/web_test/test/karma/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ karma_web_test_suite(
srcs = glob(
["*.js"],
exclude = [
"coverage_source_uncovered.js",
"coverage_source.js",
"coverage.spec.js",
"unnamed-amd-module.js",
"init-test.js",
],
Expand All @@ -45,6 +48,20 @@ karma_web_test_suite(
],
)

karma_web_test_suite(
name = "coverage_test",
srcs = [
"coverage.spec.js",
"coverage_source.js",
"coverage_source_uncovered.js",
],
browsers = [
"@io_bazel_rules_webtesting//browsers:chromium-local",
"@io_bazel_rules_webtesting//browsers:firefox-local",
],
tags = ["native"],
)

custom_browser(
name = "custom_chrome",
browser = "@io_bazel_rules_webtesting//browsers:chromium-local",
Expand Down
27 changes: 27 additions & 0 deletions packages/concatjs/web_test/test/karma/coverage.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(function(factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
} else if (typeof define === 'function' && define.amd) {
define(
'build_bazel_rules_nodejs/packages/concatjs/web_test/test/karma/coverage.spec',
[
'require', 'exports',
'build_bazel_rules_nodejs/packages/concatjs/web_test/test/karma/coverage_source'
],
factory);
}
})(function(require, exports) {
'use strict';
Object.defineProperty(exports, '__esModule', {value: true});
var coverage_source_1 =
require('build_bazel_rules_nodejs/packages/concatjs/web_test/test/karma/coverage_source');
describe('coverage function', () => {
it('should cover one branch', () => {
expect(coverage_source_1.isString(2)).toBe(false);
});
it('should cover the other branch', () => {
expect(coverage_source_1.isString('some string')).toBe(true);
});
});
});
21 changes: 21 additions & 0 deletions packages/concatjs/web_test/test/karma/coverage_source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(function(factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
} else if (typeof define === 'function' && define.amd) {
define(
'build_bazel_rules_nodejs/packages/concatjs/web_test/test/karma/coverage_source',
['require', 'exports'], factory);
}
})(function(require, exports) {
'use strict';
Object.defineProperty(exports, '__esModule', {value: true});
function isString(input) {
if (typeof input === 'string') {
return true;
} else {
return false;
}
}
exports.isString = isString;
});
20 changes: 20 additions & 0 deletions packages/concatjs/web_test/test/karma/coverage_source_uncovered.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function(factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
} else if (typeof define === 'function' && define.amd) {
define(
'build_bazel_rules_nodejs/packages/concatjs/web_test/test/karma/coverage_source_uncovered',
['require', 'exports'], factory);
}
})(function(require, exports) {
'use strict';
Object.defineProperty(exports, '__esModule', {value: true});
// noting in this file should be required, so we can test the c8 feature all: true
// which will pick up files that aren't directly referenced by test files
// but are added to coverage as empty coverage
function notCalled(input) {
return input * 13;
}
exports.notCalled = notCalled;
});
30 changes: 30 additions & 0 deletions packages/concatjs/web_test/test/karma_typescript/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ filegroup(
],
)

ts_library(
name = "coverage_test_srcs",
srcs = [
"coverage_source.ts",
"coverage_source_uncovered.ts",
],
)

ts_library(
name = "coverage_test_spec_srcs",
srcs = ["coverage.spec.ts"],
deps = [
":coverage_test_srcs",
"@npm//@types/jasmine",
],
)

karma_web_test_suite(
name = "coverage_test",
browsers = [
"@io_bazel_rules_webtesting//browsers:chromium-local",
"@io_bazel_rules_webtesting//browsers:firefox-local",
],
tags = ["native"],
deps = [
# ts_library target, must be TS module
":coverage_test_spec_srcs",
],
)

jasmine_node_test(
name = "user_files_test",
srcs = [
Expand Down
10 changes: 10 additions & 0 deletions packages/concatjs/web_test/test/karma_typescript/coverage.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {isString} from './coverage_source';

describe('coverage function', () => {
it('should cover one branch', () => {
expect(isString(2 as any)).toBe(false);
});
it('should cover the other branch', () => {
expect(isString('some string')).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function isString(input: string) {
if (typeof input === 'string') {
return true;
} else {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// noting in this file should be required, so we can test the c8 feature all: true
// which will pick up files that aren't directly referenced by test files
// but are added to coverage as empty coverage
export function notCalled(input: number) {
return input * 13;
}
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6222,6 +6222,14 @@ [email protected]:
dependencies:
jasmine-core "^3.6.0"

karma-junit-reporter@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/karma-junit-reporter/-/karma-junit-reporter-2.0.1.tgz#d34eef7f0b2fd064e0896954e8851a90cf14c8f3"
integrity sha512-VtcGfE0JE4OE1wn0LK8xxDKaTP7slN8DO3I+4xg6gAi1IoAHAXOJ1V9G/y45Xg6sxdxPOR3THCFtDlAfBo9Afw==
dependencies:
path-is-absolute "^1.0.0"
xmlbuilder "12.0.0"

[email protected]:
version "1.1.0"
resolved "https://registry.yarnpkg.com/karma-requirejs/-/karma-requirejs-1.1.0.tgz#fddae2cb87d7ebc16fb0222893564d7fee578798"
Expand Down Expand Up @@ -10453,6 +10461,11 @@ xml2js@^0.4.17:
sax ">=0.6.0"
xmlbuilder "~9.0.1"

[email protected]:
version "12.0.0"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-12.0.0.tgz#e2ed675e06834a089ddfb84db96e2c2b03f78c1a"
integrity sha512-lMo8DJ8u6JRWp0/Y4XLa/atVDr75H9litKlb2E5j3V3MesoL50EBgZDWoLT3F/LztVnG67GjPXLZpqcky/UMnQ==

xmlbuilder@~9.0.1:
version "9.0.7"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-9.0.7.tgz#132ee63d2ec5565c557e20f4c22df9aca686b10d"
Expand Down

0 comments on commit f28cb7d

Please sign in to comment.