From f28cb7d7fef1f18da58f389034edf4c6e311a3ac Mon Sep 17 00:00:00 2001 From: Lukas Spirig Date: Tue, 4 May 2021 23:44:39 +0200 Subject: [PATCH] feat(concatjs): enable junit report for karma_web_test (#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 --- examples/angular/package.json | 1 + examples/angular/yarn.lock | 13 +++++ examples/web_testing/package.json | 1 + examples/web_testing/yarn.lock | 13 +++++ package.json | 1 + packages/concatjs/package.json | 1 + packages/concatjs/web_test/BUILD.bazel | 1 + packages/concatjs/web_test/karma.conf.js | 48 +++++++++++++++++++ .../concatjs/web_test/test/karma/BUILD.bazel | 17 +++++++ .../web_test/test/karma/coverage.spec.js | 27 +++++++++++ .../web_test/test/karma/coverage_source.js | 21 ++++++++ .../test/karma/coverage_source_uncovered.js | 20 ++++++++ .../test/karma_typescript/BUILD.bazel | 30 ++++++++++++ .../test/karma_typescript/coverage.spec.ts | 10 ++++ .../test/karma_typescript/coverage_source.ts | 7 +++ .../coverage_source_uncovered.ts | 6 +++ yarn.lock | 13 +++++ 17 files changed, 230 insertions(+) create mode 100644 packages/concatjs/web_test/test/karma/coverage.spec.js create mode 100644 packages/concatjs/web_test/test/karma/coverage_source.js create mode 100644 packages/concatjs/web_test/test/karma/coverage_source_uncovered.js create mode 100644 packages/concatjs/web_test/test/karma_typescript/coverage.spec.ts create mode 100644 packages/concatjs/web_test/test/karma_typescript/coverage_source.ts create mode 100644 packages/concatjs/web_test/test/karma_typescript/coverage_source_uncovered.ts diff --git a/examples/angular/package.json b/examples/angular/package.json index 906fb17459..2be9e60b97 100644 --- a/examples/angular/package.json +++ b/examples/angular/package.json @@ -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", diff --git a/examples/angular/yarn.lock b/examples/angular/yarn.lock index 160033559d..59d755ba1d 100644 --- a/examples/angular/yarn.lock +++ b/examples/angular/yarn.lock @@ -4511,6 +4511,14 @@ karma-jasmine@2.0.1: dependencies: jasmine-core "^3.3" +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" + karma-requirejs@1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/karma-requirejs/-/karma-requirejs-1.1.0.tgz#fddae2cb87d7ebc16fb0222893564d7fee578798" @@ -7421,6 +7429,11 @@ xml2js@^0.4.17: sax ">=0.6.0" xmlbuilder "~11.0.0" +xmlbuilder@12.0.0: + 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" diff --git a/examples/web_testing/package.json b/examples/web_testing/package.json index be258109b1..3a5388b0e3 100644 --- a/examples/web_testing/package.json +++ b/examples/web_testing/package.json @@ -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", diff --git a/examples/web_testing/yarn.lock b/examples/web_testing/yarn.lock index bcc5057dcb..66ef039df0 100644 --- a/examples/web_testing/yarn.lock +++ b/examples/web_testing/yarn.lock @@ -1100,6 +1100,14 @@ karma-json-result-reporter@1.0.0: dependencies: fun-map "^3.3.1" +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" + karma-requirejs@1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/karma-requirejs/-/karma-requirejs-1.1.0.tgz#fddae2cb87d7ebc16fb0222893564d7fee578798" @@ -2101,6 +2109,11 @@ ws@~3.3.1: safe-buffer "~5.1.0" ultron "~1.1.0" +xmlbuilder@12.0.0: + 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" diff --git a/package.json b/package.json index f79226ed0b..1d7c7266e8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/concatjs/package.json b/packages/concatjs/package.json index 9bef007f34..3b4820c554 100644 --- a/packages/concatjs/package.json +++ b/packages/concatjs/package.json @@ -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" }, diff --git a/packages/concatjs/web_test/BUILD.bazel b/packages/concatjs/web_test/BUILD.bazel index d8f077c32c..434069696d 100644 --- a/packages/concatjs/web_test/BUILD.bazel +++ b/packages/concatjs/web_test/BUILD.bazel @@ -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", diff --git a/packages/concatjs/web_test/karma.conf.js b/packages/concatjs/web_test/karma.conf.js index d06d731401..e3fa8b0a58 100644 --- a/packages/concatjs/web_test/karma.conf.js +++ b/packages/concatjs/web_test/karma.conf.js @@ -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. */ @@ -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. @@ -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); + } } /** diff --git a/packages/concatjs/web_test/test/karma/BUILD.bazel b/packages/concatjs/web_test/test/karma/BUILD.bazel index e56ddd472f..6e15572d25 100644 --- a/packages/concatjs/web_test/test/karma/BUILD.bazel +++ b/packages/concatjs/web_test/test/karma/BUILD.bazel @@ -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", ], @@ -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", diff --git a/packages/concatjs/web_test/test/karma/coverage.spec.js b/packages/concatjs/web_test/test/karma/coverage.spec.js new file mode 100644 index 0000000000..4486f0d00b --- /dev/null +++ b/packages/concatjs/web_test/test/karma/coverage.spec.js @@ -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); + }); +}); +}); diff --git a/packages/concatjs/web_test/test/karma/coverage_source.js b/packages/concatjs/web_test/test/karma/coverage_source.js new file mode 100644 index 0000000000..a1d218277a --- /dev/null +++ b/packages/concatjs/web_test/test/karma/coverage_source.js @@ -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; +}); \ No newline at end of file diff --git a/packages/concatjs/web_test/test/karma/coverage_source_uncovered.js b/packages/concatjs/web_test/test/karma/coverage_source_uncovered.js new file mode 100644 index 0000000000..8ad07de908 --- /dev/null +++ b/packages/concatjs/web_test/test/karma/coverage_source_uncovered.js @@ -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; +}); \ No newline at end of file diff --git a/packages/concatjs/web_test/test/karma_typescript/BUILD.bazel b/packages/concatjs/web_test/test/karma_typescript/BUILD.bazel index 60fbe139be..3c9b681c32 100644 --- a/packages/concatjs/web_test/test/karma_typescript/BUILD.bazel +++ b/packages/concatjs/web_test/test/karma_typescript/BUILD.bazel @@ -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 = [ diff --git a/packages/concatjs/web_test/test/karma_typescript/coverage.spec.ts b/packages/concatjs/web_test/test/karma_typescript/coverage.spec.ts new file mode 100644 index 0000000000..8a2ed427e4 --- /dev/null +++ b/packages/concatjs/web_test/test/karma_typescript/coverage.spec.ts @@ -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); + }); +}); diff --git a/packages/concatjs/web_test/test/karma_typescript/coverage_source.ts b/packages/concatjs/web_test/test/karma_typescript/coverage_source.ts new file mode 100644 index 0000000000..45f569b949 --- /dev/null +++ b/packages/concatjs/web_test/test/karma_typescript/coverage_source.ts @@ -0,0 +1,7 @@ +export function isString(input: string) { + if (typeof input === 'string') { + return true; + } else { + return false; + } +} diff --git a/packages/concatjs/web_test/test/karma_typescript/coverage_source_uncovered.ts b/packages/concatjs/web_test/test/karma_typescript/coverage_source_uncovered.ts new file mode 100644 index 0000000000..a40d272795 --- /dev/null +++ b/packages/concatjs/web_test/test/karma_typescript/coverage_source_uncovered.ts @@ -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; +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index f5454bf8c7..9e1712b5f7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6222,6 +6222,14 @@ karma-jasmine@4.0.1: 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" + karma-requirejs@1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/karma-requirejs/-/karma-requirejs-1.1.0.tgz#fddae2cb87d7ebc16fb0222893564d7fee578798" @@ -10453,6 +10461,11 @@ xml2js@^0.4.17: sax ">=0.6.0" xmlbuilder "~9.0.1" +xmlbuilder@12.0.0: + 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"