diff --git a/spec/_vitest_runner/package.json b/spec/_vitest_runner/package.json index 08720d7e66..4a6c0be163 100644 --- a/spec/_vitest_runner/package.json +++ b/spec/_vitest_runner/package.json @@ -1,6 +1,6 @@ { - "name": "electron-vitest-poc", - "productName": "Electron Vitest POC", + "name": "electron-test-main", + "productName": "Electron Test Main", "version": "0.1.0", "main": "worker-entry.js" } diff --git a/spec/_vitest_runner/worker-entry.js b/spec/_vitest_runner/worker-entry.js index 5611b1aec4..2ac95d7c9a 100644 --- a/spec/_vitest_runner/worker-entry.js +++ b/spec/_vitest_runner/worker-entry.js @@ -4,6 +4,7 @@ const { app, protocol } = require('electron'); +const fs = require('node:fs'); const path = require('node:path'); const v8 = require('node:v8'); @@ -18,12 +19,15 @@ if (process.env.ELECTRON_TEST_DISABLE_HARDWARE_ACCELERATION) { app.disableHardwareAcceleration(); } -// The pool allocates (mkdtemp) and cleans up this directory; the worker just -// points Electron at it before app ready. -const userDataDir = process.env.ELECTRON_VITEST_USER_DATA_DIR; -if (!userDataDir) { +// The pool allocates (mkdtemp) and cleans up the parent directory; the worker +// adds app.name so tests that assert getPath('userData') contains the app name +// still hold. +const userDataBase = process.env.ELECTRON_VITEST_USER_DATA_DIR; +if (!userDataBase) { throw new Error('ELECTRON_VITEST_USER_DATA_DIR was not provided by the pool'); } +const userDataDir = path.join(userDataBase, app.name); +fs.mkdirSync(userDataDir, { recursive: true }); app.setPath('userData', userDataDir); v8.setFlagsFromString('--expose_gc'); diff --git a/spec/lib/net-helpers.ts b/spec/lib/net-helpers.ts index 9877b5e0f7..6dda1b2f76 100644 --- a/spec/lib/net-helpers.ts +++ b/spec/lib/net-helpers.ts @@ -2,9 +2,20 @@ import { expect } from 'chai'; import * as dns from 'node:dns'; import * as http from 'node:http'; -import { Socket } from 'node:net'; +import * as http2 from 'node:http2'; +import * as https from 'node:https'; +import { AddressInfo, Socket } from 'node:net'; +import * as url from 'node:url'; -import { defer, listen } from './spec-helpers'; +import { defer } from './defer-helpers'; + +export async function listen(server: http.Server | https.Server | http2.Http2SecureServer) { + const hostname = '127.0.0.1'; + await new Promise((resolve) => server.listen(0, hostname, () => resolve())); + const { port } = server.address() as AddressInfo; + const protocol = server instanceof http.Server ? 'http' : 'https'; + return { port, hostname, url: url.format({ protocol, hostname, port }) }; +} // See https://github.com/nodejs/node/issues/40702. dns.setDefaultResultOrder('ipv4first'); diff --git a/spec/lib/spec-helpers.ts b/spec/lib/spec-helpers.ts index c3af151d3d..64aabce442 100644 --- a/spec/lib/spec-helpers.ts +++ b/spec/lib/spec-helpers.ts @@ -5,17 +5,14 @@ import { afterAll, beforeAll, describe, it } from 'vitest'; import * as childProcess from 'node:child_process'; import * as http from 'node:http'; -import * as http2 from 'node:http2'; -import * as https from 'node:https'; -import * as net from 'node:net'; import * as path from 'node:path'; import { setTimeout } from 'node:timers/promises'; -import * as url from 'node:url'; import * as v8 from 'node:v8'; import { defer } from './defer-helpers'; export { defer, runCleanupFunctions } from './defer-helpers'; +export { listen } from './net-helpers'; export const ifit = (condition: boolean) => it.runIf(condition); export const ifdescribe = (condition: boolean) => describe.runIf(condition); @@ -240,14 +237,6 @@ export const itremote = Object.assign( } ); -export async function listen(server: http.Server | https.Server | http2.Http2SecureServer) { - const hostname = '127.0.0.1'; - await new Promise((resolve) => server.listen(0, hostname, () => resolve())); - const { port } = server.address() as net.AddressInfo; - const protocol = server instanceof http.Server ? 'http' : 'https'; - return { port, hostname, url: url.format({ protocol, hostname, port }) }; -} - export function isTestingBindingAvailable() { try { process._linkedBinding('electron_common_testing');