From a26fe65db3ec8639fb89e836faa0f723c450c988 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Wed, 14 Jul 2021 19:01:46 +0200 Subject: [PATCH] fix(test-runner): start webServer before globalSetup/teardown (#7604) --- docs/src/chrome-extensions.md | 2 +- src/test/runner.ts | 4 ++-- tests/playwright-test/web-server.spec.ts | 27 +++++++++++++++++++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/docs/src/chrome-extensions.md b/docs/src/chrome-extensions.md index 90d5c7589f..3672a0b255 100644 --- a/docs/src/chrome-extensions.md +++ b/docs/src/chrome-extensions.md @@ -59,7 +59,7 @@ asyncio.run(main()) ``` ```python sync -from playwright.async_api import sync_playwright +from playwright.sync_api import sync_playwright path_to_extension = "./my-extension" user_data_dir = "/tmp/test-user-data-dir" diff --git a/src/test/runner.ts b/src/test/runner.ts index 12b6ac53a6..86646f8c31 100644 --- a/src/test/runner.ts +++ b/src/test/runner.ts @@ -167,10 +167,10 @@ export class Runner { testFiles.forEach(file => allTestFiles.add(file)); } + const webServer: WebServer|null = config.webServer ? await WebServer.create(config.webServer) : null; let globalSetupResult: any; if (config.globalSetup) globalSetupResult = await (await this._loader.loadGlobalHook(config.globalSetup, 'globalSetup'))(this._loader.fullConfig()); - const webServer: WebServer|null = config.webServer ? await WebServer.create(config.webServer) : null; try { for (const file of allTestFiles) await this._loader.loadTestFile(file); @@ -263,11 +263,11 @@ export class Runner { await this._reporter.onEnd({ status: failed ? 'failed' : 'passed' }); return { status: failed ? 'failed' : 'passed' }; } finally { - await webServer?.kill(); if (globalSetupResult && typeof globalSetupResult === 'function') await globalSetupResult(this._loader.fullConfig()); if (config.globalTeardown) await (await this._loader.loadGlobalHook(config.globalTeardown, 'globalTeardown'))(this._loader.fullConfig()); + await webServer?.kill(); } } } diff --git a/tests/playwright-test/web-server.spec.ts b/tests/playwright-test/web-server.spec.ts index 8ef2fb4355..c00d697545 100644 --- a/tests/playwright-test/web-server.spec.ts +++ b/tests/playwright-test/web-server.spec.ts @@ -35,13 +35,38 @@ test('should create a server', async ({ runInlineTest }, { workerIndex }) => { webServer: { command: 'node ${JSON.stringify(path.join(__dirname, 'assets', 'simple-server.js'))} ${port}', port: ${port}, - } + }, + globalSetup: 'globalSetup.ts', + globalTeardown: 'globalTeardown.ts', + }; + `, + 'globalSetup.ts': ` + module.exports = async () => { + console.log('globalSetup') + return () => console.log('globalSetup teardown'); + }; + `, + 'globalTeardown.ts': ` + module.exports = async () => { + const http = require("http"); + const response = await new Promise(resolve => { + const request = http.request("http://localhost:${port}/hello", resolve); + request.end(); + }) + console.log('globalTeardown-status-'+response.statusCode) }; `, }); expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); expect(result.report.suites[0].specs[0].tests[0].results[0].status).toContain('passed'); + + const expectedLogMessages = ['Starting WebServer', 'globalSetup', 'globalSetup teardown', 'globalTeardown-status-200']; + const actualLogMessages = expectedLogMessages.map(log => ({ + log, + index: result.output.indexOf(log), + })).sort((a, b) => a.index - b.index).filter(l => l.index !== -1).map(l => l.log); + expect(actualLogMessages).toStrictEqual(expectedLogMessages); }); test('should create a server with environment variables', async ({ runInlineTest }, { workerIndex }) => {