blob: 879a5c8084a0b3c7e84ba222b6c6d08056ca8dec [file] [log] [blame]
Dmitrii Filippovacd39a22020-04-02 10:31:43 +02001/**
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
18const runUnderBazel = !!process.env["RUNFILES_DIR"];
19const path = require('path');
20
21function 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 Filippov861628f2020-05-08 14:17:43 +020037function getUiDevNpmFilePath(importPath) {
Dmitrii Filippovacd39a22020-04-02 10:31:43 +020038 if(runUnderBazel) {
Dmitrii Filippov861628f2020-05-08 14:17:43 +020039 return `external/ui_dev_npm/node_modules/${importPath}`;
Dmitrii Filippovacd39a22020-04-02 10:31:43 +020040 }
41 else {
Dmitrii Filippov861628f2020-05-08 14:17:43 +020042 return `polygerrit-ui/node_modules/${importPath}`
Dmitrii Filippovacd39a22020-04-02 10:31:43 +020043 }
44}
45
46module.exports = function(config) {
Dmitrii Filippov887226c2020-06-19 15:28:17 +020047 const localDirName = path.resolve(__dirname, '../.ts-out/polygerrit-ui/app');
Dmitrii Filippovcce4b102020-06-17 14:57:11 +020048 const rootDir = runUnderBazel ?
Dmitrii Filippov887226c2020-06-19 15:28:17 +020049 'polygerrit-ui/app/_pg_with_tests_out/' : localDirName + '/';
Dmitrii Filippovacd39a22020-04-02 10:31:43 +020050 const testFilesLocationPattern =
Dmitrii Filippovcce4b102020-06-17 14:57:11 +020051 `${rootDir}**/!(template_test_srcs)/`;
Dmitrii Filippovacd39a22020-04-02 10:31:43 +020052 // 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 Filippov861628f2020-05-08 14:17:43 +020079 getUiDevNpmFilePath('accessibility-developer-tools/dist/js/axs_testing.js'),
80 getUiDevNpmFilePath('sinon/pkg/sinon.js'),
Dmitrii Filippovacd39a22020-04-02 10:31:43 +020081 { pattern: testFilesPattern, type: 'module' },
82 ],
83 esm: {
Dmitrii Filippov85b48912020-08-13 11:41:15 +020084 nodeResolve: {
85 // By default, it tries to use page.mjs file instead of page.js
Dmitrii Filippov5dd2a1b2020-08-18 15:21:16 +020086 // 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 Filippov85b48912020-08-13 11:41:15 +020091 },
Dmitrii Filippovacd39a22020-04-02 10:31:43 +020092 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 Filippov85b48912020-08-13 11:41:15 +0200101 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 Filippovacd39a22020-04-02 10:31:43 +0200123 },
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 Filippov861628f2020-05-08 14:17:43 +0200163 ui: 'tdd',
164 timeout: 5000,
Dmitrii Filippovacd39a22020-04-02 10:31:43 +0200165 }
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};