fixtureRunner.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var fixtureRunner_exports = {};
  20. __export(fixtureRunner_exports, {
  21. FixtureRunner: () => FixtureRunner
  22. });
  23. module.exports = __toCommonJS(fixtureRunner_exports);
  24. var import_utils = require("playwright-core/lib/utils");
  25. var import_fixtures = require("../common/fixtures");
  26. var import_util = require("../util");
  27. class Fixture {
  28. constructor(runner, registration) {
  29. this.failed = false;
  30. this._deps = /* @__PURE__ */ new Set();
  31. this._usages = /* @__PURE__ */ new Set();
  32. this.runner = runner;
  33. this.registration = registration;
  34. this.value = null;
  35. const isUserFixture = this.registration.location && (0, import_util.filterStackFile)(this.registration.location.file);
  36. const title = this.registration.customTitle || this.registration.name;
  37. const location = isUserFixture ? this.registration.location : void 0;
  38. this._stepInfo = { title: `Fixture ${(0, import_utils.escapeWithQuotes)(title, '"')}`, category: "fixture", location };
  39. if (this.registration.box)
  40. this._stepInfo.group = isUserFixture ? "configuration" : "internal";
  41. this._setupDescription = {
  42. title,
  43. phase: "setup",
  44. location,
  45. slot: this.registration.timeout === void 0 ? void 0 : {
  46. timeout: this.registration.timeout,
  47. elapsed: 0
  48. }
  49. };
  50. this._teardownDescription = { ...this._setupDescription, phase: "teardown" };
  51. }
  52. async setup(testInfo, runnable) {
  53. this.runner.instanceForId.set(this.registration.id, this);
  54. if (typeof this.registration.fn !== "function") {
  55. this.value = this.registration.fn;
  56. return;
  57. }
  58. await testInfo._runAsStep(this._stepInfo, async () => {
  59. await testInfo._runWithTimeout({ ...runnable, fixture: this._setupDescription }, () => this._setupInternal(testInfo));
  60. });
  61. }
  62. async _setupInternal(testInfo) {
  63. const params = {};
  64. for (const name of this.registration.deps) {
  65. const registration = this.runner.pool.resolve(name, this.registration);
  66. const dep = this.runner.instanceForId.get(registration.id);
  67. if (!dep) {
  68. this.failed = true;
  69. return;
  70. }
  71. dep._usages.add(this);
  72. this._deps.add(dep);
  73. params[name] = dep.value;
  74. if (dep.failed) {
  75. this.failed = true;
  76. return;
  77. }
  78. }
  79. let called = false;
  80. const useFuncStarted = new import_utils.ManualPromise();
  81. const useFunc = async (value) => {
  82. if (called)
  83. throw new Error(`Cannot provide fixture value for the second time`);
  84. called = true;
  85. this.value = value;
  86. this._useFuncFinished = new import_utils.ManualPromise();
  87. useFuncStarted.resolve();
  88. await this._useFuncFinished;
  89. };
  90. const workerInfo = { config: testInfo.config, parallelIndex: testInfo.parallelIndex, workerIndex: testInfo.workerIndex, project: testInfo.project };
  91. const info = this.registration.scope === "worker" ? workerInfo : testInfo;
  92. this._selfTeardownComplete = (async () => {
  93. try {
  94. await this.registration.fn(params, useFunc, info);
  95. } catch (error) {
  96. this.failed = true;
  97. if (!useFuncStarted.isDone())
  98. useFuncStarted.reject(error);
  99. else
  100. throw error;
  101. }
  102. })();
  103. await useFuncStarted;
  104. }
  105. async teardown(testInfo, runnable) {
  106. try {
  107. const fixtureRunnable = { ...runnable, fixture: this._teardownDescription };
  108. if (!testInfo._timeoutManager.isTimeExhaustedFor(fixtureRunnable)) {
  109. await testInfo._runAsStep(this._stepInfo, async () => {
  110. await testInfo._runWithTimeout(fixtureRunnable, () => this._teardownInternal());
  111. });
  112. }
  113. } finally {
  114. for (const dep of this._deps)
  115. dep._usages.delete(this);
  116. this.runner.instanceForId.delete(this.registration.id);
  117. }
  118. }
  119. async _teardownInternal() {
  120. if (typeof this.registration.fn !== "function")
  121. return;
  122. if (this._usages.size !== 0) {
  123. console.error("Internal error: fixture integrity at", this._teardownDescription.title);
  124. this._usages.clear();
  125. }
  126. if (this._useFuncFinished) {
  127. this._useFuncFinished.resolve();
  128. this._useFuncFinished = void 0;
  129. await this._selfTeardownComplete;
  130. }
  131. }
  132. _collectFixturesInTeardownOrder(scope, collector) {
  133. if (this.registration.scope !== scope)
  134. return;
  135. for (const fixture of this._usages)
  136. fixture._collectFixturesInTeardownOrder(scope, collector);
  137. collector.add(this);
  138. }
  139. }
  140. class FixtureRunner {
  141. constructor() {
  142. this.testScopeClean = true;
  143. this.instanceForId = /* @__PURE__ */ new Map();
  144. }
  145. setPool(pool) {
  146. if (!this.testScopeClean)
  147. throw new Error("Did not teardown test scope");
  148. if (this.pool && pool.digest !== this.pool.digest) {
  149. throw new Error([
  150. `Playwright detected inconsistent test.use() options.`,
  151. `Most common mistakes that lead to this issue:`,
  152. ` - Calling test.use() outside of the test file, for example in a common helper.`,
  153. ` - One test file imports from another test file.`
  154. ].join("\n"));
  155. }
  156. this.pool = pool;
  157. }
  158. _collectFixturesInSetupOrder(registration, collector) {
  159. if (collector.has(registration))
  160. return;
  161. for (const name of registration.deps) {
  162. const dep = this.pool.resolve(name, registration);
  163. this._collectFixturesInSetupOrder(dep, collector);
  164. }
  165. collector.add(registration);
  166. }
  167. async teardownScope(scope, testInfo, runnable) {
  168. const fixtures = Array.from(this.instanceForId.values()).reverse();
  169. const collector = /* @__PURE__ */ new Set();
  170. for (const fixture of fixtures)
  171. fixture._collectFixturesInTeardownOrder(scope, collector);
  172. let firstError;
  173. for (const fixture of collector) {
  174. try {
  175. await fixture.teardown(testInfo, runnable);
  176. } catch (error) {
  177. firstError = firstError ?? error;
  178. }
  179. }
  180. if (scope === "test")
  181. this.testScopeClean = true;
  182. if (firstError)
  183. throw firstError;
  184. }
  185. async resolveParametersForFunction(fn, testInfo, autoFixtures, runnable) {
  186. const collector = /* @__PURE__ */ new Set();
  187. const auto = [];
  188. for (const registration of this.pool.autoFixtures()) {
  189. let shouldRun = true;
  190. if (autoFixtures === "all-hooks-only")
  191. shouldRun = registration.scope === "worker" || registration.auto === "all-hooks-included";
  192. else if (autoFixtures === "worker")
  193. shouldRun = registration.scope === "worker";
  194. if (shouldRun)
  195. auto.push(registration);
  196. }
  197. auto.sort((r1, r2) => (r1.scope === "worker" ? 0 : 1) - (r2.scope === "worker" ? 0 : 1));
  198. for (const registration of auto)
  199. this._collectFixturesInSetupOrder(registration, collector);
  200. const names = getRequiredFixtureNames(fn);
  201. for (const name of names)
  202. this._collectFixturesInSetupOrder(this.pool.resolve(name), collector);
  203. for (const registration of collector)
  204. await this._setupFixtureForRegistration(registration, testInfo, runnable);
  205. const params = {};
  206. for (const name of names) {
  207. const registration = this.pool.resolve(name);
  208. const fixture = this.instanceForId.get(registration.id);
  209. if (!fixture || fixture.failed)
  210. return null;
  211. params[name] = fixture.value;
  212. }
  213. return params;
  214. }
  215. async resolveParametersAndRunFunction(fn, testInfo, autoFixtures, runnable) {
  216. const params = await this.resolveParametersForFunction(fn, testInfo, autoFixtures, runnable);
  217. if (params === null) {
  218. return null;
  219. }
  220. await testInfo._runWithTimeout(runnable, () => fn(params, testInfo));
  221. }
  222. async _setupFixtureForRegistration(registration, testInfo, runnable) {
  223. if (registration.scope === "test")
  224. this.testScopeClean = false;
  225. let fixture = this.instanceForId.get(registration.id);
  226. if (fixture)
  227. return fixture;
  228. fixture = new Fixture(this, registration);
  229. await fixture.setup(testInfo, runnable);
  230. return fixture;
  231. }
  232. dependsOnWorkerFixturesOnly(fn, location) {
  233. const names = getRequiredFixtureNames(fn, location);
  234. for (const name of names) {
  235. const registration = this.pool.resolve(name);
  236. if (registration.scope !== "worker")
  237. return false;
  238. }
  239. return true;
  240. }
  241. }
  242. function getRequiredFixtureNames(fn, location) {
  243. return (0, import_fixtures.fixtureParameterNames)(fn, location ?? { file: "<unknown>", line: 1, column: 1 }, (e) => {
  244. throw new Error(`${(0, import_util.formatLocation)(e.location)}: ${e.message}`);
  245. });
  246. }
  247. // Annotate the CommonJS export names for ESM import in node:
  248. 0 && (module.exports = {
  249. FixtureRunner
  250. });