Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"tsx": "^4.21.0",
"typedoc": "~0.28.0",
"typescript": "^6.0.2",
"vitest": "^3.2.4",
"vitest": "^4.1.5",
"xvfb-maybe": "^0.2.1"
},
"dependenciesMeta": {
Expand Down
43 changes: 11 additions & 32 deletions vitest.electron-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,23 @@
import { RunnerTestFile, Task } from 'vitest';
import { Reporter } from 'vitest/reporters';

function countFailures(tasks: Task[]): number {
let count = 0;
for (const task of tasks) {
if (task.result?.state === 'fail') {
count++;
}
if ('tasks' in task && task.tasks) {
count += countFailures(task.tasks);
}
}
return count;
}
import type { Reporter, TestModule, TestRunEndReason } from 'vitest/node';

export default class ElectronExitReporter implements Reporter {
onFinished(files: RunnerTestFile[], errors: unknown[]) {
onTestRunEnd(
_testModules: ReadonlyArray<TestModule>,
unhandledErrors: ReadonlyArray<unknown>,
reason: TestRunEndReason,
) {
if (!process.versions.electron) {
return;
}

let failureCount = 0;

for (const file of files) {
if (file.result?.state === 'fail') {
failureCount++;
}
if (file.tasks) {
failureCount += countFailures(file.tasks);
}
}

const hasExecutionErrors = errors && errors.length > 0;
const exitCode = failureCount > 0 || hasExecutionErrors ? 1 : 0;
const hasFailures = reason === 'failed';
const hasExecutionErrors = unhandledErrors && unhandledErrors.length > 0;
const exitCode = hasFailures || hasExecutionErrors ? 1 : 0;

// In Electron, vitest calls process.exit() after onFinished with its own
// exit code, which doesn't account for nested test failures. Override
// process.exit so our exit code takes precedence on failure.
// In Electron, vitest calls process.exit() after onTestRunEnd with its own
// exit code. Override process.exit so our exit code takes precedence on failure.
if (exitCode !== 0) {
const originalExit = process.exit;

Check warning on line 20 in vitest.electron-reporter.ts

View workflow job for this annotation

GitHub Actions / Test (22.12.x, windows-latest)

typescript-eslint(unbound-method)

Avoid referencing unbound methods which may cause unintentional scoping of `this`.

Check warning on line 20 in vitest.electron-reporter.ts

View workflow job for this annotation

GitHub Actions / Test (22.12.x, windows-latest)

typescript-eslint(unbound-method)

Avoid referencing unbound methods which may cause unintentional scoping of `this`.

Check warning on line 20 in vitest.electron-reporter.ts

View workflow job for this annotation

GitHub Actions / Test (22.12.x, ubuntu-22.04)

typescript-eslint(unbound-method)

Avoid referencing unbound methods which may cause unintentional scoping of `this`.

Check warning on line 20 in vitest.electron-reporter.ts

View workflow job for this annotation

GitHub Actions / Test (22.12.x, ubuntu-22.04)

typescript-eslint(unbound-method)

Avoid referencing unbound methods which may cause unintentional scoping of `this`.

Check warning on line 20 in vitest.electron-reporter.ts

View workflow job for this annotation

GitHub Actions / Test (22.12.x, macos-latest)

typescript-eslint(unbound-method)

Avoid referencing unbound methods which may cause unintentional scoping of `this`.

Check warning on line 20 in vitest.electron-reporter.ts

View workflow job for this annotation

GitHub Actions / Test (22.12.x, macos-latest)

typescript-eslint(unbound-method)

Avoid referencing unbound methods which may cause unintentional scoping of `this`.
process.exit = ((_code?: number) => {
originalExit.call(process, exitCode);
}) as never;
Expand Down
Loading
Loading