2021-06-07 02:09:53 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright Microsoft Corporation. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-07-20 22:13:40 +02:00
|
|
|
import * as fs from 'fs';
|
2022-04-29 01:49:36 +02:00
|
|
|
import * as os from 'os';
|
2022-10-11 01:42:48 +02:00
|
|
|
import * as path from 'path';
|
2023-01-18 02:16:36 +01:00
|
|
|
import { isRegExp } from 'playwright-core/lib/utils';
|
2023-01-27 02:26:47 +01:00
|
|
|
import type { ConfigCLIOverrides, SerializedConfig } from './ipc';
|
2023-01-18 02:16:36 +01:00
|
|
|
import { requireOrImport } from './transform';
|
|
|
|
|
import type { Config, FullConfigInternal, FullProjectInternal, Project, ReporterDescription } from './types';
|
2023-01-27 02:26:47 +01:00
|
|
|
import { errorWithFile, getPackageJsonPath, mergeObjects } from '../util';
|
2023-02-17 01:48:28 +01:00
|
|
|
import { setCurrentConfig } from './globals';
|
2022-04-29 01:49:36 +02:00
|
|
|
|
|
|
|
|
export const defaultTimeout = 30000;
|
2021-06-07 02:09:53 +02:00
|
|
|
|
2023-03-02 00:47:05 +01:00
|
|
|
const kDefineConfigWasUsed = Symbol('defineConfigWasUsed');
|
|
|
|
|
export const defineConfig = (config: any) => {
|
|
|
|
|
config[kDefineConfigWasUsed] = true;
|
|
|
|
|
return config;
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-18 02:16:36 +01:00
|
|
|
export class ConfigLoader {
|
2022-03-29 00:53:42 +02:00
|
|
|
private _fullConfig: FullConfigInternal;
|
2021-06-07 02:09:53 +02:00
|
|
|
|
2022-04-29 22:32:39 +02:00
|
|
|
constructor(configCLIOverrides?: ConfigCLIOverrides) {
|
2021-10-19 17:38:04 +02:00
|
|
|
this._fullConfig = { ...baseFullConfig };
|
2023-02-02 00:25:26 +01:00
|
|
|
this._fullConfig._internal.configCLIOverrides = configCLIOverrides || {};
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-20 00:56:57 +01:00
|
|
|
static async deserialize(data: SerializedConfig): Promise<ConfigLoader> {
|
2023-01-18 02:16:36 +01:00
|
|
|
const loader = new ConfigLoader(data.configCLIOverrides);
|
2022-05-03 23:25:56 +02:00
|
|
|
if (data.configFile)
|
|
|
|
|
await loader.loadConfigFile(data.configFile);
|
|
|
|
|
else
|
|
|
|
|
await loader.loadEmptyConfig(data.configDir);
|
|
|
|
|
return loader;
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-29 22:32:39 +02:00
|
|
|
async loadConfigFile(file: string): Promise<FullConfigInternal> {
|
2023-01-27 21:44:15 +01:00
|
|
|
if (this._fullConfig.configFile)
|
2021-06-07 02:09:53 +02:00
|
|
|
throw new Error('Cannot load two config files');
|
2023-01-27 21:44:15 +01:00
|
|
|
const config = await requireOrImportDefaultObject(file) as Config;
|
|
|
|
|
await this._processConfigObject(config, path.dirname(file), file);
|
2023-02-17 01:48:28 +01:00
|
|
|
setCurrentConfig(this._fullConfig);
|
2022-04-29 22:32:39 +02:00
|
|
|
return this._fullConfig;
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-29 01:22:20 +02:00
|
|
|
async loadEmptyConfig(configDir: string): Promise<Config> {
|
|
|
|
|
await this._processConfigObject({}, configDir);
|
2023-02-17 01:48:28 +01:00
|
|
|
setCurrentConfig(this._fullConfig);
|
2022-01-05 22:44:29 +01:00
|
|
|
return {};
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-27 21:44:15 +01:00
|
|
|
private async _processConfigObject(config: Config, configDir: string, configFile?: string) {
|
2022-04-29 22:32:39 +02:00
|
|
|
// 1. Validate data provided in the config file.
|
2023-01-27 21:44:15 +01:00
|
|
|
validateConfig(configFile || '<default config>', config);
|
2022-04-29 22:32:39 +02:00
|
|
|
|
|
|
|
|
// 2. Override settings from CLI.
|
2023-02-02 00:25:26 +01:00
|
|
|
const configCLIOverrides = this._fullConfig._internal.configCLIOverrides;
|
2023-01-27 21:44:15 +01:00
|
|
|
config.forbidOnly = takeFirst(configCLIOverrides.forbidOnly, config.forbidOnly);
|
|
|
|
|
config.fullyParallel = takeFirst(configCLIOverrides.fullyParallel, config.fullyParallel);
|
|
|
|
|
config.globalTimeout = takeFirst(configCLIOverrides.globalTimeout, config.globalTimeout);
|
|
|
|
|
config.maxFailures = takeFirst(configCLIOverrides.maxFailures, config.maxFailures);
|
|
|
|
|
config.outputDir = takeFirst(configCLIOverrides.outputDir, config.outputDir);
|
|
|
|
|
config.quiet = takeFirst(configCLIOverrides.quiet, config.quiet);
|
|
|
|
|
config.repeatEach = takeFirst(configCLIOverrides.repeatEach, config.repeatEach);
|
|
|
|
|
config.retries = takeFirst(configCLIOverrides.retries, config.retries);
|
|
|
|
|
if (configCLIOverrides.reporter)
|
|
|
|
|
config.reporter = toReporters(configCLIOverrides.reporter as any);
|
|
|
|
|
config.shard = takeFirst(configCLIOverrides.shard, config.shard);
|
|
|
|
|
config.timeout = takeFirst(configCLIOverrides.timeout, config.timeout);
|
|
|
|
|
config.updateSnapshots = takeFirst(configCLIOverrides.updateSnapshots, config.updateSnapshots);
|
|
|
|
|
config.ignoreSnapshots = takeFirst(configCLIOverrides.ignoreSnapshots, config.ignoreSnapshots);
|
|
|
|
|
if (configCLIOverrides.projects && config.projects)
|
2022-04-29 22:32:39 +02:00
|
|
|
throw new Error(`Cannot use --browser option when configuration file defines projects. Specify browserName in the projects instead.`);
|
2023-01-27 21:44:15 +01:00
|
|
|
config.projects = takeFirst(configCLIOverrides.projects, config.projects as any);
|
|
|
|
|
config.workers = takeFirst(configCLIOverrides.workers, config.workers);
|
|
|
|
|
config.use = mergeObjects(config.use, configCLIOverrides.use);
|
2022-04-30 01:05:08 +02:00
|
|
|
for (const project of config.projects || [])
|
|
|
|
|
this._applyCLIOverridesToProject(project);
|
2022-04-29 22:32:39 +02:00
|
|
|
|
2022-05-03 23:25:56 +02:00
|
|
|
// 3. Resolve config.
|
2022-03-24 00:05:49 +01:00
|
|
|
const packageJsonPath = getPackageJsonPath(configDir);
|
|
|
|
|
const packageJsonDir = packageJsonPath ? path.dirname(packageJsonPath) : undefined;
|
|
|
|
|
const throwawayArtifactsPath = packageJsonDir || process.cwd();
|
2021-06-07 02:09:53 +02:00
|
|
|
|
2021-06-11 07:31:27 +02:00
|
|
|
// Resolve script hooks relative to the root dir.
|
2022-03-24 00:05:49 +01:00
|
|
|
if (config.globalSetup)
|
|
|
|
|
config.globalSetup = resolveScript(config.globalSetup, configDir);
|
|
|
|
|
if (config.globalTeardown)
|
|
|
|
|
config.globalTeardown = resolveScript(config.globalTeardown, configDir);
|
|
|
|
|
// Resolve all config dirs relative to configDir.
|
|
|
|
|
if (config.testDir !== undefined)
|
|
|
|
|
config.testDir = path.resolve(configDir, config.testDir);
|
|
|
|
|
if (config.outputDir !== undefined)
|
|
|
|
|
config.outputDir = path.resolve(configDir, config.outputDir);
|
|
|
|
|
if (config.snapshotDir !== undefined)
|
|
|
|
|
config.snapshotDir = path.resolve(configDir, config.snapshotDir);
|
|
|
|
|
|
2023-02-02 00:25:26 +01:00
|
|
|
this._fullConfig._internal.configDir = configDir;
|
2023-03-17 19:50:44 +01:00
|
|
|
this._fullConfig._internal.storeDir = path.resolve(configDir, (config as any)._storeDir || 'playwright');
|
2023-01-27 21:44:15 +01:00
|
|
|
this._fullConfig.configFile = configFile;
|
|
|
|
|
this._fullConfig.rootDir = config.testDir || configDir;
|
2023-02-02 00:25:26 +01:00
|
|
|
this._fullConfig._internal.globalOutputDir = takeFirst(config.outputDir, throwawayArtifactsPath, baseFullConfig._internal.globalOutputDir);
|
2022-04-29 22:32:39 +02:00
|
|
|
this._fullConfig.forbidOnly = takeFirst(config.forbidOnly, baseFullConfig.forbidOnly);
|
|
|
|
|
this._fullConfig.fullyParallel = takeFirst(config.fullyParallel, baseFullConfig.fullyParallel);
|
|
|
|
|
this._fullConfig.globalSetup = takeFirst(config.globalSetup, baseFullConfig.globalSetup);
|
|
|
|
|
this._fullConfig.globalTeardown = takeFirst(config.globalTeardown, baseFullConfig.globalTeardown);
|
|
|
|
|
this._fullConfig.globalTimeout = takeFirst(config.globalTimeout, baseFullConfig.globalTimeout);
|
|
|
|
|
this._fullConfig.grep = takeFirst(config.grep, baseFullConfig.grep);
|
|
|
|
|
this._fullConfig.grepInvert = takeFirst(config.grepInvert, baseFullConfig.grepInvert);
|
|
|
|
|
this._fullConfig.maxFailures = takeFirst(config.maxFailures, baseFullConfig.maxFailures);
|
|
|
|
|
this._fullConfig.preserveOutput = takeFirst(config.preserveOutput, baseFullConfig.preserveOutput);
|
|
|
|
|
this._fullConfig.reporter = takeFirst(resolveReporters(config.reporter, configDir), baseFullConfig.reporter);
|
|
|
|
|
this._fullConfig.reportSlowTests = takeFirst(config.reportSlowTests, baseFullConfig.reportSlowTests);
|
|
|
|
|
this._fullConfig.quiet = takeFirst(config.quiet, baseFullConfig.quiet);
|
|
|
|
|
this._fullConfig.shard = takeFirst(config.shard, baseFullConfig.shard);
|
2023-02-02 00:25:26 +01:00
|
|
|
this._fullConfig._internal.ignoreSnapshots = takeFirst(config.ignoreSnapshots, baseFullConfig._internal.ignoreSnapshots);
|
2022-04-29 22:32:39 +02:00
|
|
|
this._fullConfig.updateSnapshots = takeFirst(config.updateSnapshots, baseFullConfig.updateSnapshots);
|
2023-02-03 17:44:01 +01:00
|
|
|
this._fullConfig._internal.plugins = ((config as any)._plugins || []).map((p: any) => ({ factory: p }));
|
2023-03-02 00:47:05 +01:00
|
|
|
this._fullConfig._internal.defineConfigWasUsed = !!(config as any)[kDefineConfigWasUsed];
|
2022-09-21 20:17:36 +02:00
|
|
|
|
|
|
|
|
const workers = takeFirst(config.workers, '50%');
|
|
|
|
|
if (typeof workers === 'string') {
|
|
|
|
|
if (workers.endsWith('%')) {
|
|
|
|
|
const cpus = os.cpus().length;
|
|
|
|
|
this._fullConfig.workers = Math.max(1, Math.floor(cpus * (parseInt(workers, 10) / 100)));
|
|
|
|
|
} else {
|
|
|
|
|
this._fullConfig.workers = parseInt(workers, 10);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this._fullConfig.workers = workers;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 00:27:21 +02:00
|
|
|
const webServers = takeFirst(config.webServer, baseFullConfig.webServer);
|
|
|
|
|
if (Array.isArray(webServers)) { // multiple web server mode
|
|
|
|
|
// Due to previous choices, this value shows up to the user in globalSetup as part of FullConfig. Arrays are not supported by the old type.
|
|
|
|
|
this._fullConfig.webServer = null;
|
2023-02-02 00:25:26 +01:00
|
|
|
this._fullConfig._internal.webServers = webServers;
|
2022-07-08 00:27:21 +02:00
|
|
|
} else if (webServers) { // legacy singleton mode
|
|
|
|
|
this._fullConfig.webServer = webServers;
|
2023-02-02 00:25:26 +01:00
|
|
|
this._fullConfig._internal.webServers = [webServers];
|
2022-07-08 00:27:21 +02:00
|
|
|
}
|
2022-05-03 01:28:14 +02:00
|
|
|
this._fullConfig.metadata = takeFirst(config.metadata, baseFullConfig.metadata);
|
2022-05-05 19:14:00 +02:00
|
|
|
this._fullConfig.projects = (config.projects || [config]).map(p => this._resolveProject(config, this._fullConfig, p, throwawayArtifactsPath));
|
2023-02-01 00:59:13 +01:00
|
|
|
|
|
|
|
|
resolveProjectDependencies(this._fullConfig.projects);
|
2023-01-18 21:56:03 +01:00
|
|
|
this._assignUniqueProjectIds(this._fullConfig.projects);
|
2022-07-28 05:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-02 01:32:13 +01:00
|
|
|
ignoreProjectDependencies() {
|
|
|
|
|
for (const project of this._fullConfig.projects)
|
|
|
|
|
project._internal.deps = [];
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 05:17:19 +02:00
|
|
|
private _assignUniqueProjectIds(projects: FullProjectInternal[]) {
|
|
|
|
|
const usedNames = new Set();
|
|
|
|
|
for (const p of projects) {
|
|
|
|
|
const name = p.name || '';
|
|
|
|
|
for (let i = 0; i < projects.length; ++i) {
|
|
|
|
|
const candidate = name + (i ? i : '');
|
|
|
|
|
if (usedNames.has(candidate))
|
|
|
|
|
continue;
|
2023-02-02 00:25:26 +01:00
|
|
|
p._internal.id = candidate;
|
2022-07-28 05:17:19 +02:00
|
|
|
usedNames.add(candidate);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-29 00:53:42 +02:00
|
|
|
fullConfig(): FullConfigInternal {
|
2021-06-07 02:09:53 +02:00
|
|
|
return this._fullConfig;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-30 01:05:08 +02:00
|
|
|
private _applyCLIOverridesToProject(projectConfig: Project) {
|
2023-02-02 00:25:26 +01:00
|
|
|
const configCLIOverrides = this._fullConfig._internal.configCLIOverrides;
|
2023-01-27 21:44:15 +01:00
|
|
|
projectConfig.fullyParallel = takeFirst(configCLIOverrides.fullyParallel, projectConfig.fullyParallel);
|
|
|
|
|
projectConfig.outputDir = takeFirst(configCLIOverrides.outputDir, projectConfig.outputDir);
|
|
|
|
|
projectConfig.repeatEach = takeFirst(configCLIOverrides.repeatEach, projectConfig.repeatEach);
|
|
|
|
|
projectConfig.retries = takeFirst(configCLIOverrides.retries, projectConfig.retries);
|
|
|
|
|
projectConfig.timeout = takeFirst(configCLIOverrides.timeout, projectConfig.timeout);
|
|
|
|
|
projectConfig.use = mergeObjects(projectConfig.use, configCLIOverrides.use);
|
2022-04-30 01:05:08 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-05 19:14:00 +02:00
|
|
|
private _resolveProject(config: Config, fullConfig: FullConfigInternal, projectConfig: Project, throwawayArtifactsPath: string): FullProjectInternal {
|
2022-03-24 00:05:49 +01:00
|
|
|
// Resolve all config dirs relative to configDir.
|
|
|
|
|
if (projectConfig.testDir !== undefined)
|
2023-02-02 00:25:26 +01:00
|
|
|
projectConfig.testDir = path.resolve(fullConfig._internal.configDir, projectConfig.testDir);
|
2022-03-24 00:05:49 +01:00
|
|
|
if (projectConfig.outputDir !== undefined)
|
2023-02-02 00:25:26 +01:00
|
|
|
projectConfig.outputDir = path.resolve(fullConfig._internal.configDir, projectConfig.outputDir);
|
2022-03-24 00:05:49 +01:00
|
|
|
if (projectConfig.snapshotDir !== undefined)
|
2023-02-02 00:25:26 +01:00
|
|
|
projectConfig.snapshotDir = path.resolve(fullConfig._internal.configDir, projectConfig.snapshotDir);
|
2022-03-24 00:05:49 +01:00
|
|
|
|
2023-02-02 00:25:26 +01:00
|
|
|
const testDir = takeFirst(projectConfig.testDir, config.testDir, fullConfig._internal.configDir);
|
2022-05-26 23:39:51 +02:00
|
|
|
const respectGitIgnore = !projectConfig.testDir && !config.testDir;
|
2022-03-24 00:05:49 +01:00
|
|
|
|
2022-04-29 22:32:39 +02:00
|
|
|
const outputDir = takeFirst(projectConfig.outputDir, config.outputDir, path.join(throwawayArtifactsPath, 'test-results'));
|
|
|
|
|
const snapshotDir = takeFirst(projectConfig.snapshotDir, config.snapshotDir, testDir);
|
|
|
|
|
const name = takeFirst(projectConfig.name, config.name, '');
|
2022-09-14 00:49:04 +02:00
|
|
|
|
2022-11-10 00:29:07 +01:00
|
|
|
const defaultSnapshotPathTemplate = '{snapshotDir}/{testFileDir}/{testFileName}-snapshots/{arg}{-projectName}{-snapshotSuffix}{ext}';
|
2022-11-10 20:37:41 +01:00
|
|
|
const snapshotPathTemplate = takeFirst(projectConfig.snapshotPathTemplate, config.snapshotPathTemplate, defaultSnapshotPathTemplate);
|
2022-04-30 01:05:08 +02:00
|
|
|
return {
|
2023-02-02 00:25:26 +01:00
|
|
|
_internal: {
|
|
|
|
|
id: '',
|
|
|
|
|
fullConfig: fullConfig,
|
|
|
|
|
fullyParallel: takeFirst(projectConfig.fullyParallel, config.fullyParallel, undefined),
|
|
|
|
|
expect: takeFirst(projectConfig.expect, config.expect, {}),
|
|
|
|
|
deps: [],
|
|
|
|
|
respectGitIgnore: respectGitIgnore,
|
|
|
|
|
},
|
2022-04-29 22:32:39 +02:00
|
|
|
grep: takeFirst(projectConfig.grep, config.grep, baseFullConfig.grep),
|
|
|
|
|
grepInvert: takeFirst(projectConfig.grepInvert, config.grepInvert, baseFullConfig.grepInvert),
|
2021-06-15 22:39:07 +02:00
|
|
|
outputDir,
|
2022-04-29 22:32:39 +02:00
|
|
|
repeatEach: takeFirst(projectConfig.repeatEach, config.repeatEach, 1),
|
|
|
|
|
retries: takeFirst(projectConfig.retries, config.retries, 0),
|
|
|
|
|
metadata: takeFirst(projectConfig.metadata, config.metadata, undefined),
|
2022-03-11 01:50:26 +01:00
|
|
|
name,
|
2021-06-07 02:09:53 +02:00
|
|
|
testDir,
|
2021-11-02 16:02:49 +01:00
|
|
|
snapshotDir,
|
2022-11-10 20:37:41 +01:00
|
|
|
snapshotPathTemplate,
|
2022-04-29 22:32:39 +02:00
|
|
|
testIgnore: takeFirst(projectConfig.testIgnore, config.testIgnore, []),
|
2023-03-27 23:28:44 +02:00
|
|
|
testMatch: takeFirst(projectConfig.testMatch, config.testMatch, '**/?(*.)@(spec|test).?(m)[jt]s?(x)'),
|
2022-04-29 22:32:39 +02:00
|
|
|
timeout: takeFirst(projectConfig.timeout, config.timeout, defaultTimeout),
|
2022-07-27 17:51:45 +02:00
|
|
|
use: mergeObjects(config.use, projectConfig.use),
|
2023-02-01 17:39:07 +01:00
|
|
|
dependencies: projectConfig.dependencies || [],
|
2021-06-07 02:09:53 +02:00
|
|
|
};
|
|
|
|
|
}
|
2023-01-27 21:44:15 +01:00
|
|
|
}
|
2021-07-12 18:59:58 +02:00
|
|
|
|
2023-01-27 21:44:15 +01:00
|
|
|
async function requireOrImportDefaultObject(file: string) {
|
|
|
|
|
let object = await requireOrImport(file);
|
|
|
|
|
if (object && typeof object === 'object' && ('default' in object))
|
|
|
|
|
object = object['default'];
|
|
|
|
|
return object;
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function takeFirst<T>(...args: (T | undefined)[]): T {
|
|
|
|
|
for (const arg of args) {
|
|
|
|
|
if (arg !== undefined)
|
|
|
|
|
return arg;
|
|
|
|
|
}
|
|
|
|
|
return undefined as any as T;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 19:30:54 +02:00
|
|
|
function validateConfig(file: string, config: Config) {
|
2021-06-07 02:09:53 +02:00
|
|
|
if (typeof config !== 'object' || !config)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `Configuration file must export a single object`);
|
2021-06-07 02:09:53 +02:00
|
|
|
|
2021-06-23 19:30:54 +02:00
|
|
|
validateProject(file, config, 'config');
|
2021-06-07 02:09:53 +02:00
|
|
|
|
|
|
|
|
if ('forbidOnly' in config && config.forbidOnly !== undefined) {
|
|
|
|
|
if (typeof config.forbidOnly !== 'boolean')
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.forbidOnly must be a boolean`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('globalSetup' in config && config.globalSetup !== undefined) {
|
|
|
|
|
if (typeof config.globalSetup !== 'string')
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.globalSetup must be a string`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('globalTeardown' in config && config.globalTeardown !== undefined) {
|
|
|
|
|
if (typeof config.globalTeardown !== 'string')
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.globalTeardown must be a string`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('globalTimeout' in config && config.globalTimeout !== undefined) {
|
|
|
|
|
if (typeof config.globalTimeout !== 'number' || config.globalTimeout < 0)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.globalTimeout must be a non-negative number`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('grep' in config && config.grep !== undefined) {
|
|
|
|
|
if (Array.isArray(config.grep)) {
|
|
|
|
|
config.grep.forEach((item, index) => {
|
|
|
|
|
if (!isRegExp(item))
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.grep[${index}] must be a RegExp`);
|
2021-06-07 02:09:53 +02:00
|
|
|
});
|
|
|
|
|
} else if (!isRegExp(config.grep)) {
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.grep must be a RegExp`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 02:56:59 +02:00
|
|
|
if ('grepInvert' in config && config.grepInvert !== undefined) {
|
|
|
|
|
if (Array.isArray(config.grepInvert)) {
|
|
|
|
|
config.grepInvert.forEach((item, index) => {
|
|
|
|
|
if (!isRegExp(item))
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.grepInvert[${index}] must be a RegExp`);
|
2021-06-19 02:56:59 +02:00
|
|
|
});
|
|
|
|
|
} else if (!isRegExp(config.grepInvert)) {
|
2022-09-29 03:45:01 +02:00
|
|
|
throw errorWithFile(file, `config.grepInvert must be a RegExp`);
|
2021-06-19 02:56:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 02:09:53 +02:00
|
|
|
if ('maxFailures' in config && config.maxFailures !== undefined) {
|
|
|
|
|
if (typeof config.maxFailures !== 'number' || config.maxFailures < 0)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.maxFailures must be a non-negative number`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('preserveOutput' in config && config.preserveOutput !== undefined) {
|
|
|
|
|
if (typeof config.preserveOutput !== 'string' || !['always', 'never', 'failures-only'].includes(config.preserveOutput))
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.preserveOutput must be one of "always", "never" or "failures-only"`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('projects' in config && config.projects !== undefined) {
|
|
|
|
|
if (!Array.isArray(config.projects))
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.projects must be an array`);
|
2021-06-07 02:09:53 +02:00
|
|
|
config.projects.forEach((project, index) => {
|
2021-06-23 19:30:54 +02:00
|
|
|
validateProject(file, project, `config.projects[${index}]`);
|
2021-06-07 02:09:53 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('quiet' in config && config.quiet !== undefined) {
|
|
|
|
|
if (typeof config.quiet !== 'boolean')
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.quiet must be a boolean`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('reporter' in config && config.reporter !== undefined) {
|
|
|
|
|
if (Array.isArray(config.reporter)) {
|
|
|
|
|
config.reporter.forEach((item, index) => {
|
|
|
|
|
if (!Array.isArray(item) || item.length <= 0 || item.length > 2 || typeof item[0] !== 'string')
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.reporter[${index}] must be a tuple [name, optionalArgument]`);
|
2021-06-07 02:09:53 +02:00
|
|
|
});
|
2021-07-20 22:03:01 +02:00
|
|
|
} else if (typeof config.reporter !== 'string') {
|
|
|
|
|
throw errorWithFile(file, `config.reporter must be a string`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 07:45:58 +02:00
|
|
|
if ('reportSlowTests' in config && config.reportSlowTests !== undefined && config.reportSlowTests !== null) {
|
|
|
|
|
if (!config.reportSlowTests || typeof config.reportSlowTests !== 'object')
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.reportSlowTests must be an object`);
|
2021-06-15 07:45:58 +02:00
|
|
|
if (!('max' in config.reportSlowTests) || typeof config.reportSlowTests.max !== 'number' || config.reportSlowTests.max < 0)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.reportSlowTests.max must be a non-negative number`);
|
2021-06-15 07:45:58 +02:00
|
|
|
if (!('threshold' in config.reportSlowTests) || typeof config.reportSlowTests.threshold !== 'number' || config.reportSlowTests.threshold < 0)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.reportSlowTests.threshold must be a non-negative number`);
|
2021-06-15 07:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-07 02:09:53 +02:00
|
|
|
if ('shard' in config && config.shard !== undefined && config.shard !== null) {
|
|
|
|
|
if (!config.shard || typeof config.shard !== 'object')
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.shard must be an object`);
|
2021-06-07 02:09:53 +02:00
|
|
|
if (!('total' in config.shard) || typeof config.shard.total !== 'number' || config.shard.total < 1)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.shard.total must be a positive number`);
|
2021-06-07 02:09:53 +02:00
|
|
|
if (!('current' in config.shard) || typeof config.shard.current !== 'number' || config.shard.current < 1 || config.shard.current > config.shard.total)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.shard.current must be a positive number, not greater than config.shard.total`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-01 14:34:36 +02:00
|
|
|
if ('ignoreSnapshots' in config && config.ignoreSnapshots !== undefined) {
|
|
|
|
|
if (typeof config.ignoreSnapshots !== 'boolean')
|
|
|
|
|
throw errorWithFile(file, `config.ignoreSnapshots must be a boolean`);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 02:09:53 +02:00
|
|
|
if ('updateSnapshots' in config && config.updateSnapshots !== undefined) {
|
|
|
|
|
if (typeof config.updateSnapshots !== 'string' || !['all', 'none', 'missing'].includes(config.updateSnapshots))
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.updateSnapshots must be one of "all", "none" or "missing"`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('workers' in config && config.workers !== undefined) {
|
2022-09-21 20:17:36 +02:00
|
|
|
if (typeof config.workers === 'number' && config.workers <= 0)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `config.workers must be a positive number`);
|
2022-09-21 20:17:36 +02:00
|
|
|
else if (typeof config.workers === 'string' && !config.workers.endsWith('%'))
|
|
|
|
|
throw errorWithFile(file, `config.workers must be a number or percentage`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 19:30:54 +02:00
|
|
|
function validateProject(file: string, project: Project, title: string) {
|
2021-06-07 02:09:53 +02:00
|
|
|
if (typeof project !== 'object' || !project)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `${title} must be an object`);
|
2021-06-07 02:09:53 +02:00
|
|
|
|
|
|
|
|
if ('name' in project && project.name !== undefined) {
|
|
|
|
|
if (typeof project.name !== 'string')
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `${title}.name must be a string`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('outputDir' in project && project.outputDir !== undefined) {
|
|
|
|
|
if (typeof project.outputDir !== 'string')
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `${title}.outputDir must be a string`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('repeatEach' in project && project.repeatEach !== undefined) {
|
|
|
|
|
if (typeof project.repeatEach !== 'number' || project.repeatEach < 0)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `${title}.repeatEach must be a non-negative number`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('retries' in project && project.retries !== undefined) {
|
|
|
|
|
if (typeof project.retries !== 'number' || project.retries < 0)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `${title}.retries must be a non-negative number`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('testDir' in project && project.testDir !== undefined) {
|
|
|
|
|
if (typeof project.testDir !== 'string')
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `${title}.testDir must be a string`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-18 21:56:03 +01:00
|
|
|
for (const prop of ['testIgnore', 'testMatch'] as const) {
|
2021-06-07 02:09:53 +02:00
|
|
|
if (prop in project && project[prop] !== undefined) {
|
|
|
|
|
const value = project[prop];
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
value.forEach((item, index) => {
|
|
|
|
|
if (typeof item !== 'string' && !isRegExp(item))
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `${title}.${prop}[${index}] must be a string or a RegExp`);
|
2021-06-07 02:09:53 +02:00
|
|
|
});
|
|
|
|
|
} else if (typeof value !== 'string' && !isRegExp(value)) {
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `${title}.${prop} must be a string or a RegExp`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('timeout' in project && project.timeout !== undefined) {
|
|
|
|
|
if (typeof project.timeout !== 'number' || project.timeout < 0)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `${title}.timeout must be a non-negative number`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('use' in project && project.use !== undefined) {
|
|
|
|
|
if (!project.use || typeof project.use !== 'object')
|
2021-06-23 19:30:54 +02:00
|
|
|
throw errorWithFile(file, `${title}.use must be an object`);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 01:49:36 +02:00
|
|
|
export const baseFullConfig: FullConfigInternal = {
|
2021-06-07 02:09:53 +02:00
|
|
|
forbidOnly: false,
|
2022-03-02 03:12:21 +01:00
|
|
|
fullyParallel: false,
|
2021-06-07 02:09:53 +02:00
|
|
|
globalSetup: null,
|
|
|
|
|
globalTeardown: null,
|
|
|
|
|
globalTimeout: 0,
|
|
|
|
|
grep: /.*/,
|
2021-06-19 02:56:59 +02:00
|
|
|
grepInvert: null,
|
2021-06-07 02:09:53 +02:00
|
|
|
maxFailures: 0,
|
2022-05-03 01:28:14 +02:00
|
|
|
metadata: {},
|
2021-06-07 02:09:53 +02:00
|
|
|
preserveOutput: 'always',
|
|
|
|
|
projects: [],
|
2022-08-18 20:12:33 +02:00
|
|
|
reporter: [[process.env.CI ? 'dot' : 'list']],
|
2022-04-29 01:49:36 +02:00
|
|
|
reportSlowTests: { max: 5, threshold: 15000 },
|
2022-09-14 23:56:28 +02:00
|
|
|
configFile: '',
|
2021-06-07 02:09:53 +02:00
|
|
|
rootDir: path.resolve(process.cwd()),
|
|
|
|
|
quiet: false,
|
|
|
|
|
shard: null,
|
|
|
|
|
updateSnapshots: 'missing',
|
2023-01-27 02:26:47 +01:00
|
|
|
version: require('../../package.json').version,
|
2022-09-21 20:17:36 +02:00
|
|
|
workers: 0,
|
2021-08-03 23:24:14 +02:00
|
|
|
webServer: null,
|
2023-02-02 00:25:26 +01:00
|
|
|
_internal: {
|
|
|
|
|
webServers: [],
|
|
|
|
|
globalOutputDir: path.resolve(process.cwd()),
|
|
|
|
|
configDir: '',
|
|
|
|
|
configCLIOverrides: {},
|
|
|
|
|
storeDir: '',
|
|
|
|
|
maxConcurrentTestGroups: 0,
|
|
|
|
|
ignoreSnapshots: false,
|
2023-02-03 17:44:01 +01:00
|
|
|
plugins: [],
|
2023-02-07 18:48:46 +01:00
|
|
|
cliArgs: [],
|
|
|
|
|
cliGrep: undefined,
|
|
|
|
|
cliGrepInvert: undefined,
|
2023-02-02 00:25:26 +01:00
|
|
|
listOnly: false,
|
2023-03-02 00:47:05 +01:00
|
|
|
defineConfigWasUsed: false,
|
2023-02-02 00:25:26 +01:00
|
|
|
}
|
2021-06-07 02:09:53 +02:00
|
|
|
};
|
2021-07-20 22:03:01 +02:00
|
|
|
|
|
|
|
|
function resolveReporters(reporters: Config['reporter'], rootDir: string): ReporterDescription[]|undefined {
|
|
|
|
|
return toReporters(reporters as any)?.map(([id, arg]) => {
|
|
|
|
|
if (builtInReporters.includes(id as any))
|
|
|
|
|
return [id, arg];
|
2022-08-18 20:12:33 +02:00
|
|
|
return [require.resolve(id, { paths: [rootDir] }), arg];
|
2021-07-20 22:03:01 +02:00
|
|
|
});
|
|
|
|
|
}
|
2021-07-20 22:13:40 +02:00
|
|
|
|
|
|
|
|
function resolveScript(id: string, rootDir: string) {
|
|
|
|
|
const localPath = path.resolve(rootDir, id);
|
|
|
|
|
if (fs.existsSync(localPath))
|
|
|
|
|
return localPath;
|
|
|
|
|
return require.resolve(id, { paths: [rootDir] });
|
|
|
|
|
}
|
2023-01-26 22:20:05 +01:00
|
|
|
|
2023-02-01 00:59:13 +01:00
|
|
|
function resolveProjectDependencies(projects: FullProjectInternal[]) {
|
|
|
|
|
for (const project of projects) {
|
2023-02-01 17:39:07 +01:00
|
|
|
for (const dependencyName of project.dependencies) {
|
2023-02-01 00:59:13 +01:00
|
|
|
const dependencies = projects.filter(p => p.name === dependencyName);
|
|
|
|
|
if (!dependencies.length)
|
|
|
|
|
throw new Error(`Project '${project.name}' depends on unknown project '${dependencyName}'`);
|
|
|
|
|
if (dependencies.length > 1)
|
|
|
|
|
throw new Error(`Project dependencies should have unique names, reading ${dependencyName}`);
|
2023-02-02 00:25:26 +01:00
|
|
|
project._internal.deps.push(...dependencies);
|
2023-02-01 00:59:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 22:20:05 +01:00
|
|
|
export const kDefaultConfigFiles = ['playwright.config.ts', 'playwright.config.js', 'playwright.config.mjs'];
|
|
|
|
|
|
|
|
|
|
export function resolveConfigFile(configFileOrDirectory: string): string | null {
|
|
|
|
|
const resolveConfig = (configFile: string) => {
|
|
|
|
|
if (fs.existsSync(configFile))
|
|
|
|
|
return configFile;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const resolveConfigFileFromDirectory = (directory: string) => {
|
|
|
|
|
for (const configName of kDefaultConfigFiles) {
|
|
|
|
|
const configFile = resolveConfig(path.resolve(directory, configName));
|
|
|
|
|
if (configFile)
|
|
|
|
|
return configFile;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!fs.existsSync(configFileOrDirectory))
|
|
|
|
|
throw new Error(`${configFileOrDirectory} does not exist`);
|
|
|
|
|
if (fs.statSync(configFileOrDirectory).isDirectory()) {
|
|
|
|
|
// When passed a directory, look for a config file inside.
|
|
|
|
|
const configFile = resolveConfigFileFromDirectory(configFileOrDirectory);
|
|
|
|
|
if (configFile)
|
|
|
|
|
return configFile;
|
|
|
|
|
// If there is no config, assume this as a root testing directory.
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
// When passed a file, it must be a config file.
|
|
|
|
|
const configFile = resolveConfig(configFileOrDirectory);
|
|
|
|
|
return configFile!;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-27 21:44:15 +01:00
|
|
|
|
|
|
|
|
export const builtInReporters = ['list', 'line', 'dot', 'json', 'junit', 'null', 'github', 'html'] as const;
|
|
|
|
|
export type BuiltInReporter = typeof builtInReporters[number];
|
|
|
|
|
|
|
|
|
|
export function toReporters(reporters: BuiltInReporter | ReporterDescription[] | undefined): ReporterDescription[] | undefined {
|
|
|
|
|
if (!reporters)
|
|
|
|
|
return;
|
|
|
|
|
if (typeof reporters === 'string')
|
|
|
|
|
return [[reporters]];
|
|
|
|
|
return reporters;
|
|
|
|
|
}
|