Skip to content

Commit 507111c

Browse files
SaranshBSfrancisf
authored andcommitted
reuseWorkers config option
1 parent f7da3c3 commit 507111c

File tree

6 files changed

+28
-1
lines changed

6 files changed

+28
-1
lines changed

packages/playwright-test/src/cli.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const defaultConfig: Config = {
4141
timeout: defaultTimeout,
4242
updateSnapshots: 'missing',
4343
workers: Math.ceil(require('os').cpus().length / 2),
44+
reuseWorkers: true,
4445
};
4546

4647
export function addTestCommand(program: Command) {
@@ -56,6 +57,7 @@ export function addTestCommand(program: Command) {
5657
command.option('--global-timeout <timeout>', `Maximum time this test suite can run in milliseconds (default: unlimited)`);
5758
command.option('-j, --workers <workers>', `Number of concurrent workers, use 1 to run in a single worker (default: number of CPU cores / 2)`);
5859
command.option('--list', `Collect all the tests and report them, but do not run`);
60+
command.option('--no-reuse-workers', `Respawn workers for each spec (default: reuse enabled)`);
5961
command.option('--max-failures <N>', `Stop after the first N failures`);
6062
command.option('--output <dir>', `Folder for output artifacts (default: "test-results")`);
6163
command.option('--quiet', `Suppress stdio`);
@@ -222,6 +224,7 @@ function overridesFromOptions(options: { [key: string]: any }): Config {
222224
timeout: isDebuggerAttached ? 0 : (options.timeout ? parseInt(options.timeout, 10) : undefined),
223225
updateSnapshots: options.updateSnapshots ? 'all' as const : undefined,
224226
workers: options.workers ? parseInt(options.workers, 10) : undefined,
227+
reuseWorkers: options.reuseWorkers === false ? false : undefined,
225228
};
226229
}
227230

packages/playwright-test/src/dispatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class Dispatcher {
231231
// - there are no remaining
232232
// - we are here not because something failed
233233
// - no unrecoverable worker error
234-
if (!remaining.length && !failedTestIds.size && !params.fatalError) {
234+
if (!remaining.length && !failedTestIds.size && !params.fatalError && this._loader.fullConfig().reuseWorkers) {
235235
doneWithJob();
236236
return;
237237
}

packages/playwright-test/src/loader.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class Loader {
102102
this._fullConfig.shard = takeFirst(this._configOverrides.shard, this._config.shard, baseFullConfig.shard);
103103
this._fullConfig.updateSnapshots = takeFirst(this._configOverrides.updateSnapshots, this._config.updateSnapshots, baseFullConfig.updateSnapshots);
104104
this._fullConfig.workers = takeFirst(this._configOverrides.workers, this._config.workers, baseFullConfig.workers);
105+
this._fullConfig.reuseWorkers = takeFirst(this._configOverrides.reuseWorkers, this._config.reuseWorkers, baseFullConfig.reuseWorkers);
105106
this._fullConfig.webServer = takeFirst(this._configOverrides.webServer, this._config.webServer, baseFullConfig.webServer);
106107

107108
for (const project of projects)
@@ -442,6 +443,7 @@ const baseFullConfig: FullConfig = {
442443
updateSnapshots: 'missing',
443444
version: require('../package.json').version,
444445
workers: 1,
446+
reuseWorkers: true,
445447
webServer: null,
446448
};
447449

packages/playwright-test/types/test.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ export interface FullConfig<TestArgs = {}, WorkerArgs = {}> {
10131013
*
10141014
*/
10151015
workers: number;
1016+
reuseWorkers: boolean;
10161017
webServer: WebServerConfig | null;
10171018
}
10181019

tests/playwright-test/worker-index.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,22 @@ test('parallelIndex should be in 0..workers-1', async ({ runInlineTest }) => {
219219
expect(result.exitCode).toBe(0);
220220
expect(result.passed).toBe(20);
221221
});
222+
223+
test('should not reuse workers when reuseWorkers used as false', async ({ runInlineTest }) => {
224+
const result = await runInlineTest({
225+
'1.test.js': `
226+
const { test } = pwt;
227+
test('succeeds 1', async ({}, testInfo) => {
228+
expect(testInfo.workerIndex).toBe(0);
229+
});
230+
`,
231+
'2.spec.ts': `
232+
const { test } = pwt;
233+
test('succeeds 1', async ({}, testInfo) => {
234+
expect(testInfo.workerIndex).toBe(1);
235+
});`
236+
}, { workers: 1}, {}, { additionalArgs: ["--no-reuse-workers"] });
237+
expect(result.passed).toBe(2);
238+
expect(result.failed).toBe(0);
239+
expect(result.exitCode).toBe(0);
240+
});

utils/generate_types/overrides-test.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ interface TestConfig {
115115
updateSnapshots?: UpdateSnapshots;
116116
webServer?: WebServerConfig;
117117
workers?: number;
118+
reuseWorkers?: boolean;
118119

119120
expect?: ExpectSettings;
120121
metadata?: any;
@@ -152,6 +153,7 @@ export interface FullConfig<TestArgs = {}, WorkerArgs = {}> {
152153
shard: Shard;
153154
updateSnapshots: UpdateSnapshots;
154155
workers: number;
156+
reuseWorkers: boolean;
155157
webServer: WebServerConfig | null;
156158
}
157159

0 commit comments

Comments
 (0)