@lvce-editor/test-with-playwright-worker 22.6.0 → 22.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/workerMain.js +53 -88
  2. package/package.json +1 -1
@@ -1,10 +1,9 @@
1
1
  import { join, dirname, basename } from 'node:path';
2
- import { readFile, writeFile, readdir } from 'node:fs/promises';
2
+ import { readFile, writeFile, readdir, mkdtemp, rm } from 'node:fs/promises';
3
3
  import { pathToFileURL } from 'node:url';
4
4
  import net from 'node:net';
5
- import os from 'node:os';
6
- import { fork, spawn } from 'node:child_process';
7
- import { createInterface } from 'node:readline';
5
+ import os, { tmpdir } from 'node:os';
6
+ import { fork } from 'node:child_process';
8
7
  import { createRequire } from 'node:module';
9
8
 
10
9
  const require$1 = createRequire(import.meta.url);
@@ -1755,71 +1754,34 @@ const getElectronLaunchOptions = ({
1755
1754
  };
1756
1755
  };
1757
1756
 
1758
- const SIGINT = 'SIGINT';
1759
-
1760
- const devtoolsRegex = /^DevTools listening on (ws:\/\/.*)$/;
1761
- const waitForDevtoolsEndpoint = async child => {
1762
- const {
1763
- stderr
1764
- } = child;
1765
- if (!stderr) {
1766
- throw new Error('Electron stderr is unavailable');
1767
- }
1768
- const lines = createInterface({
1769
- input: stderr
1770
- });
1771
- const {
1772
- promise,
1773
- reject,
1774
- resolve
1775
- } = Promise.withResolvers();
1776
- const onExit = () => {
1777
- reject(new Error('Electron exited before DevTools endpoint was available'));
1778
- };
1779
- const onLine = line => {
1780
- const match = line.match(devtoolsRegex);
1781
- if (match) {
1782
- resolve(match[1]);
1783
- }
1784
- };
1785
- child.once('exit', onExit);
1786
- lines.on('line', onLine);
1787
- try {
1788
- return await promise;
1789
- } finally {
1790
- child.off('exit', onExit);
1791
- lines.off('line', onLine);
1792
- lines.close();
1793
- }
1794
- };
1795
- const getFirstPage = async browser => {
1796
- const context = browser.contexts()[0];
1797
- const pages = context.pages();
1798
- if (pages.length > 0) {
1799
- return pages[0];
1800
- }
1801
- return context.waitForEvent('page', {
1802
- timeout: 15_000
1803
- });
1757
+ const getElectronProcessArgs = ({
1758
+ args,
1759
+ platform = process.platform,
1760
+ userDataDir
1761
+ }) => {
1762
+ return [...(platform === 'linux' ? ['--no-sandbox'] : []), ...args, `--user-data-dir=${userDataDir}`];
1804
1763
  };
1764
+
1765
+ const electronLaunchTimeout = 120_000;
1805
1766
  const closeElectron = async ({
1806
- browser,
1807
- child
1767
+ electronApp,
1768
+ userDataDir
1808
1769
  }) => {
1809
1770
  try {
1810
- await browser.close();
1771
+ await electronApp.close();
1811
1772
  } catch {
1812
1773
  // ignore close errors during cleanup
1813
1774
  }
1814
- if (!child.killed) {
1815
- child.kill(SIGINT);
1816
- }
1775
+ await rm(userDataDir, {
1776
+ force: true,
1777
+ recursive: true
1778
+ });
1817
1779
  };
1818
1780
  const createElectronLaunch = ({
1819
- browser,
1820
- child,
1781
+ electronApp,
1821
1782
  page,
1822
- signal
1783
+ signal,
1784
+ userDataDir
1823
1785
  }) => {
1824
1786
  let disposed = false;
1825
1787
  const dispose = async () => {
@@ -1831,8 +1793,8 @@ const createElectronLaunch = ({
1831
1793
  process.off('SIGINT', handleSigint);
1832
1794
  process.off('SIGTERM', handleSigterm);
1833
1795
  await closeElectron({
1834
- browser,
1835
- child
1796
+ electronApp,
1797
+ userDataDir
1836
1798
  });
1837
1799
  };
1838
1800
  const handleAbort = () => {
@@ -1856,13 +1818,6 @@ const createElectronLaunch = ({
1856
1818
  signal.addEventListener('abort', handleAbort);
1857
1819
  process.once('SIGINT', handleSigint);
1858
1820
  process.once('SIGTERM', handleSigterm);
1859
- const electronApp = {
1860
- close: dispose,
1861
- process: () => {
1862
- return child;
1863
- },
1864
- [Symbol.asyncDispose]: dispose
1865
- };
1866
1821
  return {
1867
1822
  electronApp,
1868
1823
  page,
@@ -1874,39 +1829,49 @@ const startElectron = async ({
1874
1829
  signal
1875
1830
  }) => {
1876
1831
  const launchOptions = getElectronLaunchOptions(runtimeOptions);
1877
- const child = spawn(launchOptions.executablePath, ['--remote-debugging-port=0', ...launchOptions.args], {
1878
- env: launchOptions.env,
1879
- stdio: ['ignore', 'ignore', 'pipe']
1832
+ const userDataDir = await mkdtemp(join(tmpdir(), 'test-with-playwright-electron-'));
1833
+ const args = getElectronProcessArgs({
1834
+ args: launchOptions.args,
1835
+ userDataDir
1880
1836
  });
1881
- let browser;
1837
+ let electronApp;
1882
1838
  try {
1883
- const endpoint = await waitForDevtoolsEndpoint(child);
1884
1839
  const {
1885
- chromium
1840
+ _electron
1886
1841
  } = await import('@playwright/test');
1887
- browser = await chromium.connectOverCDP(endpoint);
1888
- const page = await getFirstPage(browser);
1842
+ electronApp = await _electron.launch({
1843
+ args: [...args],
1844
+ env: launchOptions.env,
1845
+ executablePath: launchOptions.executablePath,
1846
+ timeout: electronLaunchTimeout
1847
+ });
1848
+ const page = await electronApp.firstWindow({
1849
+ timeout: electronLaunchTimeout
1850
+ });
1889
1851
  return createElectronLaunch({
1890
- browser,
1891
- child,
1852
+ electronApp,
1892
1853
  page,
1893
- signal
1854
+ signal,
1855
+ userDataDir
1894
1856
  });
1895
1857
  } catch (error) {
1896
- if (browser) {
1897
- try {
1898
- await browser.close();
1899
- } catch {
1900
- // ignore close errors during cleanup
1901
- }
1902
- }
1903
- if (!child.killed) {
1904
- child.kill(SIGINT);
1858
+ if (electronApp) {
1859
+ await closeElectron({
1860
+ electronApp,
1861
+ userDataDir
1862
+ });
1863
+ } else {
1864
+ await rm(userDataDir, {
1865
+ force: true,
1866
+ recursive: true
1867
+ });
1905
1868
  }
1906
1869
  throw error;
1907
1870
  }
1908
1871
  };
1909
1872
 
1873
+ const SIGINT = 'SIGINT';
1874
+
1910
1875
  const tearDownTests = async ({
1911
1876
  child,
1912
1877
  controller,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/test-with-playwright-worker",
3
- "version": "22.6.0",
3
+ "version": "22.7.1",
4
4
  "description": "Worker package for test-with-playwright",
5
5
  "repository": {
6
6
  "type": "git",