commit e364b4065d4fb573943aac6f3cad06c565bef176 Author: Dmitrii Filippov Date: Thu Oct 15 14:31:46 2020 +0200 Improve tests infrastructures 1. Add source map support - if a test fails, a stack trace show original location instead of location in compiled .js file. 2. Fix issue introduced by the new version of the Karma IntelliJ plugin. The new version pass test names wrapped in /.../, but karma-mocha doesn't support it. As a result, it was impossible to run single test/suite in IntelliJ without hacking. Now it works out of box. 3. Fix "Browser disconnected" issues in IntellJ. Change-Id: I4c758d6e7e2739ab291857d758aaf642fcd11d54 diff --git a/polygerrit-ui/app/test/common-test-setup.js b/polygerrit-ui/app/test/common-test-setup.js index 500187a..eead4f8 100644 --- a/polygerrit-ui/app/test/common-test-setup.js +++ b/polygerrit-ui/app/test/common-test-setup.js @@ -14,7 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +// This should be the first import to install handler before any other code +import './source-map-support-install.js'; // TODO(dmfilippov): remove bundled-polymer.js imports when the following issue // https://github.com/Polymer/polymer-resin/issues/9 is resolved. import '../scripts/bundled-polymer.js'; diff --git a/polygerrit-ui/app/test/source-map-support-install.js b/polygerrit-ui/app/test/source-map-support-install.js new file mode 100644 index 0000000..a8f1473 --- /dev/null +++ b/polygerrit-ui/app/test/source-map-support-install.js @@ -0,0 +1,20 @@ +/** + * @license + * Copyright (C) 2020 The Android Open Source Project + * + * 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. + */ + +// The karma.conf.js file loads required module before any other modules +// The source-map-support.js can't be imported with import ... statement +window.sourceMapSupport.install(); diff --git a/polygerrit-ui/grep-patch-karma.js b/polygerrit-ui/grep-patch-karma.js new file mode 100644 index 0000000..adf5171 --- /dev/null +++ b/polygerrit-ui/grep-patch-karma.js @@ -0,0 +1,47 @@ +/** + * @license + * Copyright (C) 2020 The Android Open Source Project + * + * 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. + */ + +// The IntelliJ (and probably other IDEs) passes test names as a regexp in +// the format: +// --grep=/some regexp.../ +// But mochajs doesn't expect the '/' characters before and after the regexp. +// The code below patches input args and removes '/' if they exists. +function installPatch(karma) { + const originalKarmaStart = karma.start; + + karma.start = function(config, ...args) { + const regexpGrepPrefix = '--grep=/'; + const regexpGrepSuffix = '/'; + if (config && config.args) { + for (let i = 0; i < config.args.length; i++) { + const arg = config.args[i]; + if (arg.startsWith(regexpGrepPrefix) && arg.endsWith(regexpGrepSuffix)) { + const regexpText = arg.slice(regexpGrepPrefix.length, -regexpGrepPrefix.length); + config.args[i] = '--grep=' + regexpText; + } + } + } + originalKarmaStart.apply(this, [config, ...args]); + } + +} + +const karma = window.__karma__; +if (karma && karma.start && !karma.__grep_patch_installed__) { + karma.__grep_patch_installed__ = true; + installPatch(karma); +} diff --git a/polygerrit-ui/karma.conf.js b/polygerrit-ui/karma.conf.js index 879a5c8..fb87675 100644 --- a/polygerrit-ui/karma.conf.js +++ b/polygerrit-ui/karma.conf.js @@ -43,6 +43,20 @@ function getUiDevNpmFilePath(importPath) { } } +function runInIde() { + // A simple detection of IDE. + // Default browserNoActivityTimeout is 30 seconds. An IDE usually + // runs karma in background and send commands when a user wants to + // execute test. If interval between user executed tests is bigger than + // browserNoActivityTimeout, the IDE reports error and doesn't restart + // server. + // We want to increase browserNoActivityTimeout when tests run in IDE. + // Wd don't want to increase it in other cases, oterhise hanging tests + // can slow down CI. + return !runUnderBazel && + process.argv.some(arg => arg.toLowerCase().contains('intellij')); +} + module.exports = function(config) { const localDirName = path.resolve(__dirname, '../.ts-out/polygerrit-ui/app'); const rootDir = runUnderBazel ? @@ -58,7 +72,10 @@ module.exports = function(config) { const testFilesPattern = (typeof config.testFiles == 'string') ? testFilesLocationPattern + config.testFiles : testFilesLocationPattern + '*_test.js'; + // Special patch for grep parameters (see details in the grep-patch-karam.js) + const additionalFiles = runUnderBazel ? [] : ['polygerrit-ui/grep-patch-karma.js']; config.set({ + browserNoActivityTimeout: runInIde ? 60 * 60 * 1000 : 30 * 1000, // base path that will be used to resolve all patterns (eg. files, exclude) basePath: '../', plugins: [ @@ -76,6 +93,8 @@ module.exports = function(config) { // list of files / patterns to load in the browser files: [ + ...additionalFiles, + getUiDevNpmFilePath('source-map-support/browser-source-map-support.js'), getUiDevNpmFilePath('accessibility-developer-tools/dist/js/axs_testing.js'), getUiDevNpmFilePath('sinon/pkg/sinon.js'), { pattern: testFilesPattern, type: 'module' }, diff --git a/polygerrit-ui/package.json b/polygerrit-ui/package.json index 7de55aa..91b8579 100644 --- a/polygerrit-ui/package.json +++ b/polygerrit-ui/package.json @@ -15,7 +15,8 @@ "karma-mocha-reporter": "^2.2.5", "lodash": "^4.17.15", "mocha": "7.2.0", - "sinon": "^9.0.2" + "sinon": "^9.0.2", + "source-map-support": "^0.5.19" }, "license": "Apache-2.0", "private": true diff --git a/polygerrit-ui/yarn.lock b/polygerrit-ui/yarn.lock index dfc5a43..a70ded8 100644 --- a/polygerrit-ui/yarn.lock +++ b/polygerrit-ui/yarn.lock @@ -3741,7 +3741,7 @@ socket.io@2.1.1: socket.io-client "2.1.1" socket.io-parser "~3.2.0" -source-map-support@~0.5.12: +source-map-support@^0.5.19, source-map-support@~0.5.12: version "0.5.19" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61" integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==