From ae904d59be6dedf80d295b4739ca3b4bdca19758 Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Fri, 1 Nov 2019 21:11:25 +0100 Subject: [PATCH] chore: fixing coverage for karma using istanbul (#466) --- karma.base.js | 7 +++- .../test.config.js => karma.webpack.js | 13 +++++-- packages/opentelemetry-core/karma.conf.js | 4 +-- packages/opentelemetry-core/package.json | 13 ++++--- .../opentelemetry-core/test/index-webpack.ts | 3 ++ .../karma.conf.js | 4 +-- .../package.json | 10 +++--- .../test/index-webpack.ts | 3 ++ .../webpack/test.config.js | 34 ------------------- packages/opentelemetry-tracing/karma.conf.js | 4 +-- packages/opentelemetry-tracing/package.json | 10 +++--- .../test/index-webpack.ts | 3 ++ .../webpack/test.config.js | 34 ------------------- packages/opentelemetry-web/karma.conf.js | 4 +-- packages/opentelemetry-web/package.json | 10 +++--- .../opentelemetry-web/test/index-webpack.ts | 3 ++ .../opentelemetry-web/webpack/test.config.js | 34 ------------------- 17 files changed, 63 insertions(+), 130 deletions(-) rename packages/opentelemetry-core/webpack/test.config.js => karma.webpack.js (73%) delete mode 100644 packages/opentelemetry-plugin-document-load/webpack/test.config.js delete mode 100644 packages/opentelemetry-tracing/webpack/test.config.js delete mode 100644 packages/opentelemetry-web/webpack/test.config.js diff --git a/karma.base.js b/karma.base.js index 9b8869fc07..088ac3f987 100644 --- a/karma.base.js +++ b/karma.base.js @@ -19,7 +19,12 @@ module.exports = { hostname: 'localhost', browsers: ['ChromeHeadless'], frameworks: ['mocha'], - reporters: ['spec'], + coverageIstanbulReporter: { + reports: ['json'], + dir: '.nyc_output', + fixWebpackSourcePaths: true + }, + reporters: ['spec', 'coverage-istanbul'], files: ['test/index-webpack.ts'], preprocessors: { 'test/index-webpack.ts': ['webpack'] }, webpackMiddleware: { noInfo: true } diff --git a/packages/opentelemetry-core/webpack/test.config.js b/karma.webpack.js similarity index 73% rename from packages/opentelemetry-core/webpack/test.config.js rename to karma.webpack.js index 997cce7182..d62d6292c5 100644 --- a/packages/opentelemetry-core/webpack/test.config.js +++ b/karma.webpack.js @@ -14,9 +14,9 @@ * limitations under the License. */ -const webpackNodePolyfills = require('../../../webpack.node-polyfills.js'); +const webpackNodePolyfills = require('./webpack.node-polyfills.js'); -// This is the webpack configuration for browser Karma tests. +// This is the webpack configuration for browser Karma tests with coverage. module.exports = { mode: 'development', target: 'web', @@ -26,6 +26,15 @@ module.exports = { module: { rules: [ { test: /\.ts$/, use: 'ts-loader' }, + { + enforce: 'post', + exclude: /(node_modules|\.test\.[tj]sx?$)/, + test: /\.ts$/, + use: { + loader: 'istanbul-instrumenter-loader', + options: { esModules: true } + } + }, // This setting configures Node polyfills for the browser that will be // added to the webpack bundle for Karma tests. { parser: { node: webpackNodePolyfills } } diff --git a/packages/opentelemetry-core/karma.conf.js b/packages/opentelemetry-core/karma.conf.js index 66529c7d92..7183aab033 100644 --- a/packages/opentelemetry-core/karma.conf.js +++ b/packages/opentelemetry-core/karma.conf.js @@ -14,11 +14,11 @@ * limitations under the License. */ -const webpackConfig = require('./webpack/test.config.js'); +const karmaWebpackConfig = require('../../karma.webpack'); const karmaBaseConfig = require('../../karma.base'); module.exports = (config) => { config.set(Object.assign({}, karmaBaseConfig, { - webpack: webpackConfig + webpack: karmaWebpackConfig })) }; diff --git a/packages/opentelemetry-core/package.json b/packages/opentelemetry-core/package.json index 2f7360b229..dd641438a5 100644 --- a/packages/opentelemetry-core/package.json +++ b/packages/opentelemetry-core/package.json @@ -11,11 +11,12 @@ "repository": "open-telemetry/opentelemetry-js", "scripts": { "test": "nyc ts-mocha -p tsconfig.json test/**/*.ts", - "test:browser": "karma start --single-run", + "test:browser": "nyc karma start --single-run", "tdd": "yarn tdd:node", "tdd:node": "yarn test -- --watch-extensions ts --watch", "tdd:browser": "karma start", "codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../", + "codecov:browser": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../", "clean": "rimraf build/*", "check": "gts check", "compile": "tsc -p .", @@ -53,20 +54,22 @@ "@types/webpack-env": "1.13.9", "codecov": "^3.1.0", "gts": "^1.0.0", - "karma": "^4.1.0", + "istanbul-instrumenter-loader": "^3.0.1", + "karma": "^4.4.1", "karma-chrome-launcher": "^3.1.0", + "karma-coverage-istanbul-reporter": "^2.1.0", "karma-mocha": "^1.3.0", "karma-spec-reporter": "^0.0.32", "karma-webpack": "^4.0.2", "mocha": "^6.1.0", "nyc": "^14.1.1", "rimraf": "^3.0.0", - "sinon": "^7.3.2", + "sinon": "^7.5.0", + "tslint-consistent-codestyle" : "^1.16.0", + "tslint-microsoft-contrib": "^6.2.0", "ts-loader": "^6.0.4", "ts-mocha": "^6.0.0", "ts-node": "^8.0.0", - "tslint-consistent-codestyle": "^1.15.1", - "tslint-microsoft-contrib": "^6.2.0", "typescript": "^3.6.3", "webpack": "^4.35.2" }, diff --git a/packages/opentelemetry-core/test/index-webpack.ts b/packages/opentelemetry-core/test/index-webpack.ts index 3899f0edc9..7731f09091 100644 --- a/packages/opentelemetry-core/test/index-webpack.ts +++ b/packages/opentelemetry-core/test/index-webpack.ts @@ -18,3 +18,6 @@ // all modules ending in "test" from the current folder and all its subfolders. const testsContext = require.context('.', true, /test$/); testsContext.keys().forEach(testsContext); + +const srcContext = require.context('.', true, /src$/); +srcContext.keys().forEach(srcContext); diff --git a/packages/opentelemetry-plugin-document-load/karma.conf.js b/packages/opentelemetry-plugin-document-load/karma.conf.js index 66529c7d92..7183aab033 100644 --- a/packages/opentelemetry-plugin-document-load/karma.conf.js +++ b/packages/opentelemetry-plugin-document-load/karma.conf.js @@ -14,11 +14,11 @@ * limitations under the License. */ -const webpackConfig = require('./webpack/test.config.js'); +const karmaWebpackConfig = require('../../karma.webpack'); const karmaBaseConfig = require('../../karma.base'); module.exports = (config) => { config.set(Object.assign({}, karmaBaseConfig, { - webpack: webpackConfig + webpack: karmaWebpackConfig })) }; diff --git a/packages/opentelemetry-plugin-document-load/package.json b/packages/opentelemetry-plugin-document-load/package.json index 9597ca16fc..2cbf69b164 100644 --- a/packages/opentelemetry-plugin-document-load/package.json +++ b/packages/opentelemetry-plugin-document-load/package.json @@ -40,16 +40,18 @@ "access": "public" }, "devDependencies": { + "@babel/core": "^7.6.0", "@types/mocha": "^5.2.5", "@types/node": "^12.6.8", - "@types/webpack-env": "1.13.9", "@types/sinon": "^7.0.13", - "@babel/core": "^7.6.0", + "@types/webpack-env": "1.13.9", "babel-loader": "^8.0.6", "codecov": "^3.1.0", "gts": "^1.0.0", - "karma": "^4.1.0", - "karma-chrome-launcher": "^2.2.0", + "istanbul-instrumenter-loader": "^3.0.1", + "karma": "^4.4.1", + "karma-chrome-launcher": "^3.1.0", + "karma-coverage-istanbul-reporter": "^2.1.0", "karma-mocha": "^1.3.0", "karma-spec-reporter": "^0.0.32", "karma-webpack": "^4.0.2", diff --git a/packages/opentelemetry-plugin-document-load/test/index-webpack.ts b/packages/opentelemetry-plugin-document-load/test/index-webpack.ts index 3899f0edc9..7731f09091 100644 --- a/packages/opentelemetry-plugin-document-load/test/index-webpack.ts +++ b/packages/opentelemetry-plugin-document-load/test/index-webpack.ts @@ -18,3 +18,6 @@ // all modules ending in "test" from the current folder and all its subfolders. const testsContext = require.context('.', true, /test$/); testsContext.keys().forEach(testsContext); + +const srcContext = require.context('.', true, /src$/); +srcContext.keys().forEach(srcContext); diff --git a/packages/opentelemetry-plugin-document-load/webpack/test.config.js b/packages/opentelemetry-plugin-document-load/webpack/test.config.js deleted file mode 100644 index 997cce7182..0000000000 --- a/packages/opentelemetry-plugin-document-load/webpack/test.config.js +++ /dev/null @@ -1,34 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const webpackNodePolyfills = require('../../../webpack.node-polyfills.js'); - -// This is the webpack configuration for browser Karma tests. -module.exports = { - mode: 'development', - target: 'web', - output: { filename: 'bundle.js' }, - resolve: { extensions: ['.ts', '.js'] }, - devtool: 'inline-source-map', - module: { - rules: [ - { test: /\.ts$/, use: 'ts-loader' }, - // This setting configures Node polyfills for the browser that will be - // added to the webpack bundle for Karma tests. - { parser: { node: webpackNodePolyfills } } - ] - } -}; diff --git a/packages/opentelemetry-tracing/karma.conf.js b/packages/opentelemetry-tracing/karma.conf.js index 66529c7d92..7183aab033 100644 --- a/packages/opentelemetry-tracing/karma.conf.js +++ b/packages/opentelemetry-tracing/karma.conf.js @@ -14,11 +14,11 @@ * limitations under the License. */ -const webpackConfig = require('./webpack/test.config.js'); +const karmaWebpackConfig = require('../../karma.webpack'); const karmaBaseConfig = require('../../karma.base'); module.exports = (config) => { config.set(Object.assign({}, karmaBaseConfig, { - webpack: webpackConfig + webpack: karmaWebpackConfig })) }; diff --git a/packages/opentelemetry-tracing/package.json b/packages/opentelemetry-tracing/package.json index 8ee6dcedfa..e1f217dbeb 100644 --- a/packages/opentelemetry-tracing/package.json +++ b/packages/opentelemetry-tracing/package.json @@ -18,7 +18,7 @@ "prepare": "npm run compile", "tdd": "yarn test -- --watch-extensions ts --watch", "test": "nyc ts-mocha -p tsconfig.json 'test/**/*.ts' --exclude 'test/index-webpack.ts'", - "test:browser": "karma start --single-run", + "test:browser": "nyc karma start --single-run", "watch": "tsc -w" }, "keywords": [ @@ -51,8 +51,10 @@ "@types/webpack-env": "1.13.9", "codecov": "^3.1.0", "gts": "^1.0.0", - "karma": "^4.1.0", - "karma-chrome-launcher": "^2.2.0", + "istanbul-instrumenter-loader": "^3.0.1", + "karma": "^4.4.1", + "karma-chrome-launcher": "^3.1.0", + "karma-coverage-istanbul-reporter": "^2.1.0", "karma-mocha": "^1.3.0", "karma-spec-reporter": "^0.0.32", "karma-webpack": "^4.0.2", @@ -60,7 +62,7 @@ "nyc": "^14.1.1", "rimraf": "^3.0.0", "sinon": "^7.5.0", - "tslint-consistent-codestyle": "^1.15.1", + "tslint-consistent-codestyle" : "^1.16.0", "tslint-microsoft-contrib": "^6.2.0", "ts-loader": "^6.0.4", "ts-mocha": "^6.0.0", diff --git a/packages/opentelemetry-tracing/test/index-webpack.ts b/packages/opentelemetry-tracing/test/index-webpack.ts index 3899f0edc9..7731f09091 100644 --- a/packages/opentelemetry-tracing/test/index-webpack.ts +++ b/packages/opentelemetry-tracing/test/index-webpack.ts @@ -18,3 +18,6 @@ // all modules ending in "test" from the current folder and all its subfolders. const testsContext = require.context('.', true, /test$/); testsContext.keys().forEach(testsContext); + +const srcContext = require.context('.', true, /src$/); +srcContext.keys().forEach(srcContext); diff --git a/packages/opentelemetry-tracing/webpack/test.config.js b/packages/opentelemetry-tracing/webpack/test.config.js deleted file mode 100644 index 997cce7182..0000000000 --- a/packages/opentelemetry-tracing/webpack/test.config.js +++ /dev/null @@ -1,34 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const webpackNodePolyfills = require('../../../webpack.node-polyfills.js'); - -// This is the webpack configuration for browser Karma tests. -module.exports = { - mode: 'development', - target: 'web', - output: { filename: 'bundle.js' }, - resolve: { extensions: ['.ts', '.js'] }, - devtool: 'inline-source-map', - module: { - rules: [ - { test: /\.ts$/, use: 'ts-loader' }, - // This setting configures Node polyfills for the browser that will be - // added to the webpack bundle for Karma tests. - { parser: { node: webpackNodePolyfills } } - ] - } -}; diff --git a/packages/opentelemetry-web/karma.conf.js b/packages/opentelemetry-web/karma.conf.js index 66529c7d92..7183aab033 100644 --- a/packages/opentelemetry-web/karma.conf.js +++ b/packages/opentelemetry-web/karma.conf.js @@ -14,11 +14,11 @@ * limitations under the License. */ -const webpackConfig = require('./webpack/test.config.js'); +const karmaWebpackConfig = require('../../karma.webpack'); const karmaBaseConfig = require('../../karma.base'); module.exports = (config) => { config.set(Object.assign({}, karmaBaseConfig, { - webpack: webpackConfig + webpack: karmaWebpackConfig })) }; diff --git a/packages/opentelemetry-web/package.json b/packages/opentelemetry-web/package.json index a93bed101b..f4f438c862 100644 --- a/packages/opentelemetry-web/package.json +++ b/packages/opentelemetry-web/package.json @@ -40,16 +40,18 @@ "access": "public" }, "devDependencies": { + "@babel/core": "^7.6.0", "@types/mocha": "^5.2.5", "@types/node": "^12.6.8", - "@types/webpack-env": "1.13.9", "@types/sinon": "^7.0.13", - "@babel/core": "^7.6.0", + "@types/webpack-env": "1.13.9", "babel-loader": "^8.0.6", "codecov": "^3.1.0", "gts": "^1.0.0", - "karma": "^4.1.0", - "karma-chrome-launcher": "^2.2.0", + "istanbul-instrumenter-loader": "^3.0.1", + "karma": "^4.4.1", + "karma-chrome-launcher": "^3.1.0", + "karma-coverage-istanbul-reporter": "^2.1.0", "karma-mocha": "^1.3.0", "karma-spec-reporter": "^0.0.32", "karma-webpack": "^4.0.2", diff --git a/packages/opentelemetry-web/test/index-webpack.ts b/packages/opentelemetry-web/test/index-webpack.ts index 3899f0edc9..7731f09091 100644 --- a/packages/opentelemetry-web/test/index-webpack.ts +++ b/packages/opentelemetry-web/test/index-webpack.ts @@ -18,3 +18,6 @@ // all modules ending in "test" from the current folder and all its subfolders. const testsContext = require.context('.', true, /test$/); testsContext.keys().forEach(testsContext); + +const srcContext = require.context('.', true, /src$/); +srcContext.keys().forEach(srcContext); diff --git a/packages/opentelemetry-web/webpack/test.config.js b/packages/opentelemetry-web/webpack/test.config.js deleted file mode 100644 index 997cce7182..0000000000 --- a/packages/opentelemetry-web/webpack/test.config.js +++ /dev/null @@ -1,34 +0,0 @@ -/*! - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const webpackNodePolyfills = require('../../../webpack.node-polyfills.js'); - -// This is the webpack configuration for browser Karma tests. -module.exports = { - mode: 'development', - target: 'web', - output: { filename: 'bundle.js' }, - resolve: { extensions: ['.ts', '.js'] }, - devtool: 'inline-source-map', - module: { - rules: [ - { test: /\.ts$/, use: 'ts-loader' }, - // This setting configures Node polyfills for the browser that will be - // added to the webpack bundle for Karma tests. - { parser: { node: webpackNodePolyfills } } - ] - } -};