Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(test-runner): validate NaN and non-positive worker percentages (#41681)

Yury Semikhatskycc9a80340e83
diff --git a/packages/playwright/src/common/config.ts b/packages/playwright/src/common/config.ts
index 4dd22b11d8..fc4e378625 100644
--- a/packages/playwright/src/common/config.ts
+++ b/packages/playwright/src/common/config.ts
@@ -221,8 +221,13 @@ function resolveReporters(reporters: Config['reporter'], rootDir: string): Repor
function resolveWorkers(workers: string | number): number {
if (typeof workers === 'string') {
if (workers.endsWith('%')) {
+ const percent = parseInt(workers, 10);
+ if (isNaN(percent))
+ throw new Error(`Workers ${workers} must be a number or percentage.`);
+ if (percent < 1)
+ throw new Error(`Workers must be a positive number, received ${percent}.`);
const cpus = os.cpus().length;
- return Math.max(1, Math.floor(cpus * (parseInt(workers, 10) / 100)));
+ return Math.max(1, Math.floor(cpus * (percent / 100)));
}
const parsedWorkers = parseInt(workers, 10);
if (isNaN(parsedWorkers))
diff --git a/tests/playwright-test/config.spec.ts b/tests/playwright-test/config.spec.ts
index ab04bf8970..9c4acda6e4 100644
--- a/tests/playwright-test/config.spec.ts
+++ b/tests/playwright-test/config.spec.ts
@@ -573,6 +573,18 @@ test('should throw when workers is negative via CLI (regression for #39938)', as
expect(result.output).toContain('Workers must be a positive number');
});
+test('should throw when workers is an invalid percentage via CLI (regression for #41679)', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'playwright.config.ts': `module.exports = {};`,
+ 'a.test.ts': `
+ import { test, expect } from '@playwright/test';
+ test('fails', () => { expect(1).toBe(2); });
+ `,
+ }, { workers: 'abc%' });
+ expect(result.exitCode).toBe(1);
+ expect(result.output).toContain('Workers abc% must be a number or percentage.');
+});
+
test('should work with undefined values and base', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `