n8n/packages/cli/test/integration/shared/utils/test-command.ts
Matsu 0d465d4428
test: Migrate cli package from Jest to Vitest (#31804)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
2026-06-29 21:19:56 +03:00

45 lines
1.2 KiB
TypeScript

import { testDb, mockInstance } from '@n8n/backend-test-utils';
import { CommandMetadata, type CommandClass } from '@n8n/decorators';
import { Container } from '@n8n/di';
import argvParser from 'yargs-parser';
import { MessageEventBus } from '@/eventbus/message-event-bus/message-event-bus';
import { TelemetryEventRelay } from '@/events/relays/telemetry.event-relay';
mockInstance(MessageEventBus);
export const setupTestCommand = <T extends CommandClass>(Command: T) => {
// mock SIGINT/SIGTERM registration
process.once = vi.fn();
process.exit = vi.fn() as never;
beforeAll(async () => {
await testDb.init();
});
beforeEach(() => {
vi.clearAllMocks();
mockInstance(TelemetryEventRelay);
});
afterAll(async () => {
await testDb.terminate();
vi.restoreAllMocks();
});
const run = async (argv: string[] = []) => {
const command = new Command();
const rawFlags = argvParser(argv, { string: ['id'] });
const entry = Container.get(CommandMetadata)
.getEntries()
.find(([, e]) => e.class === Command)?.[1];
command.flags = entry?.flagsSchema ? entry.flagsSchema.parse(rawFlags) : rawFlags;
await command.init?.();
await command.run();
return command;
};
return { run };
};