From d7b52fbed7dc796dc9bedabe8bbccbe453b9ee15 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Fri, 4 Oct 2024 13:01:48 +0200 Subject: [PATCH] some more --- .../playwright/src/common/configLoader.ts | 16 +++++++- packages/playwright/src/runner/tasks.ts | 16 +++++--- tests/playwright-test/global-setup.spec.ts | 41 +++++++++++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/packages/playwright/src/common/configLoader.ts b/packages/playwright/src/common/configLoader.ts index 5ed1c68ea7..1675219b6d 100644 --- a/packages/playwright/src/common/configLoader.ts +++ b/packages/playwright/src/common/configLoader.ts @@ -137,13 +137,25 @@ function validateConfig(file: string, config: Config) { } if ('globalSetup' in config && config.globalSetup !== undefined) { - if (typeof config.globalSetup !== 'string') + if (Array.isArray(config.globalSetup)) { + config.globalSetup.forEach((item, index) => { + if (typeof item !== 'string') + throw errorWithFile(file, `config.globalSetup[${index}] must be a string`); + }); + } else if (typeof config.globalSetup !== 'string') { throw errorWithFile(file, `config.globalSetup must be a string`); + } } if ('globalTeardown' in config && config.globalTeardown !== undefined) { - if (typeof config.globalTeardown !== 'string') + if (Array.isArray(config.globalTeardown)) { + config.globalTeardown.forEach((item, index) => { + if (typeof item !== 'string') + throw errorWithFile(file, `config.globalTeardown[${index}] must be a string`); + }); + } else if (typeof config.globalTeardown !== 'string') { throw errorWithFile(file, `config.globalTeardown must be a string`); + } } if ('globalTimeout' in config && config.globalTimeout !== undefined) { diff --git a/packages/playwright/src/runner/tasks.ts b/packages/playwright/src/runner/tasks.ts index 25e33a7f2b..b0220a6984 100644 --- a/packages/playwright/src/runner/tasks.ts +++ b/packages/playwright/src/runner/tasks.ts @@ -99,9 +99,8 @@ export function createGlobalSetupTasks(config: FullConfigInternal) { tasks.push(createRemoveOutputDirsTask()); tasks.push(...createPluginSetupTasks(config)); if (config.globalSetups.length || config.globalTeardowns.length) { - const length = Math.max(config.globalSetups.length, config.globalTeardowns.length); - for (let i = 0; i < length; i++) - tasks.push(createGlobalSetupTask(i, length)); + for (let i = 0; i < Math.max(config.globalSetups.length, config.globalTeardowns.length); i++) + tasks.push(createGlobalSetupTask(i, config)); } return tasks; } @@ -164,14 +163,19 @@ function createPluginBeginTask(plugin: TestRunnerPluginRegistration): Task { +function createGlobalSetupTask(index: number, config: FullConfigInternal): Task { let globalSetupResult: any; let globalSetupFinished = false; let teardownHook: any; let title = 'global setup'; - if (length > 1) - title += ` (${index + 1}/${length})`; + if (config.globalSetups.length > 1 || config.globalSetups.length > 1) { + const files = [ + config.globalSetups[index], + config.globalTeardowns[index], + ].filter(Boolean).join(', '); + title += ` (${files})`; + } return { title, diff --git a/tests/playwright-test/global-setup.spec.ts b/tests/playwright-test/global-setup.spec.ts index 3d28be82cd..001299d857 100644 --- a/tests/playwright-test/global-setup.spec.ts +++ b/tests/playwright-test/global-setup.spec.ts @@ -386,3 +386,44 @@ test('teardown after error', async ({ runInlineTest }) => { 'teardown 1', ]); }); + +function expectInOrder(output: string, tokens: string[]) { + for (const token of tokens) + expect(output).toContain(token); + + const positions = tokens.map(token => `${output.indexOf(token).toString().padStart(3, '0')} ${token}`); + expect(positions, 'order is correct').toEqual(positions.toSorted()); +} + +test('globalSetup should support multiple', async ({ runInlineTest }) => { + const { passed, output } = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { + globalSetup: ['./globalSetup1.ts','./globalSetup2.ts','./globalSetup3.ts'], + globalTeardown: ['./globalTeardown1.ts', './globalTeardown2.ts'], + }; + `, + 'globalSetup1.ts': `module.exports = () => console.log('globalSetup1');`, + 'globalSetup2.ts': `module.exports = () => console.log('globalSetup2');`, + 'globalSetup3.ts': `module.exports = () => console.log('globalSetup3');`, + 'globalTeardown1.ts': `module.exports = () => console.log('globalTeardown1');`, + 'globalTeardown2.ts': `module.exports = () => console.log('globalTeardown2');`, + + 'a.test.js': ` + import { test } from '@playwright/test'; + test('a', () => console.log('test a')); + test('b', () => console.log('test b')); + `, + }, { reporter: 'line' }); + + expect(passed).toBe(2); + expectInOrder(output, [ + 'globalSetup1', + 'globalSetup2', + 'globalSetup3', + 'test a', + 'test b', + 'globalTeardown2', + 'globalTeardown1', + ]); +});