-
Notifications
You must be signed in to change notification settings - Fork 13
/
karma-config-loader.ts
124 lines (104 loc) · 5.53 KB
/
karma-config-loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Config as KarmaConfig, CustomLauncher, InlinePluginDef } from 'karma';
import { dirname, resolve } from 'path';
import {
CHROME_BROWSER_DEBUGGING_PORT_FLAG,
KARMA_BROWSER_CAPTURE_MIN_TIMEOUT,
KARMA_CUSTOM_LAUNCHER_BROWSER_NAME
} from '../../../constants';
import { Logger } from '../../../util/logging/logger';
import { asNonBlankStringOrUndefined } from '../../../util/utils';
import { KarmaEnvironmentVariable } from '../karma-environment-variable';
import { KarmaLogLevel } from '../karma-log-level';
import { KarmaTestExplorerReporter } from '../reporter/karma-test-explorer-reporter';
export class KarmaConfigLoader {
public constructor(private readonly logger: Logger) {}
public loadConfig(config: KarmaConfig, originalConfigPath: string) {
this.loadOriginalConfig(config, originalConfigPath);
this.applyConfigOverrides(config, originalConfigPath);
this.addKarmaTestExplorerReporter(config);
}
private loadOriginalConfig(config: KarmaConfig, originalKarmaConfigPath: string) {
let originalKarmaConfigModule = require(originalKarmaConfigPath); // eslint-disable-line @typescript-eslint/no-var-requires
// https://github.com/karma-runner/karma/blob/v1.7.0/lib/config.js#L364
if (typeof originalKarmaConfigModule === 'object' && typeof originalKarmaConfigModule.default !== 'undefined') {
originalKarmaConfigModule = originalKarmaConfigModule.default;
}
originalKarmaConfigModule(config);
}
private applyConfigOverrides(config: KarmaConfig, originalConfigPath: string) {
// -- Karma Port and LogLevel settings --
const karmaLogLevel = <KarmaLogLevel>process.env[KarmaEnvironmentVariable.KarmaLogLevel] ?? KarmaLogLevel.INFO;
const karmaPort = parseInt(process.env[KarmaEnvironmentVariable.KarmaPort]!, 10);
// -- Autowatch settings --
const autoWatchEnabled =
(process.env[KarmaEnvironmentVariable.AutoWatchEnabled] ?? 'false').toLowerCase() === 'true';
const configuredAutoWatchBatchDelay = parseInt(process.env[KarmaEnvironmentVariable.AutoWatchBatchDelay]!, 10);
const autoWatchBatchDelay = !autoWatchEnabled
? 0
: !Number.isNaN(configuredAutoWatchBatchDelay)
? configuredAutoWatchBatchDelay
: undefined;
// -- Browser and Custom Launcher settings --
const requestedBrowser = process.env[KarmaEnvironmentVariable.Browser];
let browser: string;
let customLauncher: CustomLauncher | undefined;
if (requestedBrowser) {
this.logger.debug(() => `Using requested karma browser: ${requestedBrowser}`);
browser = requestedBrowser;
customLauncher = undefined;
} else {
const debugPortString = process.env[KarmaEnvironmentVariable.DebugPort];
const debugPort: number | undefined = debugPortString ? parseInt(debugPortString, 10) : undefined;
const customLauncherString = process.env[KarmaEnvironmentVariable.CustomLauncher]!;
const customLaucherObject = customLauncherString ? JSON.parse(customLauncherString) : {};
this.addCustomLauncherDebugPort(customLaucherObject, debugPort);
this.logger.debug(() => `Using custom launcher: ${JSON.stringify(customLaucherObject, null, 2)}`);
browser = KARMA_CUSTOM_LAUNCHER_BROWSER_NAME;
customLauncher = customLaucherObject;
}
// -- Update Karma config --
config.port = karmaPort;
config.logLevel = (config as any)[`LOG_${karmaLogLevel.toUpperCase()}`];
config.singleRun = false;
config.autoWatch = autoWatchEnabled;
config.autoWatchBatchDelay = autoWatchBatchDelay ?? config.autoWatchBatchDelay;
config.restartOnFileChange = false;
config.browsers = [browser];
config.customLaunchers = customLauncher ? { [browser]: customLauncher } : config.customLaunchers;
config.browserNoActivityTimeout = 1000 * 60 * 60 * 24;
config.browserDisconnectTimeout = Math.max(config.browserDisconnectTimeout || 0, 30_000);
config.pingTimeout = 1000 * 60 * 60 * 24;
config.captureTimeout = Math.max(config.captureTimeout || 0, KARMA_BROWSER_CAPTURE_MIN_TIMEOUT);
config.browserSocketTimeout = 30_000;
config.processKillTimeout = 2000;
config.retryLimit = Math.max(config.retryLimit || 0, 3);
(config.exclude ??= []).push(originalConfigPath);
(config.client ??= {}).clearContext = false;
config.basePath =
asNonBlankStringOrUndefined(config.basePath) ??
(originalConfigPath ? resolve(dirname(originalConfigPath)) : process.cwd());
// -- Permanently disable Single Run --
const configSetter = typeof config.set === 'function' ? config.set : undefined;
config.set = configSetter
? (newConfig = {}) => configSetter.apply(config, [{ ...newConfig, singleRun: false }])
: config.set;
}
private addKarmaTestExplorerReporter(config: KarmaConfig) {
const reporterName = KarmaTestExplorerReporter.name;
const karmaPlugin: InlinePluginDef = { [`reporter:${reporterName}`]: ['type', KarmaTestExplorerReporter] };
const plugins = Array.isArray(config.plugins) ? config.plugins : ['karma-*'];
plugins.push(karmaPlugin);
config.plugins = plugins;
const reporters = Array.isArray(config.reporters) ? config.reporters : [];
reporters.splice(0, reporters.length, reporterName);
config.reporters = reporters;
}
private addCustomLauncherDebugPort(customLaucher: CustomLauncher, debugPort: number | undefined) {
if (!customLaucher || debugPort === undefined) {
return;
}
customLaucher.flags = customLaucher.flags?.map(flag =>
flag.startsWith(CHROME_BROWSER_DEBUGGING_PORT_FLAG) ? `${CHROME_BROWSER_DEBUGGING_PORT_FLAG}=${debugPort}` : flag
);
}
}