watchMode.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 watchMode_exports = {};
  30. __export(watchMode_exports, {
  31. runWatchModeLoop: () => runWatchModeLoop
  32. });
  33. module.exports = __toCommonJS(watchMode_exports);
  34. var import_fs = __toESM(require("fs"));
  35. var import_path = __toESM(require("path"));
  36. var import_readline = __toESM(require("readline"));
  37. var import_stream = require("stream");
  38. var import_playwrightServer = require("playwright-core/lib/remote/playwrightServer");
  39. var import_utils = require("playwright-core/lib/utils");
  40. var import_utils2 = require("playwright-core/lib/utils");
  41. var import_base = require("../reporters/base");
  42. var import_utilsBundle = require("../utilsBundle");
  43. var import_testServer = require("./testServer");
  44. var import_teleSuiteUpdater = require("../isomorphic/teleSuiteUpdater");
  45. var import_testServerConnection = require("../isomorphic/testServerConnection");
  46. var import_util = require("../util");
  47. var import_babelBundle = require("../transform/babelBundle");
  48. class InMemoryTransport extends import_stream.EventEmitter {
  49. constructor(send) {
  50. super();
  51. this._send = send;
  52. }
  53. close() {
  54. this.emit("close");
  55. }
  56. onclose(listener) {
  57. this.on("close", listener);
  58. }
  59. onerror(listener) {
  60. }
  61. onmessage(listener) {
  62. this.on("message", listener);
  63. }
  64. onopen(listener) {
  65. this.on("open", listener);
  66. }
  67. send(data) {
  68. this._send(data);
  69. }
  70. }
  71. async function runWatchModeLoop(configLocation, initialOptions) {
  72. const options = { ...initialOptions };
  73. let bufferMode = false;
  74. const testServerDispatcher = new import_testServer.TestServerDispatcher(configLocation, {});
  75. const transport = new InMemoryTransport(
  76. async (data) => {
  77. const { id, method, params } = JSON.parse(data);
  78. try {
  79. const result2 = await testServerDispatcher.transport.dispatch(method, params);
  80. transport.emit("message", JSON.stringify({ id, result: result2 }));
  81. } catch (e) {
  82. transport.emit("message", JSON.stringify({ id, error: String(e) }));
  83. }
  84. }
  85. );
  86. testServerDispatcher.transport.sendEvent = (method, params) => {
  87. transport.emit("message", JSON.stringify({ method, params }));
  88. };
  89. const testServerConnection = new import_testServerConnection.TestServerConnection(transport);
  90. transport.emit("open");
  91. const teleSuiteUpdater = new import_teleSuiteUpdater.TeleSuiteUpdater({ pathSeparator: import_path.default.sep, onUpdate() {
  92. } });
  93. const dirtyTestFiles = /* @__PURE__ */ new Set();
  94. const dirtyTestIds = /* @__PURE__ */ new Set();
  95. let onDirtyTests = new import_utils.ManualPromise();
  96. let queue = Promise.resolve();
  97. const changedFiles = /* @__PURE__ */ new Set();
  98. testServerConnection.onTestFilesChanged(({ testFiles }) => {
  99. testFiles.forEach((file) => changedFiles.add(file));
  100. queue = queue.then(async () => {
  101. if (changedFiles.size === 0)
  102. return;
  103. const { report: report2 } = await testServerConnection.listTests({ locations: options.files, projects: options.projects, grep: options.grep });
  104. teleSuiteUpdater.processListReport(report2);
  105. for (const test of teleSuiteUpdater.rootSuite.allTests()) {
  106. if (changedFiles.has(test.location.file)) {
  107. dirtyTestFiles.add(test.location.file);
  108. dirtyTestIds.add(test.id);
  109. }
  110. }
  111. changedFiles.clear();
  112. if (dirtyTestIds.size > 0) {
  113. onDirtyTests.resolve("changed");
  114. onDirtyTests = new import_utils.ManualPromise();
  115. }
  116. });
  117. });
  118. testServerConnection.onReport((report2) => teleSuiteUpdater.processTestReportEvent(report2));
  119. testServerConnection.onRecoverFromStepError(({ stepId, message, location }) => {
  120. process.stdout.write(`
  121. Test error occurred.
  122. `);
  123. process.stdout.write("\n" + createErrorCodeframe(message, location) + "\n");
  124. process.stdout.write(`
  125. ${import_utils2.colors.dim("Try recovering from the error. Press")} ${import_utils2.colors.bold("c")} ${import_utils2.colors.dim("to continue or")} ${import_utils2.colors.bold("t")} ${import_utils2.colors.dim("to throw the error")}
  126. `);
  127. readKeyPress((text) => {
  128. if (text === "c") {
  129. process.stdout.write(`
  130. ${import_utils2.colors.dim("Continuing after recovery...")}
  131. `);
  132. testServerConnection.resumeAfterStepError({ stepId, status: "recovered", value: void 0 }).catch(() => {
  133. });
  134. } else if (text === "t") {
  135. process.stdout.write(`
  136. ${import_utils2.colors.dim("Throwing error...")}
  137. `);
  138. testServerConnection.resumeAfterStepError({ stepId, status: "failed" }).catch(() => {
  139. });
  140. }
  141. return text;
  142. });
  143. });
  144. await testServerConnection.initialize({
  145. interceptStdio: false,
  146. watchTestDirs: true,
  147. populateDependenciesOnList: true,
  148. recoverFromStepErrors: !process.env.PWTEST_RECOVERY_DISABLED
  149. });
  150. await testServerConnection.runGlobalSetup({});
  151. const { report } = await testServerConnection.listTests({});
  152. teleSuiteUpdater.processListReport(report);
  153. const projectNames = teleSuiteUpdater.rootSuite.suites.map((s) => s.title);
  154. let lastRun = { type: "regular" };
  155. let result = "passed";
  156. while (true) {
  157. if (bufferMode)
  158. printBufferPrompt(dirtyTestFiles, teleSuiteUpdater.config.rootDir);
  159. else
  160. printPrompt();
  161. const waitForCommand = readCommand();
  162. const command = await Promise.race([
  163. onDirtyTests,
  164. waitForCommand.result
  165. ]);
  166. if (command === "changed")
  167. waitForCommand.dispose();
  168. if (bufferMode && command === "changed")
  169. continue;
  170. const shouldRunChangedFiles = bufferMode ? command === "run" : command === "changed";
  171. if (shouldRunChangedFiles) {
  172. if (dirtyTestIds.size === 0)
  173. continue;
  174. const testIds = [...dirtyTestIds];
  175. dirtyTestIds.clear();
  176. dirtyTestFiles.clear();
  177. await runTests(options, testServerConnection, { testIds, title: "files changed" });
  178. lastRun = { type: "changed", dirtyTestIds: testIds };
  179. continue;
  180. }
  181. if (command === "run") {
  182. await runTests(options, testServerConnection);
  183. lastRun = { type: "regular" };
  184. continue;
  185. }
  186. if (command === "project") {
  187. const { selectedProjects } = await import_utilsBundle.enquirer.prompt({
  188. type: "multiselect",
  189. name: "selectedProjects",
  190. message: "Select projects",
  191. choices: projectNames
  192. }).catch(() => ({ selectedProjects: null }));
  193. if (!selectedProjects)
  194. continue;
  195. options.projects = selectedProjects.length ? selectedProjects : void 0;
  196. await runTests(options, testServerConnection);
  197. lastRun = { type: "regular" };
  198. continue;
  199. }
  200. if (command === "file") {
  201. const { filePattern } = await import_utilsBundle.enquirer.prompt({
  202. type: "text",
  203. name: "filePattern",
  204. message: "Input filename pattern (regex)"
  205. }).catch(() => ({ filePattern: null }));
  206. if (filePattern === null)
  207. continue;
  208. if (filePattern.trim())
  209. options.files = filePattern.split(" ");
  210. else
  211. options.files = void 0;
  212. await runTests(options, testServerConnection);
  213. lastRun = { type: "regular" };
  214. continue;
  215. }
  216. if (command === "grep") {
  217. const { testPattern } = await import_utilsBundle.enquirer.prompt({
  218. type: "text",
  219. name: "testPattern",
  220. message: "Input test name pattern (regex)"
  221. }).catch(() => ({ testPattern: null }));
  222. if (testPattern === null)
  223. continue;
  224. if (testPattern.trim())
  225. options.grep = testPattern;
  226. else
  227. options.grep = void 0;
  228. await runTests(options, testServerConnection);
  229. lastRun = { type: "regular" };
  230. continue;
  231. }
  232. if (command === "failed") {
  233. const failedTestIds = teleSuiteUpdater.rootSuite.allTests().filter((t) => !t.ok()).map((t) => t.id);
  234. await runTests({}, testServerConnection, { title: "running failed tests", testIds: failedTestIds });
  235. lastRun = { type: "failed", failedTestIds };
  236. continue;
  237. }
  238. if (command === "repeat") {
  239. if (lastRun.type === "regular") {
  240. await runTests(options, testServerConnection, { title: "re-running tests" });
  241. continue;
  242. } else if (lastRun.type === "changed") {
  243. await runTests(options, testServerConnection, { title: "re-running tests", testIds: lastRun.dirtyTestIds });
  244. } else if (lastRun.type === "failed") {
  245. await runTests({}, testServerConnection, { title: "re-running tests", testIds: lastRun.failedTestIds });
  246. }
  247. continue;
  248. }
  249. if (command === "toggle-show-browser") {
  250. await toggleShowBrowser();
  251. continue;
  252. }
  253. if (command === "toggle-buffer-mode") {
  254. bufferMode = !bufferMode;
  255. continue;
  256. }
  257. if (command === "exit")
  258. break;
  259. if (command === "interrupted") {
  260. result = "interrupted";
  261. break;
  262. }
  263. }
  264. const teardown = await testServerConnection.runGlobalTeardown({});
  265. return result === "passed" ? teardown.status : result;
  266. }
  267. function readKeyPress(handler) {
  268. const promise = new import_utils.ManualPromise();
  269. const rl = import_readline.default.createInterface({ input: process.stdin, escapeCodeTimeout: 50 });
  270. import_readline.default.emitKeypressEvents(process.stdin, rl);
  271. if (process.stdin.isTTY)
  272. process.stdin.setRawMode(true);
  273. const listener = import_utils.eventsHelper.addEventListener(process.stdin, "keypress", (text, key) => {
  274. const result = handler(text, key);
  275. if (result)
  276. promise.resolve(result);
  277. });
  278. const dispose = () => {
  279. import_utils.eventsHelper.removeEventListeners([listener]);
  280. rl.close();
  281. if (process.stdin.isTTY)
  282. process.stdin.setRawMode(false);
  283. };
  284. void promise.finally(dispose);
  285. return { result: promise, dispose };
  286. }
  287. const isInterrupt = (text, key) => text === "" || text === "\x1B" || key && key.name === "escape" || key && key.ctrl && key.name === "c";
  288. async function runTests(watchOptions, testServerConnection, options) {
  289. printConfiguration(watchOptions, options?.title);
  290. const waitForDone = readKeyPress((text, key) => {
  291. if (isInterrupt(text, key)) {
  292. testServerConnection.stopTestsNoReply({});
  293. return "done";
  294. }
  295. });
  296. await testServerConnection.runTests({
  297. grep: watchOptions.grep,
  298. testIds: options?.testIds,
  299. locations: watchOptions?.files,
  300. projects: watchOptions.projects,
  301. connectWsEndpoint,
  302. reuseContext: connectWsEndpoint ? true : void 0,
  303. workers: connectWsEndpoint ? 1 : void 0,
  304. headed: connectWsEndpoint ? true : void 0
  305. }).finally(() => waitForDone.dispose());
  306. }
  307. function readCommand() {
  308. return readKeyPress((text, key) => {
  309. if (isInterrupt(text, key))
  310. return "interrupted";
  311. if (process.platform !== "win32" && key && key.ctrl && key.name === "z") {
  312. process.kill(process.ppid, "SIGTSTP");
  313. process.kill(process.pid, "SIGTSTP");
  314. }
  315. const name = key?.name;
  316. if (name === "q")
  317. return "exit";
  318. if (name === "h") {
  319. process.stdout.write(`${(0, import_base.separator)(import_base.terminalScreen)}
  320. Run tests
  321. ${import_utils2.colors.bold("enter")} ${import_utils2.colors.dim("run tests")}
  322. ${import_utils2.colors.bold("f")} ${import_utils2.colors.dim("run failed tests")}
  323. ${import_utils2.colors.bold("r")} ${import_utils2.colors.dim("repeat last run")}
  324. ${import_utils2.colors.bold("q")} ${import_utils2.colors.dim("quit")}
  325. Change settings
  326. ${import_utils2.colors.bold("c")} ${import_utils2.colors.dim("set project")}
  327. ${import_utils2.colors.bold("p")} ${import_utils2.colors.dim("set file filter")}
  328. ${import_utils2.colors.bold("t")} ${import_utils2.colors.dim("set title filter")}
  329. ${import_utils2.colors.bold("s")} ${import_utils2.colors.dim("toggle show & reuse the browser")}
  330. ${import_utils2.colors.bold("b")} ${import_utils2.colors.dim("toggle buffer mode")}
  331. `);
  332. return;
  333. }
  334. switch (name) {
  335. case "return":
  336. return "run";
  337. case "r":
  338. return "repeat";
  339. case "c":
  340. return "project";
  341. case "p":
  342. return "file";
  343. case "t":
  344. return "grep";
  345. case "f":
  346. return "failed";
  347. case "s":
  348. return "toggle-show-browser";
  349. case "b":
  350. return "toggle-buffer-mode";
  351. }
  352. });
  353. }
  354. let showBrowserServer;
  355. let connectWsEndpoint = void 0;
  356. let seq = 1;
  357. function printConfiguration(options, title) {
  358. const packageManagerCommand = (0, import_utils.getPackageManagerExecCommand)();
  359. const tokens = [];
  360. tokens.push(`${packageManagerCommand} playwright test`);
  361. if (options.projects)
  362. tokens.push(...options.projects.map((p) => import_utils2.colors.blue(`--project ${p}`)));
  363. if (options.grep)
  364. tokens.push(import_utils2.colors.red(`--grep ${options.grep}`));
  365. if (options.files)
  366. tokens.push(...options.files.map((a) => import_utils2.colors.bold(a)));
  367. if (title)
  368. tokens.push(import_utils2.colors.dim(`(${title})`));
  369. tokens.push(import_utils2.colors.dim(`#${seq++}`));
  370. const lines = [];
  371. const sep = (0, import_base.separator)(import_base.terminalScreen);
  372. lines.push("\x1Bc" + sep);
  373. lines.push(`${tokens.join(" ")}`);
  374. lines.push(`${import_utils2.colors.dim("Show & reuse browser:")} ${import_utils2.colors.bold(showBrowserServer ? "on" : "off")}`);
  375. process.stdout.write(lines.join("\n"));
  376. }
  377. function printBufferPrompt(dirtyTestFiles, rootDir) {
  378. const sep = (0, import_base.separator)(import_base.terminalScreen);
  379. process.stdout.write("\x1Bc");
  380. process.stdout.write(`${sep}
  381. `);
  382. if (dirtyTestFiles.size === 0) {
  383. process.stdout.write(`${import_utils2.colors.dim("Waiting for file changes. Press")} ${import_utils2.colors.bold("q")} ${import_utils2.colors.dim("to quit or")} ${import_utils2.colors.bold("h")} ${import_utils2.colors.dim("for more options.")}
  384. `);
  385. return;
  386. }
  387. process.stdout.write(`${import_utils2.colors.dim(`${dirtyTestFiles.size} test ${dirtyTestFiles.size === 1 ? "file" : "files"} changed:`)}
  388. `);
  389. for (const file of dirtyTestFiles)
  390. process.stdout.write(` \xB7 ${import_path.default.relative(rootDir, file)}
  391. `);
  392. process.stdout.write(`
  393. ${import_utils2.colors.dim(`Press`)} ${import_utils2.colors.bold("enter")} ${import_utils2.colors.dim("to run")}, ${import_utils2.colors.bold("q")} ${import_utils2.colors.dim("to quit or")} ${import_utils2.colors.bold("h")} ${import_utils2.colors.dim("for more options.")}
  394. `);
  395. }
  396. function printPrompt() {
  397. const sep = (0, import_base.separator)(import_base.terminalScreen);
  398. process.stdout.write(`
  399. ${sep}
  400. ${import_utils2.colors.dim("Waiting for file changes. Press")} ${import_utils2.colors.bold("enter")} ${import_utils2.colors.dim("to run tests")}, ${import_utils2.colors.bold("q")} ${import_utils2.colors.dim("to quit or")} ${import_utils2.colors.bold("h")} ${import_utils2.colors.dim("for more options.")}
  401. `);
  402. }
  403. async function toggleShowBrowser() {
  404. if (!showBrowserServer) {
  405. showBrowserServer = new import_playwrightServer.PlaywrightServer({ mode: "extension", path: "/" + (0, import_utils.createGuid)(), maxConnections: 1 });
  406. connectWsEndpoint = await showBrowserServer.listen();
  407. process.stdout.write(`${import_utils2.colors.dim("Show & reuse browser:")} ${import_utils2.colors.bold("on")}
  408. `);
  409. } else {
  410. await showBrowserServer?.close();
  411. showBrowserServer = void 0;
  412. connectWsEndpoint = void 0;
  413. process.stdout.write(`${import_utils2.colors.dim("Show & reuse browser:")} ${import_utils2.colors.bold("off")}
  414. `);
  415. }
  416. }
  417. function createErrorCodeframe(message, location) {
  418. let source;
  419. try {
  420. source = import_fs.default.readFileSync(location.file, "utf-8") + "\n//";
  421. } catch (e) {
  422. return;
  423. }
  424. return (0, import_babelBundle.codeFrameColumns)(
  425. source,
  426. {
  427. start: {
  428. line: location.line,
  429. column: location.column
  430. }
  431. },
  432. {
  433. highlightCode: true,
  434. linesAbove: 5,
  435. linesBelow: 5,
  436. message: (0, import_util.stripAnsiEscapes)(message).split("\n")[0] || void 0
  437. }
  438. );
  439. }
  440. // Annotate the CommonJS export names for ESM import in node:
  441. 0 && (module.exports = {
  442. runWatchModeLoop
  443. });