-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathvitest.electron-reporter.ts
More file actions
47 lines (41 loc) · 1.27 KB
/
vitest.electron-reporter.ts
File metadata and controls
47 lines (41 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
}
export default class ElectronExitReporter implements Reporter {
onFinished(files: RunnerTestFile[], errors: unknown[]) {
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;
// 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.
if (exitCode !== 0) {
const originalExit = process.exit;
process.exit = ((_code?: number) => {
originalExit.call(process, exitCode);
}) as never;
}
}
}