n8n/packages/cli/test/integration/shared/utils/test-command.ts
Konstantin Tieber db3b57b040
Some checks are pending
CI: Master (Build, Test, Lint) / Build for Github Cache (push) Waiting to run
CI: Master (Build, Test, Lint) / Unit tests (22.x) (push) Waiting to run
CI: Master (Build, Test, Lint) / Unit tests (24.13.1) (push) Waiting to run
CI: Master (Build, Test, Lint) / Unit tests (25.x) (push) Waiting to run
CI: Master (Build, Test, Lint) / Lint (push) Waiting to run
CI: Master (Build, Test, Lint) / Performance (push) Waiting to run
CI: Master (Build, Test, Lint) / Notify Slack on failure (push) Blocked by required conditions
feat(core): Add flag to import workflow cli to activate workflow on import (#29341)
2026-05-05 11:25:41 +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 = jest.fn();
process.exit = jest.fn() as never;
beforeAll(async () => {
await testDb.init();
});
beforeEach(() => {
jest.clearAllMocks();
mockInstance(TelemetryEventRelay);
});
afterAll(async () => {
await testDb.terminate();
jest.restoreAllMocks();
});
const run = async (argv: string[] = []) => {
const command = new Command();
const rawFlags = argvParser(argv);
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 };
};