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

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 +64 -15
  2. package/package.json +1 -1
@@ -1,8 +1,8 @@
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';
5
+ import os, { tmpdir } from 'node:os';
6
6
  import { fork, spawn } from 'node:child_process';
7
7
  import { createInterface } from 'node:readline';
8
8
  import { createRequire } from 'node:module';
@@ -1755,8 +1755,17 @@ const getElectronLaunchOptions = ({
1755
1755
  };
1756
1756
  };
1757
1757
 
1758
+ const getElectronProcessArgs = ({
1759
+ args,
1760
+ platform = process.platform,
1761
+ userDataDir
1762
+ }) => {
1763
+ return ['--remote-debugging-port=0', ...(platform === 'linux' ? ['--no-sandbox'] : []), ...args, `--user-data-dir=${userDataDir}`];
1764
+ };
1765
+
1758
1766
  const SIGINT = 'SIGINT';
1759
1767
 
1768
+ const electronConnectionTimeout = 120_000;
1760
1769
  const devtoolsRegex = /^DevTools listening on (ws:\/\/.*)$/;
1761
1770
  const waitForDevtoolsEndpoint = async child => {
1762
1771
  const {
@@ -1773,10 +1782,13 @@ const waitForDevtoolsEndpoint = async child => {
1773
1782
  reject,
1774
1783
  resolve
1775
1784
  } = Promise.withResolvers();
1785
+ const stderrLines = [];
1776
1786
  const onExit = () => {
1777
- reject(new Error('Electron exited before DevTools endpoint was available'));
1787
+ const details = stderrLines.length > 0 ? `: ${stderrLines.join('\n')}` : '';
1788
+ reject(new Error(`Electron exited before DevTools endpoint was available${details}`));
1778
1789
  };
1779
1790
  const onLine = line => {
1791
+ stderrLines.push(line);
1780
1792
  const match = line.match(devtoolsRegex);
1781
1793
  if (match) {
1782
1794
  resolve(match[1]);
@@ -1802,24 +1814,50 @@ const getFirstPage = async browser => {
1802
1814
  timeout: 15_000
1803
1815
  });
1804
1816
  };
1817
+ const waitForChildExit = async child => {
1818
+ if (child.exitCode !== null || child.signalCode !== null) {
1819
+ return;
1820
+ }
1821
+ await new Promise(resolve => {
1822
+ const timeout = setTimeout(resolve, 5000);
1823
+ child.once('exit', () => {
1824
+ clearTimeout(timeout);
1825
+ resolve();
1826
+ });
1827
+ });
1828
+ };
1829
+ const stopElectronProcess = async child => {
1830
+ if (child.exitCode === null && child.signalCode === null) {
1831
+ child.kill(SIGINT);
1832
+ await waitForChildExit(child);
1833
+ }
1834
+ if (child.exitCode === null && child.signalCode === null) {
1835
+ child.kill('SIGKILL');
1836
+ await waitForChildExit(child);
1837
+ }
1838
+ };
1805
1839
  const closeElectron = async ({
1806
1840
  browser,
1807
- child
1841
+ child,
1842
+ userDataDir
1808
1843
  }) => {
1809
1844
  try {
1810
1845
  await browser.close();
1811
1846
  } catch {
1812
1847
  // ignore close errors during cleanup
1813
1848
  }
1814
- if (!child.killed) {
1815
- child.kill(SIGINT);
1816
- }
1849
+ await stopElectronProcess(child);
1850
+ await rm(userDataDir, {
1851
+ force: true,
1852
+ recursive: true
1853
+ });
1817
1854
  };
1818
1855
  const createElectronLaunch = ({
1819
1856
  browser,
1820
1857
  child,
1821
1858
  page,
1822
- signal
1859
+ signal,
1860
+ userDataDir
1823
1861
  }) => {
1824
1862
  let disposed = false;
1825
1863
  const dispose = async () => {
@@ -1832,7 +1870,8 @@ const createElectronLaunch = ({
1832
1870
  process.off('SIGTERM', handleSigterm);
1833
1871
  await closeElectron({
1834
1872
  browser,
1835
- child
1873
+ child,
1874
+ userDataDir
1836
1875
  });
1837
1876
  };
1838
1877
  const handleAbort = () => {
@@ -1874,7 +1913,12 @@ const startElectron = async ({
1874
1913
  signal
1875
1914
  }) => {
1876
1915
  const launchOptions = getElectronLaunchOptions(runtimeOptions);
1877
- const child = spawn(launchOptions.executablePath, ['--remote-debugging-port=0', ...launchOptions.args], {
1916
+ const userDataDir = await mkdtemp(join(tmpdir(), 'test-with-playwright-electron-'));
1917
+ const args = getElectronProcessArgs({
1918
+ args: launchOptions.args,
1919
+ userDataDir
1920
+ });
1921
+ const child = spawn(launchOptions.executablePath, args, {
1878
1922
  env: launchOptions.env,
1879
1923
  stdio: ['ignore', 'ignore', 'pipe']
1880
1924
  });
@@ -1884,13 +1928,16 @@ const startElectron = async ({
1884
1928
  const {
1885
1929
  chromium
1886
1930
  } = await import('@playwright/test');
1887
- browser = await chromium.connectOverCDP(endpoint);
1931
+ browser = await chromium.connectOverCDP(endpoint, {
1932
+ timeout: electronConnectionTimeout
1933
+ });
1888
1934
  const page = await getFirstPage(browser);
1889
1935
  return createElectronLaunch({
1890
1936
  browser,
1891
1937
  child,
1892
1938
  page,
1893
- signal
1939
+ signal,
1940
+ userDataDir
1894
1941
  });
1895
1942
  } catch (error) {
1896
1943
  if (browser) {
@@ -1900,9 +1947,11 @@ const startElectron = async ({
1900
1947
  // ignore close errors during cleanup
1901
1948
  }
1902
1949
  }
1903
- if (!child.killed) {
1904
- child.kill(SIGINT);
1905
- }
1950
+ await stopElectronProcess(child);
1951
+ await rm(userDataDir, {
1952
+ force: true,
1953
+ recursive: true
1954
+ });
1906
1955
  throw error;
1907
1956
  }
1908
1957
  };
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.0",
4
4
  "description": "Worker package for test-with-playwright",
5
5
  "repository": {
6
6
  "type": "git",