From f954891491116afd73472b44943b15aa62bea9af Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Wed, 20 Jul 2022 12:41:35 -0700 Subject: [PATCH] fix(runner): globalTeardown without globalSetup should work (#15814) --- packages/playwright-test/src/runner.ts | 17 ++++++++++------ tests/playwright-test/global-setup.spec.ts | 23 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/packages/playwright-test/src/runner.ts b/packages/playwright-test/src/runner.ts index 476e6a9b21..2de30e8d0d 100644 --- a/packages/playwright-test/src/runner.ts +++ b/packages/playwright-test/src/runner.ts @@ -473,12 +473,17 @@ export class Runner { } // The do global setup. - if (!sigintWatcher.hadSignal() && config.globalSetup) { - const hook = await this._loader.loadGlobalHook(config.globalSetup, 'globalSetup'); - await Promise.race([ - Promise.resolve().then(() => hook(this._loader.fullConfig())).then((r: any) => globalSetupResult = r || ''), - sigintWatcher.promise(), - ]); + if (!sigintWatcher.hadSignal()) { + if (config.globalSetup) { + const hook = await this._loader.loadGlobalHook(config.globalSetup, 'globalSetup'); + await Promise.race([ + Promise.resolve().then(() => hook(this._loader.fullConfig())).then((r: any) => globalSetupResult = r || ''), + sigintWatcher.promise(), + ]); + } else { + // Make sure we run globalTeardown. + globalSetupResult = ''; + } } }, result); diff --git a/tests/playwright-test/global-setup.spec.ts b/tests/playwright-test/global-setup.spec.ts index dc635a0129..ad6a3e73e1 100644 --- a/tests/playwright-test/global-setup.spec.ts +++ b/tests/playwright-test/global-setup.spec.ts @@ -48,6 +48,29 @@ test('globalSetup and globalTeardown should work', async ({ runInlineTest }) => expect(output).toContain('teardown=42'); }); +test('standalone globalTeardown should work', async ({ runInlineTest }) => { + const { results, output } = await runInlineTest({ + 'playwright.config.ts': ` + import * as path from 'path'; + module.exports = { + globalTeardown: './globalTeardown.ts', + }; + `, + 'globalTeardown.ts': ` + module.exports = async () => { + console.log('got my teardown'); + }; + `, + 'a.test.js': ` + const { test } = pwt; + test('should work', async ({}, testInfo) => { + }); + `, + }); + expect(results[0].status).toBe('passed'); + expect(output).toContain('got my teardown'); +}); + test('globalTeardown runs after failures', async ({ runInlineTest }) => { const { results, output } = await runInlineTest({ 'playwright.config.ts': `