Skip to content

Commit f25fdf4

Browse files
authored
test: run tests under ELECTRON_AS_NODE (#1966)
Assisted-by: Claude Opus 4.8
1 parent 5bd6285 commit f25fdf4

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"package": "electron-forge package",
2424
"publish": "electron-forge publish",
2525
"start": "tsx ./tools/clean-webpack.ts && electron-forge start --",
26-
"test": "vitest run",
27-
"test:ci": "vitest run --coverage --reporter=default --reporter=junit --outputFile=./reports/report.xml",
28-
"test:report": "vitest run --reporter=json --outputFile=report.json | true",
26+
"test": "tsx ./tools/vitest.ts run",
27+
"test:ci": "tsx ./tools/vitest.ts run --coverage --reporter=default --reporter=junit --outputFile=./reports/report.xml",
28+
"test:report": "tsx ./tools/vitest.ts run --reporter=json --outputFile=report.json | true",
2929
"tsc": "tsc --noEmit -p .",
3030
"electron-releases": "tsx ./tools/fetch-releases.ts",
3131
"postinstall": "husky && npm run electron-releases"

tools/vitest.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { spawn } from 'node:child_process';
2+
import * as path from 'node:path';
3+
4+
// When required from Node.js (rather than Electron's main process), the
5+
// `electron` module resolves to the path of the Electron executable.
6+
const electron = require('electron') as unknown as string;
7+
const vitestPkg = require.resolve('vitest/package.json');
8+
const vitestCli = path.join(path.dirname(vitestPkg), 'vitest.mjs');
9+
10+
const child = spawn(electron, [vitestCli, ...process.argv.slice(2)], {
11+
cwd: path.join(__dirname, '..'),
12+
env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' },
13+
stdio: 'inherit',
14+
});
15+
16+
child.on('exit', (code, signal) => {
17+
if (signal) {
18+
process.kill(process.pid, signal);
19+
} else {
20+
process.exit(code ?? 0);
21+
}
22+
});

0 commit comments

Comments
 (0)