Skip to content

Commit aa46b5a

Browse files
killaguclaude
andcommitted
feat(egg-bin): support vitest threads pool mode
Add --pool flag to egg-bin test command (default: threads) and propagate via EGG_VITEST_POOL env var so @eggjs/mock can auto-switch cluster-client to worker_threads IPC when running in threads pool. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 97a0339 commit aa46b5a

6 files changed

Lines changed: 66 additions & 1 deletion

File tree

plugins/mock/src/lib/format_options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ export function formatOptions(initOptions?: MockOptions): MockApplicationOptions
8888
options.cache = false;
8989
}
9090

91+
// when running under vitest threads pool, use worker_threads start mode
92+
// so egg cluster-client uses thread-based IPC instead of process-based
93+
if (!options.startMode && process.env.EGG_VITEST_POOL === 'threads') {
94+
options.startMode = 'worker_threads';
95+
}
96+
9197
debug('[formatOptions] options: %j', options);
9298
return options;
9399
}

plugins/mock/src/lib/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ export interface MockOptions {
5252
mockCtxStorage?: boolean;
5353

5454
beforeInit?: (app: any) => Promise<void>;
55+
56+
/**
57+
* Start mode for egg cluster-client
58+
*/
59+
startMode?: 'process' | 'worker_threads';
5560
}
5661

5762
export interface MockClusterOptions extends MockOptions {

pnpm-lock.yaml

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/egg-bin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"npminstall": "catalog:",
8686
"rimraf": "catalog:",
8787
"runscript": "catalog:",
88+
"sdk-base": "catalog:",
8889
"typescript": "catalog:"
8990
},
9091
"peerDependencies": {

tools/egg-bin/src/commands/test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export default class Test<T extends typeof Test> extends BaseCommand<T> {
5959
default: false,
6060
char: 'w',
6161
}),
62+
pool: Flags.string({
63+
description: 'vitest worker pool type',
64+
options: ['forks', 'threads'],
65+
default: process.env.EGG_VITEST_POOL ?? 'threads',
66+
}),
6267
};
6368

6469
public async run(): Promise<void> {
@@ -141,6 +146,9 @@ export default class Test<T extends typeof Test> extends BaseCommand<T> {
141146
// expose timeout to test fixtures (e.g. for testing timeout behavior)
142147
process.env.EGG_BIN_TIMEOUT = String(flags.timeout);
143148

149+
// propagate pool mode so downstream code (e.g. @eggjs/mock) can detect it
150+
process.env.EGG_VITEST_POOL = flags.pool;
151+
144152
debug('run test with vitest, files: %o, flags: %o', files, flags);
145153
const config = await this.buildVitestConfig(files);
146154

@@ -245,7 +253,7 @@ export default class Test<T extends typeof Test> extends BaseCommand<T> {
245253
setupFiles,
246254
runner,
247255
reporters: [process.env.TEST_REPORTER ?? 'default'],
248-
pool: 'forks',
256+
pool: flags.pool as 'forks' | 'threads',
249257
fileParallelism: process.env.EGG_FILE_PARALLELISM !== 'false',
250258
// vitest 4 moved poolOptions to top-level
251259
execArgv: [...this.globalExecArgv],

tools/egg-bin/test/commands/test.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ describe('test/commands/test.test.ts', () => {
117117
.end();
118118
});
119119

120+
it('should success on ts with --pool threads and cluster-client', async () => {
121+
const cwd = getFixtures('example-ts-cluster-client');
122+
await coffee
123+
.fork(eggBin, ['test', '--pool', 'threads'], { cwd })
124+
.debug()
125+
.expect('stdout', /index\.test\.ts/)
126+
.expect('stdout', /Tests.*passed/)
127+
.expect('code', 0)
128+
.end();
129+
});
130+
120131
it('should success with --bail', async () => {
121132
await coffee
122133
.fork(eggBin, ['test', '--bail'], { cwd })

0 commit comments

Comments
 (0)