config.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var config_exports = {};
  30. __export(config_exports, {
  31. FullConfigInternal: () => FullConfigInternal,
  32. FullProjectInternal: () => FullProjectInternal,
  33. builtInReporters: () => builtInReporters,
  34. defaultGrep: () => defaultGrep,
  35. defaultReporter: () => defaultReporter,
  36. defaultTimeout: () => defaultTimeout,
  37. getProjectId: () => getProjectId,
  38. takeFirst: () => takeFirst,
  39. toReporters: () => toReporters
  40. });
  41. module.exports = __toCommonJS(config_exports);
  42. var import_fs = __toESM(require("fs"));
  43. var import_os = __toESM(require("os"));
  44. var import_path = __toESM(require("path"));
  45. var import_util = require("../util");
  46. const defaultTimeout = 3e4;
  47. class FullConfigInternal {
  48. constructor(location, userConfig, configCLIOverrides, metadata) {
  49. this.projects = [];
  50. this.cliArgs = [];
  51. this.cliListOnly = false;
  52. this.preOnlyTestFilters = [];
  53. this.postShardTestFilters = [];
  54. this.defineConfigWasUsed = false;
  55. this.globalSetups = [];
  56. this.globalTeardowns = [];
  57. if (configCLIOverrides.projects && userConfig.projects)
  58. throw new Error(`Cannot use --browser option when configuration file defines projects. Specify browserName in the projects instead.`);
  59. const { resolvedConfigFile, configDir } = location;
  60. const packageJsonPath = (0, import_util.getPackageJsonPath)(configDir);
  61. const packageJsonDir = packageJsonPath ? import_path.default.dirname(packageJsonPath) : process.cwd();
  62. this.configDir = configDir;
  63. this.configCLIOverrides = configCLIOverrides;
  64. const privateConfiguration = userConfig["@playwright/test"];
  65. this.plugins = (privateConfiguration?.plugins || []).map((p) => ({ factory: p }));
  66. this.singleTSConfigPath = pathResolve(configDir, userConfig.tsconfig);
  67. this.captureGitInfo = userConfig.captureGitInfo;
  68. this.failOnFlakyTests = takeFirst(configCLIOverrides.failOnFlakyTests, userConfig.failOnFlakyTests, false);
  69. this.globalSetups = (Array.isArray(userConfig.globalSetup) ? userConfig.globalSetup : [userConfig.globalSetup]).map((s) => resolveScript(s, configDir)).filter((script) => script !== void 0);
  70. this.globalTeardowns = (Array.isArray(userConfig.globalTeardown) ? userConfig.globalTeardown : [userConfig.globalTeardown]).map((s) => resolveScript(s, configDir)).filter((script) => script !== void 0);
  71. userConfig.metadata = userConfig.metadata || {};
  72. this.config = {
  73. configFile: resolvedConfigFile,
  74. rootDir: pathResolve(configDir, userConfig.testDir) || configDir,
  75. forbidOnly: takeFirst(configCLIOverrides.forbidOnly, userConfig.forbidOnly, false),
  76. fullyParallel: takeFirst(configCLIOverrides.fullyParallel, userConfig.fullyParallel, false),
  77. globalSetup: this.globalSetups[0] ?? null,
  78. globalTeardown: this.globalTeardowns[0] ?? null,
  79. globalTimeout: takeFirst(configCLIOverrides.debug ? 0 : void 0, configCLIOverrides.globalTimeout, userConfig.globalTimeout, 0),
  80. grep: takeFirst(userConfig.grep, defaultGrep),
  81. grepInvert: takeFirst(userConfig.grepInvert, null),
  82. maxFailures: takeFirst(configCLIOverrides.debug ? 1 : void 0, configCLIOverrides.maxFailures, userConfig.maxFailures, 0),
  83. metadata: metadata ?? userConfig.metadata,
  84. preserveOutput: takeFirst(userConfig.preserveOutput, "always"),
  85. reporter: takeFirst(configCLIOverrides.reporter, resolveReporters(userConfig.reporter, configDir), [[defaultReporter]]),
  86. reportSlowTests: takeFirst(userConfig.reportSlowTests, {
  87. max: 5,
  88. threshold: 3e5
  89. /* 5 minutes */
  90. }),
  91. quiet: takeFirst(configCLIOverrides.quiet, userConfig.quiet, false),
  92. projects: [],
  93. shard: takeFirst(configCLIOverrides.shard, userConfig.shard, null),
  94. updateSnapshots: takeFirst(configCLIOverrides.updateSnapshots, userConfig.updateSnapshots, "missing"),
  95. updateSourceMethod: takeFirst(configCLIOverrides.updateSourceMethod, userConfig.updateSourceMethod, "patch"),
  96. version: require("../../package.json").version,
  97. workers: resolveWorkers(takeFirst(configCLIOverrides.debug ? 1 : void 0, configCLIOverrides.workers, userConfig.workers, "50%")),
  98. webServer: null
  99. };
  100. for (const key in userConfig) {
  101. if (key.startsWith("@"))
  102. this.config[key] = userConfig[key];
  103. }
  104. this.config[configInternalSymbol] = this;
  105. const webServers = takeFirst(userConfig.webServer, null);
  106. if (Array.isArray(webServers)) {
  107. this.config.webServer = null;
  108. this.webServers = webServers;
  109. } else if (webServers) {
  110. this.config.webServer = webServers;
  111. this.webServers = [webServers];
  112. } else {
  113. this.webServers = [];
  114. }
  115. const projectConfigs = configCLIOverrides.projects || userConfig.projects || [userConfig];
  116. this.projects = projectConfigs.map((p) => new FullProjectInternal(configDir, userConfig, this, p, this.configCLIOverrides, packageJsonDir));
  117. resolveProjectDependencies(this.projects);
  118. this._assignUniqueProjectIds(this.projects);
  119. this.config.projects = this.projects.map((p) => p.project);
  120. }
  121. _assignUniqueProjectIds(projects) {
  122. const usedNames = /* @__PURE__ */ new Set();
  123. for (const p of projects) {
  124. const name = p.project.name || "";
  125. for (let i = 0; i < projects.length; ++i) {
  126. const candidate = name + (i ? i : "");
  127. if (usedNames.has(candidate))
  128. continue;
  129. p.id = candidate;
  130. p.project.__projectId = p.id;
  131. usedNames.add(candidate);
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. class FullProjectInternal {
  138. constructor(configDir, config, fullConfig, projectConfig, configCLIOverrides, packageJsonDir) {
  139. this.id = "";
  140. this.deps = [];
  141. this.fullConfig = fullConfig;
  142. const testDir = takeFirst(pathResolve(configDir, projectConfig.testDir), pathResolve(configDir, config.testDir), fullConfig.configDir);
  143. this.snapshotPathTemplate = takeFirst(projectConfig.snapshotPathTemplate, config.snapshotPathTemplate);
  144. this.project = {
  145. grep: takeFirst(projectConfig.grep, config.grep, defaultGrep),
  146. grepInvert: takeFirst(projectConfig.grepInvert, config.grepInvert, null),
  147. outputDir: takeFirst(configCLIOverrides.outputDir, pathResolve(configDir, projectConfig.outputDir), pathResolve(configDir, config.outputDir), import_path.default.join(packageJsonDir, "test-results")),
  148. // Note: we either apply the cli override for repeatEach or not, depending on whether the
  149. // project is top-level vs dependency. See collectProjectsAndTestFiles in loadUtils.
  150. repeatEach: takeFirst(projectConfig.repeatEach, config.repeatEach, 1),
  151. retries: takeFirst(configCLIOverrides.retries, projectConfig.retries, config.retries, 0),
  152. metadata: takeFirst(projectConfig.metadata, config.metadata, {}),
  153. name: takeFirst(projectConfig.name, config.name, ""),
  154. testDir,
  155. snapshotDir: takeFirst(pathResolve(configDir, projectConfig.snapshotDir), pathResolve(configDir, config.snapshotDir), testDir),
  156. testIgnore: takeFirst(projectConfig.testIgnore, config.testIgnore, []),
  157. testMatch: takeFirst(projectConfig.testMatch, config.testMatch, "**/*.@(spec|test).?(c|m)[jt]s?(x)"),
  158. timeout: takeFirst(configCLIOverrides.debug ? 0 : void 0, configCLIOverrides.timeout, projectConfig.timeout, config.timeout, defaultTimeout),
  159. use: (0, import_util.mergeObjects)(config.use, projectConfig.use, configCLIOverrides.use),
  160. dependencies: projectConfig.dependencies || [],
  161. teardown: projectConfig.teardown
  162. };
  163. this.fullyParallel = takeFirst(configCLIOverrides.fullyParallel, projectConfig.fullyParallel, config.fullyParallel, void 0);
  164. this.expect = takeFirst(projectConfig.expect, config.expect, {});
  165. if (this.expect.toHaveScreenshot?.stylePath) {
  166. const stylePaths = Array.isArray(this.expect.toHaveScreenshot.stylePath) ? this.expect.toHaveScreenshot.stylePath : [this.expect.toHaveScreenshot.stylePath];
  167. this.expect.toHaveScreenshot.stylePath = stylePaths.map((stylePath) => import_path.default.resolve(configDir, stylePath));
  168. }
  169. this.respectGitIgnore = takeFirst(projectConfig.respectGitIgnore, config.respectGitIgnore, !projectConfig.testDir && !config.testDir);
  170. this.ignoreSnapshots = takeFirst(configCLIOverrides.ignoreSnapshots, projectConfig.ignoreSnapshots, config.ignoreSnapshots, false);
  171. this.workers = projectConfig.workers ? resolveWorkers(projectConfig.workers) : void 0;
  172. if (configCLIOverrides.debug && this.workers)
  173. this.workers = 1;
  174. }
  175. }
  176. function takeFirst(...args) {
  177. for (const arg of args) {
  178. if (arg !== void 0)
  179. return arg;
  180. }
  181. return void 0;
  182. }
  183. function pathResolve(baseDir, relative) {
  184. if (!relative)
  185. return void 0;
  186. return import_path.default.resolve(baseDir, relative);
  187. }
  188. function resolveReporters(reporters, rootDir) {
  189. return toReporters(reporters)?.map(([id, arg]) => {
  190. if (builtInReporters.includes(id))
  191. return [id, arg];
  192. return [require.resolve(id, { paths: [rootDir] }), arg];
  193. });
  194. }
  195. function resolveWorkers(workers) {
  196. if (typeof workers === "string") {
  197. if (workers.endsWith("%")) {
  198. const cpus = import_os.default.cpus().length;
  199. return Math.max(1, Math.floor(cpus * (parseInt(workers, 10) / 100)));
  200. }
  201. const parsedWorkers = parseInt(workers, 10);
  202. if (isNaN(parsedWorkers))
  203. throw new Error(`Workers ${workers} must be a number or percentage.`);
  204. return parsedWorkers;
  205. }
  206. return workers;
  207. }
  208. function resolveProjectDependencies(projects) {
  209. const teardownSet = /* @__PURE__ */ new Set();
  210. for (const project of projects) {
  211. for (const dependencyName of project.project.dependencies) {
  212. const dependencies = projects.filter((p) => p.project.name === dependencyName);
  213. if (!dependencies.length)
  214. throw new Error(`Project '${project.project.name}' depends on unknown project '${dependencyName}'`);
  215. if (dependencies.length > 1)
  216. throw new Error(`Project dependencies should have unique names, reading ${dependencyName}`);
  217. project.deps.push(...dependencies);
  218. }
  219. if (project.project.teardown) {
  220. const teardowns = projects.filter((p) => p.project.name === project.project.teardown);
  221. if (!teardowns.length)
  222. throw new Error(`Project '${project.project.name}' has unknown teardown project '${project.project.teardown}'`);
  223. if (teardowns.length > 1)
  224. throw new Error(`Project teardowns should have unique names, reading ${project.project.teardown}`);
  225. const teardown = teardowns[0];
  226. project.teardown = teardown;
  227. teardownSet.add(teardown);
  228. }
  229. }
  230. for (const teardown of teardownSet) {
  231. if (teardown.deps.length)
  232. throw new Error(`Teardown project ${teardown.project.name} must not have dependencies`);
  233. }
  234. for (const project of projects) {
  235. for (const dep of project.deps) {
  236. if (teardownSet.has(dep))
  237. throw new Error(`Project ${project.project.name} must not depend on a teardown project ${dep.project.name}`);
  238. }
  239. }
  240. }
  241. function toReporters(reporters) {
  242. if (!reporters)
  243. return;
  244. if (typeof reporters === "string")
  245. return [[reporters]];
  246. return reporters;
  247. }
  248. const builtInReporters = ["list", "line", "dot", "json", "junit", "null", "github", "html", "blob"];
  249. function resolveScript(id, rootDir) {
  250. if (!id)
  251. return void 0;
  252. const localPath = import_path.default.resolve(rootDir, id);
  253. if (import_fs.default.existsSync(localPath))
  254. return localPath;
  255. return require.resolve(id, { paths: [rootDir] });
  256. }
  257. const defaultGrep = /.*/;
  258. const defaultReporter = process.env.CI ? "dot" : "list";
  259. const configInternalSymbol = Symbol("configInternalSymbol");
  260. function getProjectId(project) {
  261. return project.__projectId;
  262. }
  263. // Annotate the CommonJS export names for ESM import in node:
  264. 0 && (module.exports = {
  265. FullConfigInternal,
  266. FullProjectInternal,
  267. builtInReporters,
  268. defaultGrep,
  269. defaultReporter,
  270. defaultTimeout,
  271. getProjectId,
  272. takeFirst,
  273. toReporters
  274. });