diff --git a/packages/playwright/src/common/config.ts b/packages/playwright/src/common/config.ts index 2bf09e31e03c7..7f359ea0553ec 100644 --- a/packages/playwright/src/common/config.ts +++ b/packages/playwright/src/common/config.ts @@ -246,8 +246,12 @@ function resolveWorkers(workers: string | number): number { const parsedWorkers = parseInt(workers, 10); if (isNaN(parsedWorkers)) throw new Error(`Workers ${workers} must be a number or percentage.`); + if (parsedWorkers < 1) + throw new Error(`Workers must be a positive number, received ${parsedWorkers}.`); return parsedWorkers; } + if (workers < 1) + throw new Error(`Workers must be a positive number, received ${workers}.`); return workers; } diff --git a/tests/playwright-test/config.spec.ts b/tests/playwright-test/config.spec.ts index 2c6342f00126e..8289a7823eff4 100644 --- a/tests/playwright-test/config.spec.ts +++ b/tests/playwright-test/config.spec.ts @@ -552,6 +552,58 @@ test('should throw when workers option is invalid', async ({ runInlineTest }) => expect(result.output).toContain('config.workers must be a number or percentage'); }); +test('should throw when workers is 0 in config', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { workers: 0 }; + `, + 'a.test.ts': ` + import { test, expect } from '@playwright/test'; + test('pass', async ({}) => {}); + `, + }); + expect(result.exitCode).toBe(1); + expect(result.output).toContain('must be a positive'); +}); + +test('should throw when workers is negative in config', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { workers: -1 }; + `, + 'a.test.ts': ` + import { test, expect } from '@playwright/test'; + test('pass', async ({}) => {}); + `, + }); + expect(result.exitCode).toBe(1); + expect(result.output).toContain('must be a positive'); +}); + +test('should throw when workers is 0 via CLI', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'playwright.config.ts': `module.exports = {};`, + 'a.test.ts': ` + import { test, expect } from '@playwright/test'; + test('pass', async ({}) => {}); + `, + }, { workers: 0 }); + expect(result.exitCode).not.toBe(0); + expect(result.output).toContain('must be a positive'); +}); + +test('should throw when workers is negative via CLI', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'playwright.config.ts': `module.exports = {};`, + 'a.test.ts': ` + import { test, expect } from '@playwright/test'; + test('pass', async ({}) => {}); + `, + }, { workers: -1 }); + expect(result.exitCode).not.toBe(0); + expect(result.output).toContain('must be a positive'); +}); + test('should work with undefined values and base', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': `