Dmitrii Filippov | acd39a2 | 2020-04-02 10:31:43 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2020 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | const runUnderBazel = !!process.env["RUNFILES_DIR"]; |
| 19 | const path = require('path'); |
| 20 | |
| 21 | function getModulesDir() { |
| 22 | if(runUnderBazel) { |
| 23 | // Run under bazel |
| 24 | return [ |
| 25 | `external/ui_npm/node_modules`, |
| 26 | `external/ui_dev_npm/node_modules` |
| 27 | ]; |
| 28 | } |
| 29 | |
| 30 | // Run from intellij or npm run test:kdebug |
| 31 | return [ |
| 32 | path.join(__dirname, 'app/node_modules'), |
| 33 | path.join(__dirname, 'node_modules'), |
| 34 | ]; |
| 35 | } |
| 36 | |
Dmitrii Filippov | 861628f | 2020-05-08 14:17:43 +0200 | [diff] [blame] | 37 | function getUiDevNpmFilePath(importPath) { |
Dmitrii Filippov | acd39a2 | 2020-04-02 10:31:43 +0200 | [diff] [blame] | 38 | if(runUnderBazel) { |
Dmitrii Filippov | 861628f | 2020-05-08 14:17:43 +0200 | [diff] [blame] | 39 | return `external/ui_dev_npm/node_modules/${importPath}`; |
Dmitrii Filippov | acd39a2 | 2020-04-02 10:31:43 +0200 | [diff] [blame] | 40 | } |
| 41 | else { |
Dmitrii Filippov | 861628f | 2020-05-08 14:17:43 +0200 | [diff] [blame] | 42 | return `polygerrit-ui/node_modules/${importPath}` |
Dmitrii Filippov | acd39a2 | 2020-04-02 10:31:43 +0200 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | module.exports = function(config) { |
Dmitrii Filippov | 887226c | 2020-06-19 15:28:17 +0200 | [diff] [blame] | 47 | const localDirName = path.resolve(__dirname, '../.ts-out/polygerrit-ui/app'); |
Dmitrii Filippov | cce4b10 | 2020-06-17 14:57:11 +0200 | [diff] [blame] | 48 | const rootDir = runUnderBazel ? |
Dmitrii Filippov | 887226c | 2020-06-19 15:28:17 +0200 | [diff] [blame] | 49 | 'polygerrit-ui/app/_pg_with_tests_out/' : localDirName + '/'; |
Dmitrii Filippov | acd39a2 | 2020-04-02 10:31:43 +0200 | [diff] [blame] | 50 | const testFilesLocationPattern = |
Dmitrii Filippov | cce4b10 | 2020-06-17 14:57:11 +0200 | [diff] [blame] | 51 | `${rootDir}**/!(template_test_srcs)/`; |
Dmitrii Filippov | acd39a2 | 2020-04-02 10:31:43 +0200 | [diff] [blame] | 52 | // Use --test-files to specify pattern for a test files. |
| 53 | // It can be just a file name, without a path: |
| 54 | // --test-files async-foreach-behavior_test.js |
| 55 | // If you specify --test-files without pattern, it gets true value |
| 56 | // In this case we ill run all tests (usefull for package.json "debugtest" |
| 57 | // script) |
| 58 | const testFilesPattern = (typeof config.testFiles == 'string') ? |
| 59 | testFilesLocationPattern + config.testFiles : |
| 60 | testFilesLocationPattern + '*_test.js'; |
| 61 | config.set({ |
| 62 | // base path that will be used to resolve all patterns (eg. files, exclude) |
| 63 | basePath: '../', |
| 64 | plugins: [ |
| 65 | // Do not use karma-* to load all installed plugin |
| 66 | // This can lead to unexpected behavior under bazel |
| 67 | // if you forget to add a plugin in a bazel rule. |
| 68 | require.resolve('@open-wc/karma-esm'), |
| 69 | 'karma-mocha', |
| 70 | 'karma-chrome-launcher', |
| 71 | 'karma-mocha-reporter', |
| 72 | ], |
| 73 | // frameworks to use |
| 74 | // available frameworks: https://npmjs.org/browse/keyword/karma-adapter |
| 75 | frameworks: ['mocha', 'esm'], |
| 76 | |
| 77 | // list of files / patterns to load in the browser |
| 78 | files: [ |
Dmitrii Filippov | 861628f | 2020-05-08 14:17:43 +0200 | [diff] [blame] | 79 | getUiDevNpmFilePath('accessibility-developer-tools/dist/js/axs_testing.js'), |
| 80 | getUiDevNpmFilePath('sinon/pkg/sinon.js'), |
Dmitrii Filippov | acd39a2 | 2020-04-02 10:31:43 +0200 | [diff] [blame] | 81 | { pattern: testFilesPattern, type: 'module' }, |
| 82 | ], |
| 83 | esm: { |
Dmitrii Filippov | 85b4891 | 2020-08-13 11:41:15 +0200 | [diff] [blame] | 84 | nodeResolve: { |
| 85 | // By default, it tries to use page.mjs file instead of page.js |
Dmitrii Filippov | 5dd2a1b | 2020-08-18 15:21:16 +0200 | [diff] [blame] | 86 | // when importing 'page/page', so we shouldn't use .mjs extension |
| 87 | // in node resolve. |
| 88 | // The .ts extension is required to display source code in browser |
| 89 | // (otherwise esm plugin crashes) |
| 90 | extensions: ['.js', '.ts'], |
Dmitrii Filippov | 85b4891 | 2020-08-13 11:41:15 +0200 | [diff] [blame] | 91 | }, |
Dmitrii Filippov | acd39a2 | 2020-04-02 10:31:43 +0200 | [diff] [blame] | 92 | moduleDirs: getModulesDir(), |
| 93 | // Bazel and yarn uses symlinks for files. |
| 94 | // preserveSymlinks is necessary for correct modules paths resolving |
| 95 | preserveSymlinks: true, |
| 96 | // By default, esm-dev-server uses 'auto' compatibility mode. |
| 97 | // In the 'auto' mode it incorrectly applies polyfills and |
| 98 | // breaks tests in some browser versions |
| 99 | // (for example, Chrome 69 on gerrit-ci). |
| 100 | compatibility: 'none', |
Dmitrii Filippov | 85b4891 | 2020-08-13 11:41:15 +0200 | [diff] [blame] | 101 | plugins: [ |
| 102 | { |
| 103 | transform(context) { |
| 104 | if (context.path.endsWith('/node_modules/page/page.js')) { |
| 105 | const orignalBody = context.body; |
| 106 | // Can't import page.js directly, because this is undefined. |
| 107 | // Replace it with window |
| 108 | // The same replace exists in server.go |
| 109 | // Rollup makes this replacement automatically |
| 110 | const transformedBody = orignalBody.replace( |
| 111 | '}(this, (function () { \'use strict\';', |
| 112 | '}(window, (function () { \'use strict\';' |
| 113 | ); |
| 114 | if(orignalBody.length === transformedBody.length) { |
| 115 | console.error('The page.js was updated. Please update transform accordingly'); |
| 116 | process.exit(1); |
| 117 | } |
| 118 | return {body: transformedBody}; |
| 119 | } |
| 120 | }, |
| 121 | } |
| 122 | ] |
Dmitrii Filippov | acd39a2 | 2020-04-02 10:31:43 +0200 | [diff] [blame] | 123 | }, |
| 124 | // test results reporter to use |
| 125 | // possible values: 'dots', 'progress' |
| 126 | // available reporters: https://npmjs.org/browse/keyword/karma-reporter |
| 127 | reporters: ['mocha'], |
| 128 | |
| 129 | |
| 130 | // web server port |
| 131 | port: 9876, |
| 132 | |
| 133 | |
| 134 | // enable / disable colors in the output (reporters and logs) |
| 135 | colors: true, |
| 136 | |
| 137 | |
| 138 | // level of logging |
| 139 | // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG |
| 140 | logLevel: config.LOG_INFO, |
| 141 | |
| 142 | |
| 143 | // enable / disable watching file and executing tests whenever any file changes |
| 144 | autoWatch: false, |
| 145 | |
| 146 | |
| 147 | // start these browsers |
| 148 | // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher |
| 149 | browsers: ["CustomChromeHeadless"], |
| 150 | browserForDebugging: "CustomChromeHeadlessWithDebugPort", |
| 151 | |
| 152 | |
| 153 | // Continuous Integration mode |
| 154 | // if true, Karma captures browsers, runs the tests and exits |
| 155 | singleRun: true, |
| 156 | |
| 157 | // Concurrency level |
| 158 | // how many browser should be started simultaneous |
| 159 | concurrency: Infinity, |
| 160 | |
| 161 | client: { |
| 162 | mocha: { |
Dmitrii Filippov | 861628f | 2020-05-08 14:17:43 +0200 | [diff] [blame] | 163 | ui: 'tdd', |
| 164 | timeout: 5000, |
Dmitrii Filippov | acd39a2 | 2020-04-02 10:31:43 +0200 | [diff] [blame] | 165 | } |
| 166 | }, |
| 167 | |
| 168 | customLaunchers: { |
| 169 | // Based on https://developers.google.com/web/updates/2017/06/headless-karma-mocha-chai |
| 170 | "CustomChromeHeadless": { |
| 171 | base: 'ChromeHeadless', |
| 172 | flags: ['--disable-translate', '--disable-extensions'], |
| 173 | }, |
| 174 | "ChromeDev": { |
| 175 | base: 'Chrome', |
| 176 | flags: ['--disable-extensions', ' --auto-open-devtools-for-tabs'], |
| 177 | }, |
| 178 | "CustomChromeHeadlessWithDebugPort": { |
| 179 | base: 'CustomChromeHeadless', |
| 180 | flags: ['--remote-debugging-port=9222'], |
| 181 | } |
| 182 | } |
| 183 | }); |
| 184 | }; |