From 3144ce3eedf0f9f29bc15fc1e51417e65bc7a3d5 Mon Sep 17 00:00:00 2001 From: Junyi Date: Sun, 21 Jul 2024 15:16:55 +0800 Subject: [PATCH] refactor(test): change mock cluster constructor arguments (#4917) * refactor(test): change mock cluster constructor arguments * refactor(test): change mock cluster constructor arguments --- packages/core/test/src/server/mock-cluster.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/core/test/src/server/mock-cluster.ts b/packages/core/test/src/server/mock-cluster.ts index c7d577335..4afdacff8 100644 --- a/packages/core/test/src/server/mock-cluster.ts +++ b/packages/core/test/src/server/mock-cluster.ts @@ -14,22 +14,23 @@ import { getPortPromise } from 'portfinder'; import { uid } from '@nocobase/utils'; import { createMockServer } from './mock-server'; -type ProcessConfig = { - env: Record; -}; - type ClusterOptions = { - script: string; - config: ProcessConfig; - instances: number; + script?: string; + env?: Record; plugins?: string[]; + instances?: number; }; export class MockCluster { + private script = `${process.env.APP_PACKAGE_ROOT}/src/index.ts`; private processes = []; private mockApp; - constructor(private options: ClusterOptions) {} + constructor(private options: ClusterOptions = {}) { + if (options.script) { + this.script = options.script; + } + } async start(): Promise { // NOTE: use this for install app first @@ -40,12 +41,12 @@ export class MockCluster { this.processes = []; const ports = []; - for (let i = 0; i < this.options.instances; i++) { + for (let i = 0; i < (this.options.instances ?? 2); i++) { const port = await getPortPromise(); - const childProcess = spawn('node', ['./node_modules/tsx/dist/cli.mjs', this.options.script, 'start'], { + const childProcess = spawn('node', ['./node_modules/tsx/dist/cli.mjs', this.script, 'start'], { env: { ...process.env, - ...this.options.config.env, + ...this.options.env, APP_PORT: `${port}`, APPEND_PRESET_BUILT_IN_PLUGINS: (this.options.plugins ?? []).join(','), SOCKET_PATH: `storage/tests/gateway-cluster-${uid()}.sock`,