Skip to content

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

fix(runner): skip global hooks in --list mode (#9480)

Yury Semikhatskye827bde1c258
diff --git a/packages/playwright-test/src/runner.ts b/packages/playwright-test/src/runner.ts
index dec52fdfb5..2692e8d920 100644
--- a/packages/playwright-test/src/runner.ts
+++ b/packages/playwright-test/src/runner.ts
@@ -180,7 +180,7 @@ export class Runner {
const webServer = (!list && config.webServer) ? await WebServer.create(config.webServer) : undefined;
let globalSetupResult: any;
- if (config.globalSetup)
+ if (config.globalSetup && !list)
globalSetupResult = await (await this._loader.loadGlobalHook(config.globalSetup, 'globalSetup'))(this._loader.fullConfig());
try {
for (const file of allTestFiles)
@@ -326,7 +326,7 @@ export class Runner {
} finally {
if (globalSetupResult && typeof globalSetupResult === 'function')
await globalSetupResult(this._loader.fullConfig());
- if (config.globalTeardown)
+ if (config.globalTeardown && !list)
await (await this._loader.loadGlobalHook(config.globalTeardown, 'globalTeardown'))(this._loader.fullConfig());
await webServer?.kill();
}
diff --git a/tests/playwright-test/list-mode.spec.ts b/tests/playwright-test/list-mode.spec.ts
index 6ecc3f111d..69fb362419 100644
--- a/tests/playwright-test/list-mode.spec.ts
+++ b/tests/playwright-test/list-mode.spec.ts
@@ -64,3 +64,44 @@ test('should not list tests to stdout when JSON reporter is used', async ({ runI
expect(result.report.suites[0].specs.length).toBe(2);
expect(result.report.suites[0].specs.map(spec => spec.title)).toStrictEqual(['example1', 'example2']);
});
+
+test('globalSetup and globalTeardown should not run', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'playwright.config.ts': `
+ import * as path from 'path';
+ module.exports = {
+ globalSetup: './globalSetup',
+ globalTeardown: './globalTeardown.ts',
+ };
+ `,
+ 'globalSetup.ts': `
+ module.exports = () => {
+ console.log('Running globalSetup');
+ };
+ `,
+ 'globalTeardown.ts': `
+ module.exports = () => {
+ console.log('Running globalTeardown');
+ };
+ `,
+ 'a.test.js': `
+ const { test } = pwt;
+ test('should work 1', async ({}, testInfo) => {
+ console.log('Running test 1');
+ });
+ `,
+ 'b.test.js': `
+ const { test } = pwt;
+ test('should work 2', async ({}, testInfo) => {
+ console.log('Running test 2');
+ });
+ `,
+ }, { 'list': true });
+ expect(result.exitCode).toBe(0);
+ expect(result.output).toContain([
+ `Listing tests:`,
+ ` a.test.js:6:7 › should work 1`,
+ ` b.test.js:6:7 › should work 2`,
+ `Total: 2 tests in 2 files`,
+ ].join('\n'));
+});