fixtureRunner.js 9.5 KB

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