agent-ecosystem/test/setup.ts
iliya 4d471ef7be fix: preserve process methods in test setup to avoid vitest worker crash
Stub process with Proxy instead of spread — spreading loses inherited
methods (listeners, on, etc.), causing process.listeners is not a function
and ERR_IPC_CHANNEL_CLOSED in CI on Windows.

Made-with: Cursor
2026-03-03 01:21:23 +02:00

54 lines
1.4 KiB
TypeScript

/**
* Vitest setup file.
* Runs before each test file.
*/
import { afterEach, beforeEach, expect, vi } from 'vitest';
// Mock process.env for tests that need home directory.
// Use Proxy so process keeps all methods (listeners, on, etc.) — spreading loses them.
const testEnv = { ...process.env, HOME: '/home/testuser' };
vi.stubGlobal(
'process',
new Proxy(process, {
get(target, prop) {
if (prop === 'env') return testEnv;
return (target as Record<string | symbol, unknown>)[prop];
},
})
);
let errorSpy: ReturnType<typeof vi.spyOn>;
let warnSpy: ReturnType<typeof vi.spyOn>;
function formatConsoleCall(args: unknown[]): string {
return args
.map((arg) => {
if (arg instanceof Error) {
return arg.message;
}
return String(arg);
})
.join(' ');
}
beforeEach(() => {
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
});
afterEach(() => {
const unexpectedErrors = errorSpy.mock.calls.map(formatConsoleCall);
const unexpectedWarnings = warnSpy.mock.calls.map(formatConsoleCall);
errorSpy.mockRestore();
warnSpy.mockRestore();
expect(unexpectedErrors, `Unexpected console.error calls:\n${unexpectedErrors.join('\n')}`).toEqual(
[]
);
expect(
unexpectedWarnings,
`Unexpected console.warn calls:\n${unexpectedWarnings.join('\n')}`
).toEqual([]);
});