fix: align runner app name, userData path, and decouple net-helpers from vitest

- spec/_vitest_runner/package.json: name/productName match spec/package.json
  so app.name assertions hold
- worker-entry: userData is <mkdtemp>/<app.name>/ so getPath('userData')
  still includes the app name
- net-helpers: import defer from defer-helpers and own listen() directly,
  so the utility-process fixture (which ts-node-requires net-helpers) no
  longer pulls in spec-helpers -> vitest
This commit is contained in:
Samuel Attard
2026-04-12 00:11:57 -07:00
parent dc02d7907b
commit 2e826cdaa4
4 changed files with 24 additions and 20 deletions

View File

@@ -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"
}

View File

@@ -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');

View File

@@ -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<void>((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');

View File

@@ -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<void>((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');